gpt4 book ai didi

python - python中Klipfolio的曲线拟合算法

转载 作者:行者123 更新时间:2023-12-01 07:47:39 25 4
gpt4 key购买 nike

我正在寻找一种方法来应用这种算法进行曲线拟合,该线不会超过更适合的值

下面是带有示例值的 python 代码,如果您复制/粘贴它并运行,您将看到右侧的行位于点上方

from scipy import interpolate
from numpy import linspace

y = [5, 0, 4, 4]
x = linspace(0, len(y)-1, num=len(y), endpoint=True)
f2 = interp1d(x, y, kind='cubic')
xnew = linspace(0, len(y)-1, num=100, endpoint=True)

plt.plot(xnew, f2(xnew), '--');
plt.scatter(x=[i for i in range(len(x))],y=y, color='red');

最佳答案

这是一个基于您的代码的示例,比较线性、二次和三次插值。只有线性插值不在右侧数据点之上。除了线性插值之外,我所知道的用于此目的的唯一通用方法是裁剪 - 我个人认为这是一种不好的做法。

plot

from scipy import interpolate
from numpy import linspace
import matplotlib
import matplotlib.pyplot as plt

y = [5, 0, 4, 4]
x = linspace(0, len(y)-1, num=len(y), endpoint=True)
f1 = interpolate.interp1d(x, y, kind='linear')
f2 = interpolate.interp1d(x, y, kind='quadratic')
f3 = interpolate.interp1d(x, y, kind='cubic')
xnew = linspace(0, len(y)-1, num=100, endpoint=True)

plt.title('Interpolation comparison')
plt.plot(xnew, f1(xnew), linestyle='solid', color='red', label='linear');
plt.plot(xnew, f2(xnew), linestyle='dashed', color='green', label='quadratic');
plt.plot(xnew, f3(xnew), linestyle='dashdot', color='blue', label='cubic');
plt.scatter(x=[i for i in range(len(x))],y=y, color='black');
plt.legend() # turn on the legend
plt.show()

关于python - python中Klipfolio的曲线拟合算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56375946/

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