gpt4 book ai didi

python - numpy数组: First occurence of N consecutive values smaller than threshold

转载 作者:行者123 更新时间:2023-12-04 11:18:17 25 4
gpt4 key购买 nike

我有一个一维numpy数组-例如,

a = np.array([1, 4, 5, 7, 1, 2, 2, 4, 10])

我想获得N个后续值都低于某个值x的第一个数字的索引。

在这种情况下,对于 N=3x=3,我将搜索第一个数字,该数字后面的三个条目都小于3。这将是 a[4]

可以简单地通过 for循环遍历所有值来轻松地实现这一点,但是我想知道是否有更干净,更有效的方法来实现此目的。

最佳答案

方法#1:

这是向量化NumPy的方式-

def start_valid_island(a, thresh, window_size):
m = a<thresh
me = np.r_[False,m,False]
idx = np.flatnonzero(me[:-1]!=me[1:])
lens = idx[1::2]-idx[::2]
return idx[::2][(lens >= window_size).argmax()]

sample 运行-
In [44]: a
Out[44]: array([ 1, 4, 5, 7, 1, 2, 2, 4, 10])

In [45]: start_valid_island(a, thresh=3, window_size=3)
Out[45]: 4

In [46]: a[:3] = 1

In [47]: start_valid_island(a, thresh=3, window_size=3)
Out[47]: 0

方法2:

SciPy's binary-erosion -
from scipy.ndimage.morphology import binary_erosion

def start_valid_island_v2(a, thresh, window_size):
m = a<thresh
k = np.ones(window_size,dtype=bool)
return binary_erosion(m,k,origin=-(window_size//2)).argmax()

方法#3:

要完成设置,这是一个基于短篇幅并使用 numba的效率的循环式-
from numba import njit

@njit
def start_valid_island_v3(a, thresh, window_size):
n = len(a)
out = None
for i in range(n-window_size+1):
found = True
for j in range(window_size):
if a[i+j]>=thresh:
found = False
break
if found:
out = i
break
return out

时间-
In [142]: np.random.seed(0)
...: a = np.random.randint(0,10,(100000000))

In [145]: %timeit start_valid_island(a, thresh=3, window_size=3)
1 loop, best of 3: 810 ms per loop

In [146]: %timeit start_valid_island_v2(a, thresh=3, window_size=3)
1 loop, best of 3: 1.27 s per loop

In [147]: %timeit start_valid_island_v3(a, thresh=3, window_size=3)
1000000 loops, best of 3: 608 ns per loop

关于python - numpy数组: First occurence of N consecutive values smaller than threshold,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com