首次出现大于当前值的值 [英] Numpy first occurrence of value greater than existing value

查看:114
本文介绍了首次出现大于当前值的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在numpy中有一个一维数组,我想找到一个值超过numpy数组中的值的索引位置.

I have a 1D array in numpy and I want to find the position of the index where a value exceeds the value in numpy array.

例如

aa = range(-10,10)

aa中查找位置,超过该位置的值5.

Find position in aa where, the value 5 gets exceeded.

推荐答案

这有点快(看起来更好)

This is a little faster (and looks nicer)

np.argmax(aa>5)

由于 argmax 会在first True(如果多次出现最大值,则返回对应于第一次出现的索引."),并且不保存其他列表.

Since argmax will stop at the first True ("In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence are returned.") and doesn't save another list.

In [2]: N = 10000

In [3]: aa = np.arange(-N,N)

In [4]: timeit np.argmax(aa>N/2)
100000 loops, best of 3: 52.3 us per loop

In [5]: timeit np.where(aa>N/2)[0][0]
10000 loops, best of 3: 141 us per loop

In [6]: timeit np.nonzero(aa>N/2)[0][0]
10000 loops, best of 3: 142 us per loop

这篇关于首次出现大于当前值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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