当前位置:   article > 正文

如何使用Python numpy.where()方法

python中方法名().where()

In Python, we can use the numpy.where() function to select elements from a numpy array, based on a condition.

在Python中,我们可以使用numpy.where()函数根据条件从numpy数组中选择元素。

Not only that, but we can perform some operations on those elements if the condition is satisfied.

不仅如此,如果条件满足,我们可以对这些元素执行一些操作。

Let’s look at how we can use this function, using some illustrative examples!

让我们来看一些示例,看看如何使用此功能!



Python numpy.where()的语法 (Syntax of Python numpy.where())

This function accepts a numpy-like array (ex. a NumPy array of integers/booleans).

此函数接受类似numpy的数组(例如,整数/布尔值的NumPy数组)。

It returns a new numpy array, after filtering based on a condition, which is a numpy-like array of boolean values.

在根据condition过滤后,它返回一个新的numpy数组,这是一个类似于numpy的布尔值数组。

For example, condition can take the value of array([[True, True, True]]), which is a numpy-like boolean array. (By default, NumPy only supports numeric values, but we can cast them to bool also)

例如, condition可以采用array([[True, True, True]] )的值,它是一个类似numpy的布尔数组。 (默认情况下,NumPy仅支持数字值,但我们也可以将其boolbool

For example, if condition is array([[True, True, False]]), and our array is a = ndarray([[1, 2, 3]]), on applying a condition to array (a[:, condition]), we will get the array ndarray([[1 2]]).

例如,如果conditionarray([[True, True, False]]) ,而我们的数组为a = ndarray([[1, 2, 3]])a = ndarray([[1, 2, 3]])条件应用于数组( a[:, condition] ),我们将得到数组ndarray([[1 2]])

  1. import numpy as np
  2. a = np.arange(10)
  3. print(a[a <= 2]) # Will only capture elements <= 2 and ignore others

Output

输出量

  1. array([0 1 2])

NOTE: The same condition condition can also be represented as a <= 2. This is the recommended format for the condition array, as it is very tedious writing it as a boolean array

注意 :相同的条件条件也可以表示为<= 2 。 这是条件数组的推荐格式,因为将其编写为布尔数组非常繁琐

But what if we want to preserve the dimension of the result, and not lose out on elements from our original array? We can use numpy.where() for this.

但是,如果我们想保留结果的维数而不丢失我们原始数组中的元素,该怎么办? 我们可以为此使用numpy.where()

  1. numpy.where(condition [, x, y])

We have two more parameters x and y. What are those?

我们还有两个参数xy 。 那些是什么?

Basically, what this says is that if condition holds true for some element in our array, the new array will choose elements from x.

基本上,这就是说,如果condition对我们数组中的某个元素成立,则新数组将从x选择元素。

Otherwise, if it’s false, elements from y will be taken.

否则,如果为假,则将使用y元素。

With that, our final output array will be an array with elements from x wherever condition = True, and elements from y whenever condition = False.

这样,我们的最终输出数组将是一个数组,其中condition = True x元素, condition = Falsey元素。

Note that although x and y are optional, if you specify x, you MUST also specify y. This is because, in this case, the output array shape must be the same as the input array.

请注意,尽管xy是可选的,但如果指定x ,则还必须指定y 。 这是因为在这种情况下 ,输出数组的形状必须与输入数组的形状相同。

NOTE: The same logic applies for both single and multi-dimensional arrays too. In both cases, we filter based on the condition. Also remember that the shapes of x, y and condition are broadcasted together.

注意 :相同的逻辑也适用于一维和多维数组。 在这两种情况下,我们都根据条件进行过滤。 还要记住, xycondition的形状是一起广播的。

Now, let us look at some examples, to understand this function properly.

现在,让我们看一些示例,以正确理解此功能。



使用Python numpy.where() (Using Python numpy.where())

Suppose we want to take only positive elements from a numpy array and set all negative elements to 0, let’s write the code using numpy.where().

假设我们只想从numpy数组中获取正元素,并将所有负元素设置为0,让我们使用numpy.where()编写代码。

1.用numpy.where()替换Elements (1. Replace Elements with numpy.where())

We’ll use a 2 dimensional random array here, and only output the positive elements.

我们将在此处使用二维随机数组,仅输出正元素。

  1. import numpy as np
  2. # Random initialization of a (2D array)
  3. a = np.random.randn(2, 3)
  4. print(a)
  5. # b will be all elements of a whenever the condition holds true (i.e only positive elements)
  6. # Otherwise, set it as 0
  7. b = np.where(a > 0, a, 0)
  8. print(b)

Possible Output

可能的输出

  1. [[-1.06455975 0.94589166 -1.94987123]
  2. [-1.72083344 -0.69813711 1.05448464]]
  3. [[0. 0.94589166 0. ]
  4. [0. 0. 1.05448464]]

As you can see, only the positive elements are now retained!

如您所见,现在仅保留了积极元素!

2.仅在有条件的情况下使用numpy.where() (2. Using numpy.where() with only a condition)

There may be some confusion regarding the above code, as some of you may think that the more intuitive way would be to simply write the condition like this:

上面的代码可能有些混乱,因为你们中的一些人可能认为更直观的方法是简单地编写如下条件:

  1. import random
  2. import numpy as np
  3. a = np.random.randn(2, 3)
  4. b = np.where(a > 0)
  5. print(b)

If you now try running the above code, with this change, you’ll get an output like this:

如果现在尝试运行上面的代码,进行此更改,将得到如下输出:

  1. (array([0, 1]), array([2, 1]))

If you observe closely, b is now a tuple of numpy arrays. And each array is the location of a positive element. What does this mean?

如果仔细观察, b现在是一个numpy数组的元组 。 每个数组都是一个正元素的位置。 这是什么意思?

Whenever we provide just a condition, this function is actually equivalent to np.asarray.nonzero().

只要我们提供一个条件,此函数实际上等效于np.asarray.nonzero()

In our example, np.asarray(a > 0) will return a boolean-like array after applying the condition, and np.nonzero(arr_like) will return the indices of the non-zero elements of arr_like. (Refer to this link)

在我们的示例中, np.asarray(a > 0)将在应用条件后返回类似于布尔的数组,而np.nonzero(arr_like)将返回np.nonzero(arr_like)的非零元素的arr_like 。 (请参阅链接)

So, we’ll now look at a simpler example, that shows us how flexible we can be with numpy!

因此,我们现在来看一个更简单的示例,它向我们展示了使用numpy的灵活性。

  1. import numpy as np
  2. a = np.arange(10)
  3. b = np.where(a < 5, a, a * 10)
  4. print(a)
  5. print(b)

Ouptut

乌普图

  1. [0 1 2 3 4 5 6 7 8 9]
  2. [ 0 1 2 3 4 50 60 70 80 90]

Here, the condition is a < 5, which will be the numpy-like array [True True True True True False False False False False], x is the array a, and y is the array a * 10. So, we choose from an only if a < 5, and from a * 10, if a > 5.

这里的条件是a < 5 ,它将是类似numpy的数组[True True True True True False False False False False]x是数组a,而y是数组a *10。因此,我们选择仅当<5时才是,如果*> 5,则要从* 10开始。

So, this transforms all elements >= 5, by multiplication with 10. This is what we get indeed!

因此,这将通过乘以10转换> = 5的所有元素。这确实是我们得到的!



用numpy.where()广播 (Broadcasting with numpy.where())

If we provide all of condition, x, and y arrays, numpy will broadcast them together.

如果我们提供所有conditionxy数组,则numpy会将它们一起广播。

  1. import numpy as np
  2. a = np.arange(12).reshape(3, 4)
  3. b = np.arange(4).reshape(1, 4)
  4. print(a)
  5. print(b)
  6. # Broadcasts (a < 5, a, and b * 10)
  7. # of shape (3, 4), (3, 4) and (1, 4)
  8. c = np.where(a < 5, a, b * 10)
  9. print(c)

Output

输出量

  1. [[ 0 1 2 3]
  2. [ 4 5 6 7]
  3. [ 8 9 10 11]]
  4. [[0 1 2 3]]
  5. [[ 0 1 2 3]
  6. [ 4 10 20 30]
  7. [ 0 10 20 30]]

Again, here, the output is selected based on the condition, so all elements, but here, b is broadcasted to the shape of a. (One of its dimensions has only one element, so there will be no errors during broadcasting)

再次,在这里,基于该条件时所选择的输出,因此,所有元件,但在这里, b被广播到的形状a 。 (其维度之一只有一个元素,因此在广播过程中不会出现错误)

So, b will now become [[0 1 2 3] [0 1 2 3] [0 1 2 3]], and now, we can select elements even from this broadcasted array.

因此, b现在将变为[[0 1 2 3] [0 1 2 3] [0 1 2 3]] ,现在,我们甚至可以从此广播数组中选择元素。

So the shape of the output is the same as the shape of a.

所以输出的形状相同的形状a



结论 (Conclusion)

In this article, we learned about how we can use the Python numpy.where() function to select arrays based on another condition array.

在本文中,我们学习了如何使用Python numpy.where()函数基于另一个条件数组选择数组。



参考资料 (References)



翻译自: https://www.journaldev.com/37898/python-numpy-where

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/685183
推荐阅读
相关标签
  

闽ICP备14008679号