Numpy:获取2D数组最小值的列和行索引

时间:2023-01-31 21:29:53

For example,

例如,

x = array([[1,2,3],[3,2,5],[9,0,2]])
some_func(x) gives (2,1)

I know one can do it by a custom function:

我知道可以通过自定义函数来实现:

def find_min_idx(x):
    k = x.argmin()
    ncol = x.shape[1]
    return k/ncol, k%ncol

However, I am wondering if there's a numpy built-in function that does this faster.

但是,我想知道是否有一个numpy内置函数可以更快地执行此操作。

Thanks.

谢谢。

EDIT: thanks for the answers. I tested their speeds as follows:

编辑:谢谢你的答案。我测试了它们的速度如下:

%timeit np.unravel_index(x.argmin(), x.shape)
#100000 loops, best of 3: 4.67 µs per loop

%timeit np.where(x==x.min())
#100000 loops, best of 3: 12.7 µs per loop

%timeit find_min_idx(x) # this is using the custom function above
#100000 loops, best of 3: 2.44 µs per loop

Seems the custom function is actually faster than unravel_index() and where(). unravel_index() does similar things as the custom function plus the overhead of checking extra arguments. where() is capable of returning multiple indices but is significantly slower for my purpose. Perhaps pure python code is not that slow for doing just two simple arithmetic and the custom function approach is as fast as one can get.

似乎自定义函数实际上比unravel_index()和where()更快。 unravel_index()执行与自定义函数类似的操作以及检查额外参数的开销。 where()能够返回多个索引,但对于我的目的而言要慢得多。也许纯粹的python代码并不是那么简单,只做两个简单的算术,自定义函数方法就像人们可以获得的那样快。

2 个解决方案

#1


9  

You may use np.where:

你可以使用np.where:

In [9]: np.where(x == np.min(x))
Out[9]: (array([2]), array([1]))

Also as @senderle mentioned in comment, to get values in an array, you can use np.argwhere:

另外,正如@senderle在评论中提到的,要获取数组中的值,您可以使用np.argwhere:

In [21]: np.argwhere(x == np.min(x))
Out[21]: array([[2, 1]])

Updated:

As OP's times show, and much clearer that argmin is desired (no duplicated mins etc.), one way I think may slightly improve OP's original approach is to use divmod:

正如OP的时代所示,并且更加清楚argmin是否需要(没有重复的分钟等),我认为可能略微改进OP的原始方法的一种方法是使用divmod:

divmod(x.argmin(), x.shape[1])

Timed them and you will find that extra bits of speed, not much but still an improvement.

定时他们,你会发现额外的速度,不多但仍然是一个改进。

%timeit find_min_idx(x)
1000000 loops, best of 3: 1.1 µs per loop

%timeit divmod(x.argmin(), x.shape[1])
1000000 loops, best of 3: 1.04 µs per loop

If you are really concerned about performance, you may take a look at cython.

如果你真的关心性能,你可以看一下cython。

#2


6  

You can use np.unravel_index

您可以使用np.unravel_index

print(np.unravel_index(x.argmin(), x.shape))
(2, 1)

#1


9  

You may use np.where:

你可以使用np.where:

In [9]: np.where(x == np.min(x))
Out[9]: (array([2]), array([1]))

Also as @senderle mentioned in comment, to get values in an array, you can use np.argwhere:

另外,正如@senderle在评论中提到的,要获取数组中的值,您可以使用np.argwhere:

In [21]: np.argwhere(x == np.min(x))
Out[21]: array([[2, 1]])

Updated:

As OP's times show, and much clearer that argmin is desired (no duplicated mins etc.), one way I think may slightly improve OP's original approach is to use divmod:

正如OP的时代所示,并且更加清楚argmin是否需要(没有重复的分钟等),我认为可能略微改进OP的原始方法的一种方法是使用divmod:

divmod(x.argmin(), x.shape[1])

Timed them and you will find that extra bits of speed, not much but still an improvement.

定时他们,你会发现额外的速度,不多但仍然是一个改进。

%timeit find_min_idx(x)
1000000 loops, best of 3: 1.1 µs per loop

%timeit divmod(x.argmin(), x.shape[1])
1000000 loops, best of 3: 1.04 µs per loop

If you are really concerned about performance, you may take a look at cython.

如果你真的关心性能,你可以看一下cython。

#2


6  

You can use np.unravel_index

您可以使用np.unravel_index

print(np.unravel_index(x.argmin(), x.shape))
(2, 1)