我正在寻找一种方法来应用这种算法进行曲线拟合,这条线不超过它的值,它是更合适的
下面是带有样本值的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');发布于 2019-05-30 21:51:51
这是一个基于你的代码比较线性,二次和三次插值的例子。只有线性插值不在右侧数据点的上方。除了线性插值,我所知道的唯一的通用方法是裁剪--我个人认为这是一种糟糕的做法。

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()https://stackoverflow.com/questions/56375946
复制相似问题