输入数据是一个二维数组(时间戳,值)对,按时间戳排序:
np.array([[50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66],
[ 2, 3, 5, 6, 4, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3]])
我想找到值超过阈值(例如 >=4)的时间窗口。似乎我可以用 bool 条件做阈值部分,并用 np.extract()
映射回时间戳:
>>> a[1] >= 4
array([False, False, True, True, True, False, False, False, False,
True, True, True, False, False, False, False, False])
>>> np.extract(a[1] >= 4, a[0])
array([52, 53, 54, 59, 60, 61])
但是我需要每个窗口的第一个和最后一个时间戳匹配阈值(即。[[52, 54], [59, 61]]
),这是我可以的地方'不太找到正确的方法。
最佳答案
这是一种方法:
# Create a mask
In [42]: mask = (a[1] >= 4)
# find indice of start and end of the threshold
In [43]: ind = np.where(np.diff(mask))[0]
# add 1 to starting indices
In [44]: ind[::2] += 1
# find and reshape the result
In [45]: result = a[0][ind].reshape(-1, 2)
In [46]: result
Out[46]:
array([[52, 54],
[59, 61]])
关于python - 阈值numpy数组,查找窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54089396/