gpt4 book ai didi

python - 如何使用 Python 检测图形的转折点

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:08 26 4
gpt4 key购买 nike

我有一个 x 和 y 值的指数衰减图,下图中标记了一个转折点。我的问题是,无论有没有噪音,我如何使用 Python 检测转折点的区域或中间点?这些值位于此 link 中给出的 csv 文件中.

更新:这是显示转折点的更清晰的图表。

enter image description here

最佳答案

解决此问题的一种简单方法是使用 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()

enter image description here

但是,找到阈值的位置在很大程度上取决于噪声!所以你可能需要做一些平滑,例如

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

enter image description here

请注意,渐变现在更加平滑。 重要:取决于输入的数据,哪个过滤器合适!

关于python - 如何使用 Python 检测图形的转折点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com