检查数组中是否有3个连续的值超过某个阈值 [英] Check if there are 3 consecutive values in an array which are above some threshold
问题描述
说我有一个像这样的np.array:
Say I have a np.array like this:
a = [1, 3, 4, 5, 60, 43, 53, 4, 46, 54, 56, 78]
是否有一种快速的方法来获取3个连续数字都超过某个阈值的所有位置的索引?也就是说,对于某个阈值th
,获取所有满足条件的x
:
Is there a quick method to get the indices of all locations where 3 consecutive numbers are all above some threshold? That is, for some threshold th
, get all x
where this holds:
a[x]>th and a[x+1]>th and a[x+2]>th
示例:对于阈值40和上面给出的列表,x应该为[4,8,9]
.
Example: for threshold 40 and the list given above, x should be [4,8,9]
.
非常感谢.
推荐答案
方法1
在比较后获得的布尔数组的掩码上使用convolution
-
Use convolution
on the mask of boolean array obtained after comparison -
In [40]: a # input array
Out[40]: array([ 1, 3, 4, 5, 60, 43, 53, 4, 46, 54, 56, 78])
In [42]: N = 3 # compare N consecutive numbers
In [44]: T = 40 # threshold for comparison
In [45]: np.flatnonzero(np.convolve(a>T, np.ones(N, dtype=int),'valid')>=N)
Out[45]: array([4, 8, 9])
方法2
使用binary_erosion
-
In [77]: from scipy.ndimage.morphology import binary_erosion
In [31]: np.flatnonzero(binary_erosion(a>T,np.ones(N, dtype=int), origin=-(N//2)))
Out[31]: array([4, 8, 9])
方法3(特定情况):少量连续数字检查
要检查少量的连续数字(在这种情况下为3),我们还可以在比较的蒙版上slicing
以获得更好的性能-
For checking such a small number of consecutive numbers (three in this case), we can also slicing
on the compared mask for better performance -
m = a>T
out = np.flatnonzero(m[:-2] & m[1:-1] & m[2:])
基准化
给定样本的100000
重复/平铺数组上的时间-
Benchmarking
Timings on 100000
repeated/tiled array from given sample -
In [78]: a
Out[78]: array([ 1, 3, 4, 5, 60, 43, 53, 4, 46, 54, 56, 78])
In [79]: a = np.tile(a,100000)
In [80]: N = 3
In [81]: T = 40
# Approach #3
In [82]: %%timeit
...: m = a>T
...: out = np.flatnonzero(m[:-2] & m[1:-1] & m[2:])
1000 loops, best of 3: 1.83 ms per loop
# Approach #1
In [83]: %timeit np.flatnonzero(np.convolve(a>T, np.ones(N, dtype=int),'valid')>=N)
100 loops, best of 3: 10.9 ms per loop
# Approach #2
In [84]: %timeit np.flatnonzero(binary_erosion(a>T,np.ones(N, dtype=int), origin=-(N//2)))
100 loops, best of 3: 11.7 ms per loop
这篇关于检查数组中是否有3个连续的值超过某个阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!