使用NumPy查找数组中的最大值索引

时间:2021-06-07 13:02:28

I would like to find a maximum in a float64 array, excluding nan values.

我想在float64数组中找到一个最大值,不包括nan值。

I saw np.nanmax function 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 ?

是不是有办法直接恢复索引?

3 个解决方案

#1


16  

Numpy has an argmax function 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函数可以返回,尽管你必须手动处理nans。 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)

#2


6  

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.])

#3


3  

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])

#1


16  

Numpy has an argmax function 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函数可以返回,尽管你必须手动处理nans。 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)

#2


6  

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.])

#3


3  

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])