python - python中是否有检测曲线上奇异点的函数?

标签 python sliding-window

我需要检测从数据集绘制的给定曲线上的奇异点(极值、趋势变化、急剧变化)。首先要想到的是带推导的拐点检测(但我没有绘制曲线的数学表达式),其次是如何检测角点。所以如果可能的话我可以构建(使用 python)一个滑动窗口来检测这些类型的 SP(奇异点),如果可能的话使用的库和函数是什么?

谢谢

Singular point detection

最佳答案

我只是scraped some of your data向您展示您可以在整个数据集上找到点,而无需使用滑动窗口(但理论上您可以):

  1. 局部极值(在原始数据中找到峰值)
  2. 最大陡度(在一阶导数中找到峰值)
  3. 拐点(找到二阶导数的峰值)

首先,让我们看一下导数的计算:

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

df = pd.read_csv("Default Dataset.csv", 
                 sep=';', 
                 decimal=",", 
                 header=None)

### Interpolate linearily ###
x_new = np.linspace(0, df[0].iloc[-1], 2000)
y_new = np.interp(x_new, df[0], df[1])

### First and second derivative ###
diff1 = np.insert(np.diff(y_new), 0, 0)
diff2 = np.insert(np.diff(diff1), 0, 0)

### Plot everything ###
plt.figure(figsize=(12,3))
plt.subplot(131)
plt.plot(x_new, y_new)
plt.subplot(132)
plt.plot(x_new, diff1)
plt.subplot(133)
plt.plot(x_new, diff2)
plt.tight_layout()

在这里,我还插值以在数据点之间具有相等的间距。 此外,我在微分后使用np.insert 函数在0 位置插入一个0,以确保与原始数据的形状相同。

Comparison of raw, 1st deriv and 2nd deriv

接下来,我们将找到峰值:

import peakutils as pu

ix_abs   = pu.indexes(y_new, thres=0.5, min_dist=15)
ix_diff1 = pu.indexes(diff1, thres=0.5, min_dist=15)
ix_diff2 = pu.indexes(diff2, thres=0.5, min_dist=15)

plt.scatter(x_new[ix_abs], y_new[ix_abs], color='g', label='abs')
plt.scatter(x_new[ix_diff1], y_new[ix_diff1], color='r', label='first deriv')
plt.scatter(x_new[ix_diff2], y_new[ix_diff2], color='purple', label='second deriv')

plt.plot(x_new, y_new)
plt.legend(loc='best')

peaks in data

我正在使用 peakutils包,因为它几乎在所有情况下都能很好地工作。您会看到并未找到示例中指示的所有点。您可以为 thresholdminimum distance 使用不同的参数来找到更好的解决方案。但这应该是进一步研究的良好起点。实际上,最小距离 参数将为您提供所需的滑动窗口

关于python - python中是否有检测曲线上奇异点的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55372120/

相关文章:

matlab - 如何建立数据流挖掘的滑动窗口模型?

python - 获取字符串中字符串的特定值

python - 如何将数据帧行分组到pandas groupby列表中

matlab - 检测到的 Windows MATLAB 上的非最大抑制

sql - 用于 28 天滑动窗口聚合的 BigQuery SQL(无需编写 28 行 SQL)

python - 在 Python 中快速对图像运行滑动窗口方法的技巧

r - R 中滑动窗口数据框中最常见的值

python - 更新挂起代码——Python MySQL Connector

python - 使用 winpdb 调试远程脚本

python - unauthorized_client : Client is unauthorized to retrieve access tokens using this method