
Python的numpy模块提供了一个基于条件选择元素的函数。要在Python中处理一个数组,可以使用Numpy库。不幸的是,它并不默认与Python一起出现,你需要先安装它,然后在Python文件的头部导入它以使用其方法。
np.where
np.where是一个numpy 库 方法,它返回输入数组中满足给定条件的元素的索引。 numpy.where() 函数在一个bool数组上进行迭代,对于每一个True,它产生元素数组x;对于每一个False,它产生数组y中的相应项目。
因此,它返回一个由条件为True 的x元素和其他地方的y元素组成的数组。要在Numpy数组中找到一个索引,请使用np.where函数。
语法
numpy.where(condition[, x, y])
参数
**condition。**一个条件表达式,返回Numpy数组的bool
**x, y。**数组(可选,即要么两个都通过,要么不通过)
- 如果所有参数 -> condition, x & y都在numpy.where()中给出,它将根据条件产生的bool数组中的值返回从x & y中选择的项目。所有3个数组必须是相同大小的。
- 如果没有传递x和y参数,而只传递了条件参数,那么它将返回一个数组的元组(每个轴一个),其中包含条件返回的bool数组中True项目的索引。
例子
请看下面的代码。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
result = np.where(arr == 19)
print('Tuple of arrays returned : ', result)
print("Elements with value 19 exists at following indices", result[0], sep='\n')
输出
python3 app.py
Tuple of arrays returned : (array([8]),)
Elements with value 19 exists at following indices
[8]
找出一维Numpy数组中一个值的索引
让我们来找一下数值为19的numpy数组元素出现在不同的地方。但是,首先,让我们看看它的所有索引。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([11, 19, 13, 14, 15, 11, 19, 21, 19, 20, 21])
result = np.where(arr == 19)
print('Tuple of arrays returned : ', result)
print("Elements with value 19 exists at following indices", result[0], sep='\n')
输出
python3 app.py
Tuple of arrays returned : (array([1, 6, 8]),)
Elements with value 19 exists at following indices
[1 6 8]
结果是一个数组的元组(每个轴一个),包含数组中值19存在的索引。
获取值为19的元素的第一个索引。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([11, 19, 13, 14, 15, 11, 19, 21, 19, 20, 21])
result = np.where(arr == 19)
print('Tuple of arrays returned: ', result)
print("Elements with value 19 first exists at index:", result[0][0])
输出
python3 app.py
Tuple of arrays returned: (array([1, 6, 8]),)
Elements with value 19 first exists at index: 1
Python numpy.where(),NumPy数组ndarray中满足条件的元素可以被替换或进行指定处理。
如果元素在numpy数组中没有找到怎么办
如果给定的项目在numpy数组中不存在,那么返回的索引数组将是空的。
请看下面的代码。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([11, 19, 13, 14, 15, 11, 19, 21, 19, 20, 21])
result = np.where(arr == 46)
print('Tuple of arrays returned: ', result)
print("Elements with value 19 first exists at index:", result[0][0])
输出
python3 app.py
Tuple of arrays returned: (array([], dtype=int64),)
Traceback (most recent call last):
File "app.py", line 9, in <module>
print("Elements with value 19 first exists at index:", result[0][0])
IndexError: index 0 is out of bounds for axis 0 with size 0
在Python中查找二维Numpy数组中的一个值的索引
让我们来创建一个二维Numpy数组。请看下面的代码示例。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([[11, 19, 18], [14, 15, 11], [19, 21, 46], [29, 21, 19]])
result = np.where(arr == 19)
print('Tuple of arrays returned: ', result)
print("Elements with value 19 first exists at index:", result[0][0])
輸出
python3 app.py
Tuple of arrays returned: (array([0, 2, 3]), array([1, 0, 2]))
Elements with value 19 first exists at index: 0
它返回数组的元组,每个维度都有一个。就像我们的例子,这是一个二维数组,所以**numpy.where()**将返回两个数组的元组。
两个数组的长度将是相同的。所以为了得到一个确切的索引列表,我们可以对这些数组进行压缩。
请看下面的代码。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([[11, 19, 18], [14, 15, 11], [19, 21, 46], [29, 21, 19]])
result = np.where(arr == 19)
listOfIndices= list(zip(result[0], result[1]))
for indice in listOfIndices:
print(indice)
输出
python3 app.py
(0, 1)
(2, 0)
(3, 2)
如何在Numpy中根据多个条件获得元素的索引
当也可以向numpy.where()函数传递多个条件。
例如,获得数值小于21和大于15的元素的索引。
# app.py
import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([11, 19, 18, 14, 15, 11, 19, 21, 46, 29, 21, 19])
result = np.where((arr > 15) & (arr < 21))
print(result)
输出
python3 app.py
(array([ 1, 2, 6, 11]),)
在上面的例子中,它将返回小于21和大于14的元素值。
总结
如果你想在Python numpy数组中找到值的索引,可以使用numpy.where()。