python - 阈值numpy数组,查找窗口

标签 python arrays numpy

输入数据是一个二维数组(时间戳,值)对,按时间戳排序:

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/

相关文章:

python - 计时器线程 - AttributeError '...' 没有属性 '...'

java - 如何通过字符串打印出数组中的名称列表?

c# - 如何在 C# 中将文本值拆分为包含单词的数组?

python - SKLearn 交叉验证 : How to pass info on fold examples to my scorer function?

python - 将字符串数组(类别)从 Pandas 数据帧转换为整数数组

python - numpy.float64 类型和 is_integer() 问题

python - 选择单词出现后的子字符串

python - Python 3 中的行内变量

python - 如何设置 Celery 以通过 ssl 与 Azure Redis 实例对话

具有数组返回类型的 Java ImageWorker 发布/处理方法