Numpy:获取二维数组最小值的列和行索引 [英] Numpy: get the column and row index of the minimum value of a 2D array

查看:76
本文介绍了Numpy:获取二维数组最小值的列和行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

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

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

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

谢谢.

感谢您的回答.我测试了它们的速度如下:

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

似乎自定义函数实际上比 unravel_index() 和 where() 更快.unravel_index() 做与自定义函数类似的事情,加上检查额外参数的开销.where() 能够返回多个索引,但对于我的目的来说要慢得多.也许纯 python 代码只做两个简单的算术运算并没有那么慢,而自定义函数方法则是最快的.

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.

推荐答案

您可以使用 np.where:

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

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

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

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

更新:

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

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:

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

如果你真的很关心性能,你可以看看cython.

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

这篇关于Numpy:获取二维数组最小值的列和行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆