在python中计算图形中的尖峰数

尼尔辛拉伯

使用数据集,df我绘制了一个如下所示的图形:

df

Time    Temperature
8:23:04     18.5
8:23:04     19
9:12:57     19
9:12:57     20
9:12:58     20
9:12:58     21
9:12:59     21
9:12:59     23
9:13:00     23
9:13:00     25
9:13:01     25
9:13:01     27
9:13:02     27
9:13:02     28
9:13:03     28

图(整体) 在此处输入图片说明

放大数据时,我们可以看到更多细节:

在此处输入图片说明

我想计算一下这个温度测量设备的激活次数,这会导致温度急剧上升。我已经定义了一个激活如下:

设 T0, T1, T2, T3 为时间 t=0,t=1,t=2,t=3 时的温度,d0= T1-T0, d1= T2-T1, d2= T3-T2, ...是 2 个相邻值的差值。

如果

1) d0 ≥ 0 且 d1 ≥ 0 且 d2 ≥ 0,并且

2) T2-T0 > max(d0, d1, d2),和

3) T2-T0 < 30 秒

It is considered as an activation. I want to count how many activations are there in total. What's a good way to do this?

Thanks.

Michael

There could be a number of different, valid answers depending on how a spike is defined.

Assuming you just want the indices where the temperature increases significantly. One simple method is to just look for very large jumps in value, above some threshold value. The threshold can be calculated from the mean difference of the data, which should give a rough approximation of where the significant variations in value occur. Here's a basic implementation:

import numpy as np

# Data
x = np.array([0, 1, 2, 50, 51, 52, 53, 100, 99, 98, 97, 96, 10, 9, 8, 80])

# Data diff
xdiff = x[1:] - x[0:-1]

# Find mean change
xdiff_mean = np.abs(xdiff).mean()

# Identify all indices greater than the mean
spikes = xdiff > abs(xdiff_mean)+1
print(x[1:][spikes])  # prints 50, 100, 80
print(np.where(spikes)[0]+1)  # prints 3, 7, 15

您还可以查看使用异常值拒绝,这比与均值差异的基本比较要聪明得多。关于如何做到这一点有很多答案:scipy.stats 能否识别和掩盖明显的异常值?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章