繁体   English   中英

如何从时间序列 dataframe 中识别和提取事件?

[英]How do I identify and extract events from a time-series dataframe?

我有一个时间序列数据集,如下所示:时间序列

在我将数据集导入 Python 中的 pandas dataframe 之后,我想:

  • 将所有高于零的增长识别为单个事件
  • 计算每个事件的曲线下面积
  • 存储区域的值以及事件的开始时间和结束时间

关于我如何处理这个问题的任何指示? 我对无监督学习有一点经验。 但是,我什至从这开始就遇到了麻烦,因为我不确定如何定义条件来识别事件。

下面是一些玩具数据集的示例代码,该数据集使用 pandas shift和比较运算符,如gt (大于)和ne (不等于)来识别峰值的开始。 我在df中创建一个新列,如果当前行是新峰的开始,则为True ,否则为False 我希望尝试这个示例对您有所帮助,否则我建议尝试查找类似的问题,也许使用“峰值查找”之类的关键字。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#Create data like what you've shared
#this is random, but repeatable with the seed
#choose a random value between 0 and 3 and repeat it between 2 and 5 times
#then always go back to 0 for two timepoints
np.random.seed(1)
num_time = 100
values = []

while len(values) < num_time:
    value = np.random.random()*3
    repeat = np.random.randint(2,5)
    values.extend([value]*repeat+[0,0])
    
df = pd.DataFrame({
    'time':range(num_time),
    'value':values[:num_time],
})



#add a new column with the 'peak group' of each value
#the shifting is to identify when the timeseries changes from less-than-threshold to more-than-threshold
threshold = 0.5 #could set this to zero if you'd like
passed_threshold = df['value'].gt(threshold)
df['peak_start'] = passed_threshold.ne(passed_threshold.shift(1)) & passed_threshold

#plotting to show the marked peak starts with orange dots
plt.plot(df.time, df.value)
plt.plot(df.loc[df.peak_start,'time'],df.loc[df.peak_start,'value'],'o')

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM