Python的numpy.where(condition, x, y)
函数返回一个数组,其中的元素来自x
,其中condition
是True
,其他地方的元素来自y
。
当简单地调用 numpy.where(condition)
时,它是np.asarray(condition).nonzero()
的缩写,并返回一个元组,其中包含符合每个维度条件的元素的索引。
如果你觉得听起来不错,请继续阅读,你将通过Python代码片段和生动的视觉效果充分理解numpy.where()
函数。
本教程是关于numpy.where()
函数的。
- 具体来说,我将介绍它的语法和参数。
- 然后,你将学习这个函数的一些基本例子。
- 最后,我将解决关于
numpy.where()
的三个首要问题,包括numpy.where
2d array,numpy.where
multiple conditions, 和numpy.where
returns tuple。
语法和参数
下面是numpy.where()
的语法。
# Syntax
numpy.where(condition, [x, y, ]/)
下面是numpy.where()
的参数表。
参数 | 接受 | 描述 |
condition | array_like ,bool | 如果True ,则产生x ,否则产生y 。 |
x, y | array_like ; 可选 | 可以从中选择的值。 |
numpy.where()
函数的输出取决于你传递的参数。请看下面的基本例子,以便有更好的理解!
基本例子1
好的,让我们重新审视一下前面显示的两个基本例子。
Python 的numpy.where(condition, x, y)
返回一个数组,其中的元素来自x
,而condition
是True
,其他地方的元素来自y
。
代码从导入numpy as np
开始,创建两个ndarray
对象x
和y
,并使用条件x < 3
来调用numpy.where()
方法。
然后,numpy.where(condition, x, y)
在上面所示的罩子下完成所有的输出数组构造。
最后,我们打印出输出数组,并发现我们对numpy.where
有了更多的了解!
import numpy as np
# basic example 1
x = np.arange(1, 4)
y = np.zeros(3)
print(np.where(x < 3, x, y))
输出
基本例子2
接下来,当简单地调用 numpy.where(condition)
时,它是np.asarray(condition).nonzero()
的缩写,并返回一个包含符合各维度条件的元素索引的元组。
代码从导入NumPy作为np
开始,创建一个ndarray
对象x
,并使用条件x < 3
来调用numpy.where()
方法。
然后,numpy.where(condition)
在上面显示的引擎盖下完成所有的输出元组构造。
最后,我们打印出输出元组,发现自己对numpy.where
有了更多的了解!
import numpy as np
# basic example 2
x = np.arange(1, 4)
print(np.where(x < 3))
输出
np.where() 2d数组
在这一部分,让我们用numpy.where()
来处理2d数组。
为了恢复你的记忆,如果调用numpy.where(condition, x, y)
,我们可以得到一个新的ndarray
,其元素来自x
或y
,这取决于条件。
代码的开始是导入numpy作为np
,并创建两个ndarray
对象x
和y
(都是零)。
然后,如果x
中的元素不符合我们的条件x > 6
,我们将用y
中的相应元素替换x
中的元素,在我们的例子中是零。
最后,我们打印出输出数组。
import numpy as np
x = np.arange(1, 13).reshape(3, 4)
y = np.zeros(12).reshape(3, 4)
# x:
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
# y:
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
new_array = np.where(x > 6, x, y)
print(new_array)
# same as:
# new_array = np.where(x > 6, x, 0)
输出。
np.where() 多个条件
你可以将多个条件传递给numpy.where()
,在AND逻辑中使用&
,在 OR 逻辑中使用**|
** 。例如,如果x
中的元素不符合我们的多重条件,我们将用y
中的相应元素替换x
中的元素。
代码从导入numpy作为np
开始,创建了两个ndarray
对象x
和y
(都是零),并使用运算符 &
和 **|
**分别创建两个多重条件。
然后,如果x
中的元素不符合我们的多重条件,我们将用y
中的相应元素替换x
中的元素,在我们的例子中,这个元素是零。
最后,我们打印出输出数组。
import numpy as np
x = np.arange(12).reshape(3, 4)
y = np.zeros(12).reshape(3, 4)
# x:
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# just to focus on the multiple conditions first!
conditions1 = (x > 3) & (x < 8)
conditions2 = (x < 3) | (x > 8)
# conditions are bool ndarray
print(f'condition 1:\n {conditions1}\n')
print(f'condition 2:\n {conditions2}\n')
# use the np.where() function
print(f'np.where(conditions1, x, y):\n {np.where(conditions1, x, y)}\n')
print(f'np.where(conditions2, x, y):\n {np.where(conditions2, x, y)}')
输出。
np.where() 返回 tuple
到目前为止,我们已经相当熟悉调用模式,numpy.where(condition, x, y)
。
那么,用返回一个元组的(笨拙的)方式代替呢?是的,我说的是使用numpy.where(condition)
。
之前,我提到,当简单地调用 numpy.where(condition)
时,它是np.asarray(condition).nonzero()
的缩写,并返回一个元组,其中包含符合各维条件的元素的索引。
让我们想象一下,我们有一个2D数组(两个维度!),想要得到满足某些条件的元素的行和列的索引。我们可以使用numpy.where(condition)
来获得符合我们条件的元素的索引,并进一步使用我们的老友 [zip()](https://blog.finxter.com/python-ziiiiiiip-a-helpful-guide/)
和 [list()](https://blog.finxter.com/python-list/)
来获得元素的坐标!
代码开始时,将numpy导入为np
,并创建一个ndarray
对象x
。
然后,我们调用numpy.where(condition)
,以获得每个维度上满足条件的元素的索引(在矩阵视图中的行和列;在直角坐标视图中的x轴和y轴)。
最后,我们使用 [zip()](https://blog.finxter.com/python-ziiiiiiip-a-helpful-guide/)
和 [list()](https://blog.finxter.com/python-list/)
函数来获得符合我们条件的坐标!
import numpy as np
x = np.arange(12).reshape(3, 4)
# x:
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# use the np.where() function to get the indices
a_tuple = np.where(x)
print(type(a_tuple))
print('-'*85)
print("Indices of elements that meets the condition for each dimension:")
print(np.where(x))
# use the zip() and list() function to get the coordinates
print('-'*85)
print("Coordinates (row, column) that meets the condition for each dimension:")
print(list(zip(*np.where(x))))
输出。
总结
我们的文章np.where()
,到此为止。
我们了解了它的语法、参数和基本例子。
我们还研究了关于np.where()
函数的前三个问题,包括np.where()
2d数组,np.where()
多重条件,以及np.where()
返回元组。