我有一个类似于 x 和 y 值的指数衰减图,并在下图中标记了转折点。我的问题是,无论有没有噪音,我如何使用Python检测转折点的区域或中间点?这些值位于 link 中给出的 csv 文件中.
更新:这是更清晰的图表,显示了转折点/变化点。
最佳答案
解决此问题的一个简单方法是使用 np.gradient
和阈值。
import numpy as np
from matplotlib import pyplot as plt
# generate some toy data
n = 600
t = np.linspace(0, 600, n)
y = (300 * np.exp(-0.1 * t) + 1) + 20 * (np.random.random(n))
# get the gradient
dy = np.gradient(y)
# search gradient for first occurrence of thresh value:
thresh = 0.01
idx_thresh = np.argmax(dy > thresh)
# y[idx_thresh] would be the "turning point"
# visualization
plt.plot(t, y, 'b', label='y')
plt.plot(t, dy, 'g', label='dy')
plt.plot(t[idx_thresh:], y[idx_thresh:], 'r', label=f'y[dy > {thresh}]')
plt.legend()
但是,找到阈值的位置很大程度上取决于噪声!因此,您可能需要进行一些平滑处理,例如
from scipy.signal import savgol_filter
y_filtered = savgol_filter(y, 11, 3)
dy_f = np.gradient(y_filtered)
idx_thresh = np.argmax(dy_f > thresh)
plt.plot(t, y_filtered, 'k', label='y_filtered')
plt.plot(t, dy_f, 'g', label='dy_f')
plt.plot(t[idx_thresh:], y[idx_thresh:], 'r', label=f'y[dy > {thresh}]')
plt.legend()
请注意,渐变现在更加平滑。 重要提示:这取决于哪个过滤器适合的输入数据!
关于python - 如何使用 Python 检测图形的转折点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856376/