Python 使用 NumPy 在数组中查找最大值的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21989513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding index of maximum value in array with NumPy
提问by froggy
I would like to find a maximum in a float64array, excluding nanvalues.
我想在float64数组中找到最大值,不包括nan值。
I saw np.nanmaxfunction but it doesn't give the index corresponding to the found value.
我看到了np.nanmax函数,但它没有给出与找到的值相对应的索引。
it 's quite strange to scan after to the value specially the function necessarily use the index ??? Can't it be a mistake searching like that .
扫描到值之后很奇怪,特别是函数必须使用索引???像这样搜索会不会是错误的。
isn't there a way to recover the index directly ?
没有办法直接恢复索引吗?
采纳答案by Jaime
Numpy has an argmaxfunction that returns just that, although you will have to deal with the nans manually. nans always get sorted to the end of an array, so with that in mind you can do:
Numpy 有一个argmax函数可以返回该值,尽管您必须nan手动处理s。nans 总是排序到数组的末尾,所以考虑到这一点,你可以这样做:
a = np.random.rand(10000)
a[np.random.randint(10000, size=(10,))] = np.nan
a = a.reshape(100, 100)
def nanargmax(a):
idx = np.argmax(a, axis=None)
multi_idx = np.unravel_index(idx, a.shape)
if np.isnan(a[multi_idx]):
nan_count = np.sum(np.isnan(a))
# In numpy < 1.8 use idx = np.argsort(a, axis=None)[-nan_count-1]
idx = np.argpartition(a, -nan_count-1, axis=None)[-nan_count-1]
multi_idx = np.unravel_index(idx, a.shape)
return multi_idx
>>> nanargmax(a)
(20, 93)
回答by Oz123
You should use np.where
你应该使用 np.where
In [17]: a=np.random.uniform(0, 10, size=10)
In [18]: a
Out[18]:
array([ 1.43249468, 4.93950873, 7.22094395, 1.20248629, 4.66783985,
6.17578054, 4.6542771 , 7.09244492, 7.58580515, 5.72501954])
In [20]: np.where(a==a.max())
Out[20]: (array([8]),)
This also works for 2 arrays, the returned value, is the index. Here we create a range from 1 to 9:
这也适用于 2 个数组,返回值是索引。这里我们创建一个从 1 到 9 的范围:
x = np.arange(9.).reshape(3, 3)
This returns the index, of the the items that equal 5:
这将返回等于 5 的项目的索引:
In [34]: np.where(x == 5)
Out[34]: (array([1]), array([2])) # the first one is the row index, the second is the column
You can use this value directly to slice your array:
您可以直接使用此值对数组进行切片:
In [35]: x[np.where(x == 5)]
Out[35]: array([ 5.])
回答by rysqui
You want to use numpy.nanargmax
你想使用numpy.nanargmax
The documentation provides some clear examples.
该文档提供了一些清晰的示例。
a = np.array([[np.nan, 4], [2, 3]])
print np.argmax(a)
0
print np.nanargmax(a)
1
np.nanargmax(a, axis=0)
array([1, 0])
np.nanargmax(a, axis=1)
array([1, 1])

