首页 > 解决方案 > 不均匀时间序列中的峰值检测

问题描述

我正在使用一个包含措施的数据集,并结合了以下内容datetime

datetime value
2017-01-01 00:01:00,32.7
2017-01-01 00:03:00,37.8
2017-01-01 00:04:05,35.0
2017-01-01 00:05:37,101.1
2017-01-01 00:07:00,39.1
2017-01-01 00:09:00,38.9

我正在尝试检测并删除可能出现的潜在峰值,例如2017-01-01 00:05:37,101.1测量值。

到目前为止我发现的一些东西:

我到处搜索,但什么也找不到。实现将在 Python 中,但我愿意挖掘其他语言以获取逻辑。

标签: pythonpandasalgorithmtime-serieslanguage-agnostic

解决方案


我已在github上将此代码发布给将来遇到此问题或类似问题的任何人。

经过大量的试验和错误,我认为我创造了一些有效的东西。使用@user58697 告诉我的内容,我设法创建了一个代码来检测阈值之间的每个峰值。

通过使用他/她解释的逻辑,if ((flow[i+1] - flow[i]) / (time[i+1] - time[i]) > threshold我编写了以下代码:

首先读取.csv并解析日期,然后拆分为两个 numpy 数组:

dataset = pd.read_csv('https://raw.githubusercontent.com/MigasTigas/peak_removal/master/dataset_simple_example.csv', parse_dates=['date'])

dataset = dataset.sort_values(by=['date']).reset_index(drop=True).to_numpy()  # Sort and convert to numpy array

# Split into 2 arrays
values = [float(i[1]) for i in dataset]  # Flow values, in float
values = np.array(values)

dates = [i[0].to_pydatetime() for i in dataset]
dates = np.array(dates)

然后将 应用于(flow[i+1] - flow[i]) / (time[i+1] - time[i])整个数据集:

flow = np.diff(values)
time = np.diff(dates).tolist()
time = np.divide(time, np.power(10, 9))

slopes = np.divide(flow, time) # (flow[i+1] - flow[i]) / (time[i+1] - time[i])
slopes = np.insert(slopes, 0, 0, axis=0) # Since we "lose" the first index, this one is 0, just for alignments

x最后,为了检测峰值,我们将数据减少到每个数秒的滚动窗口。这样我们就可以很容易地检测到它们:

# ROLLING WINDOW
size = len(dataset)
rolling_window = []
rolling_window_indexes = []
RW = []
RWi = []
window_size = 240  # Seconds

dates = [i.to_pydatetime() for i in dataset['date']]
dates = np.array(dates)

# create the rollings windows
for line in range(size):
    limit_stamp = dates[line] + datetime.timedelta(seconds=window_size)
    for subline in range(line, size, 1):
        if dates[subline] <= limit_stamp:

            rolling_window.append(slopes[subline])  # Values of the slopes
            rolling_window_indexes.append(subline)  # Indexes of the respective values

        else:

            RW.append(rolling_window)
            if line != size: # To prevent clearing the last rolling window
                rolling_window = []

            RWi.append(rolling_window_indexes)
            if line != size:
                rolling_window_indexes = []

            break
else:
    # To get the last rolling window since it breaks before append
    RW.append(rolling_window)
    RWi.append(rolling_window_indexes)

获得所有滚动窗口后,我们开始有趣:

t = 0.3  # Threshold
peaks = []

for index, rollWin in enumerate(RW):
    if rollWin[0] > t: # If the first value is greater of threshold
        top = rollWin[0] # Sets as a possible peak
        bottom = np.min(rollWin) # Finds the minimum of the peak

        if bottom < -t: # If less than the negative threshold
            bottomIndex = int(np.argmin(rollWin)) # Find it's index

            for peak in range(0, bottomIndex, 1): # Appends all points between the first index of the rolling window until the bottomIndex
                peaks.append(RWi[index][peak]) 

这段代码背后的想法是每个峰值都有一个上升和下降,如果两者都大于规定的阈值,那么它是一个异常峰值以及它们之间的所有峰值:

在此处输入图像描述

在此处输入图像描述

翻译成使用的真实数据集的地方,发布在github 上在此处输入图像描述 在此处输入图像描述


推荐阅读