用python修正拐点估计

2024-09-28 21:59:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python找到曲线上的拐点。曲线的数据如下:https://www.dropbox.com/s/rig8frgewde8i5n/fitted.txt?dl=0。请注意,曲线已与原始数据拟合。此处提供原始数据:https://www.dropbox.com/s/1lskykdi1ia1lu7/ww.txt?dl=0

import numpy as np
# Read in array from text file
arr = np.loadtxt(path_to_file)

inflexion_point_1 = np.diff(arr).argmin()
inflexion_point_2 = np.diff(arr).argmax()

这两个拐点在附图中显示为红线。然而,他们的位置似乎并不正确。第一个拐点应靠近黑色箭头指示的区域。我怎么解决这个问题?在

enter image description here

另外,下面是微分曲线图:

^{pr2}$

正如您所看到的,代码的行为是编码的,即它找到的argmaxnp差异数组的。不过,我想找一个离第110天更近的位置,也就是说,离argmax大约一半。在

enter image description here

--编辑--

另外,这是另一个显示原始数据和拟合曲线的曲线图(使用二次函数)。在

enter image description here


Tags: httpstxtcom原始数据wwwnpdiff曲线
1条回答
网友
1楼 · 发布于 2024-09-28 21:59:27

有什么理由不直接在梯度上使用单变量样条函数?在

from scipy.interpolate import UnivariateSpline

#raw data
data = np.genfromtxt('ww.txt')

plt.plot(np.gradient(data), '+')

spl = UnivariateSpline(np.arange(len(data)), np.gradient(data), k=5)
spl.set_smoothing_factor(1000)
plt.plot(spl(np.arange(len(data))), label='Smooth Fct 1e3')
spl.set_smoothing_factor(10000)
plt.plot(spl(np.arange(len(data))), label='Smooth Fct 1e4')
plt.legend(loc='lower left')

max_idx = np.argmax(spl(np.arange(len(data))))
plt.vlines(max_idx, -5, 9, linewidth=5, alpha=0.3)

enter image description here

我们也可以用数值求解最大值:

^{pr2}$

相关问题 更多 >