输入数据是2D数组(时间戳,值)对,按时间戳排序:
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)的时间窗口。似乎我可以使用布尔条件来处理阈值部分,并使用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]]
)匹配的每个窗口的第一个和最后一个时间戳,这是我无法找到正确方法的地方。
答案 0 :(得分:6)
这是一种方法:
# 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]])
答案 1 :(得分:1)
拥有array([52, 53, 54, 59, 60, 61])
后,您可以按照以下方式使用numpy.split
a = np.array([52,53,54,59,60,61])
b = list(a)
indices = [inx for inx,j in enumerate([i[1]-i[0] for i in zip(b,b[1:])]) if j>1]
suba = np.split(a,indices)
print(suba) #prints [array([52, 53]), array([54, 59, 60, 61])]
请注意,您应该将起点作为numpy.split
的第二个参数输入-在此示例中,索引为[2]
(具有一个值的列表)