繁体   English   中英

python中是否有功能来检测曲线上的奇异点?

[英]Is there a function in python to detect singular points on a curve?

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

谢谢

奇异点检测

我只是抓取了一些数据,以显示可以在整个数据集上找到点,而无需使用滑动窗口(但理论上可以):

  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()

在这里,我还进行插值,以使数据点之间的间距相等。 此外,我插入一个0在位置0使用np.insert功能的分化后,为确保相同的形状的原始数据。

原始,一阶导数和二阶导数的比较

接下来,我们将找到这些峰:

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')

数据峰值

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

暂无
暂无

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

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