python中有检测曲线奇异点的函数吗?

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

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

谢谢

Singular point detection

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

  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,以确保与原始数据相同的形状。

接下来,我们将找到峰:

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 包,因为它在几乎所有情况下都能很好地工作。您会看到并未找到示例中指示的所有点。您可以尝试使用 thresholdminimum distance 的不同参数来找到更好的解决方案。但这应该是进一步研究的良好起点。事实上,minimum distance 参数会给你想要的 滑动 window.