Python 使用插值单变量样条线和插值1D的结果略有不同

Python 使用插值单变量样条线和插值1D的结果略有不同,python,scipy,curve-fitting,Python,Scipy,Curve Fitting,我曾使用过拟合三次样条曲线,但遇到了一些记忆问题,因此根据以下内容,我已切换到使用。然而,我注意到结果函数之间存在一些(非常)小的差异。因此,我的问题是: A.据我所知,造成差异的原因与基本方法(是否使用FITPACK)有关。然而,基础的数学应该是不同的吗 B.是否可以使用插值单变量样条线(改变平滑样条线阶数或边界只会使两个图形更加不同)重现interp1d结果 再现细微差异的最小代码: from scipy.interpolate import interp1d from scipy.inte

我曾使用过拟合三次样条曲线,但遇到了一些记忆问题,因此根据以下内容,我已切换到使用。然而,我注意到结果函数之间存在一些(非常)小的差异。因此,我的问题是:

A.据我所知,造成差异的原因与基本方法(是否使用FITPACK)有关。然而,基础的数学应该是不同的吗

B.是否可以使用插值单变量样条线(改变平滑样条线阶数或边界只会使两个图形更加不同)重现interp1d结果

再现细微差异的最小代码:

from scipy.interpolate import interp1d
from scipy.interpolate import InterpolatedUnivariateSpline
import matplotlib.pyplot as plt
import matplotlib
import numpy

x = [916.03189697265634, 916.0718969726563, 916.11189697265627, 916.15189697265623, 916.1918969726562, 916.23189697265627, 916.27189697265624, 916.31189697265631, 916.35189697265628, 916.39189697265624, 916.4318969726562, 916.47189697265628, 916.51189697265625, 916.55189697265632, 916.59189697265629, 916.63189697265625, 916.67189697265621, 916.71189697265618]
y = [893483.0, 2185234.0, 3903053.0, 4264327.0, 3128900.0, 1374942.0, 554350.0, 442512.0, 414232.0, 403098.0, 413778.0, 264185.0, 363063.0, 473762.0, 452284.0, 526806.0, 461402.0, 424270.0]
newX = numpy.linspace(x[0],x[-1],2500*(x[-1]-x[0]))

f_interp1d = interp1d(x,y, kind='cubic')
f_Univariate = InterpolatedUnivariateSpline(x,y)
yINTER = f_interp1d(newX)
yUNIVAR = f_Univariate(newX)
fig =  plt.figure()
ax = fig.add_subplot(111)
plt.plot(x,y,'b*')
plt.plot(newX,yINTER,'r--')
plt.plot(newX,yUNIVAR,'g--')
plt.legend(['Raw Data','Interp1d','Univariate Spline'],loc='best')
plt.show()
生成以下图表(看起来很好):

但是,近距离观察表明存在差异:

from scipy.interpolate import interp1d
from scipy.interpolate import InterpolatedUnivariateSpline
import matplotlib.pyplot as plt
import matplotlib
import numpy

x = [916.03189697265634, 916.0718969726563, 916.11189697265627, 916.15189697265623, 916.1918969726562, 916.23189697265627, 916.27189697265624, 916.31189697265631, 916.35189697265628, 916.39189697265624, 916.4318969726562, 916.47189697265628, 916.51189697265625, 916.55189697265632, 916.59189697265629, 916.63189697265625, 916.67189697265621, 916.71189697265618]
y = [893483.0, 2185234.0, 3903053.0, 4264327.0, 3128900.0, 1374942.0, 554350.0, 442512.0, 414232.0, 403098.0, 413778.0, 264185.0, 363063.0, 473762.0, 452284.0, 526806.0, 461402.0, 424270.0]
newX = numpy.linspace(x[0],x[-1],2500*(x[-1]-x[0]))

f_interp1d = interp1d(x,y, kind='cubic')
f_Univariate = InterpolatedUnivariateSpline(x,y)
yINTER = f_interp1d(newX)
yUNIVAR = f_Univariate(newX)
fig =  plt.figure()
ax = fig.add_subplot(111)
plt.plot(x,y,'b*')
plt.plot(newX,yINTER,'r--')
plt.plot(newX,yUNIVAR,'g--')
plt.legend(['Raw Data','Interp1d','Univariate Spline'],loc='best')
plt.show()

我发现主要的区别在于插值单变量样条线尝试执行连续拟合,而立方插值1d应用分段拟合

我提出的唯一解决方案(目前)是确保两个函数只使用4个数据点(最高数据点附近),因为两个函数都将产生一个解决方案(而不是使用5个数据点的单个解决方案和两个部分解决方案)

片段:

# Strip top point
maxInt = 0
for index,i in enumerate(y):
    if i > maxInt:
        maxInt = i
x_sub = x[y.index(maxInt)-2:y.index(maxInt)+2]
y_sub = y[y.index(maxInt)-2:y.index(maxInt)+2]
newX = numpy.linspace(x_sub[0],x_sub[-1],2500*(x_sub[-1]-x_sub[0]))

f_interp1d = interp1d(x_sub,y_sub, kind='cubic')
f_Univariate = InterpolatedUnivariateSpline(x_sub,y_sub)
yINTER = f_interp1d(newX)
yUNIVAR = f_Univariate(newX)
fig =  plt.figure()
ax = fig.add_subplot(111)
plt.plot(x,y,'b*')
plt.plot(newX,yINTER,'r--')
plt.plot(newX,yUNIVAR,'g--')
plt.legend(['Raw Data','Interp1d','Univariate Spline'],loc='best')
plt.show()
这将生成以下图形(缩小):

特写显示这两个功能确实“相同”:

然而,我仍然希望有更好的方法迫使这两个函数产生类似的行为