Your X
值被颠倒,scipy.interpolate.spline
要求自变量单调递增,并且不推荐使用此方法 - 使用interp1d
相反(见下文)。
>>> from scipy.interpolate import spline
>>> import numpy as np
>>> X = [736176.0, 736175.0, 736174.0] # <-- your original X is decreasing
>>> Y = [711.74, 730.0, 698.0]
>>> Xsmooth = np.linspace(736174.0, 736176.0, 10)
>>> spline(X, Y, Xsmooth)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
reverse X
and Y
首先,它有效
>>> spline(
... list(reversed(X)), # <-- reverse order of X so also
... list(reversed(Y)), # <-- reverse order of Y to match
... Xsmooth
... )
array([ 698. , 262.18297973, 159.33767533, 293.62017489,
569.18656683, 890.19293934, 1160.79538066, 1285.149979 ,
1167.41282274, 711.74 ])
请注意,许多样条插值方法需要X
单调递增:
x
: (N,) 类数组- 独立输入数据的一维数组。一定会增加。
- InterpolatedUnivariateSpline
x
: (N,) 类数组- 数据点的输入维度 – 必须递增
默认顺序为scipy.interpolate.spline
是立方的。由于只有 3 个数据点,三次样条之间存在很大差异 (order=3
)和二次样条(order=2
)。下图显示了不同阶样条之间的差异;注:100分用于smooth拟合曲线more.
的文档scipy.interpolate.spline含糊不清,表明它可能不受支持。例如,它没有列在scipy.interpolate主页或在插值教程. The 来源spline显示它实际上调用了spleval and splmake列在下面附加工具 as:
现有功能是为了向后兼容(不应在新代码中使用).
我会跟随板球_007的建议和使用interp1d。这是当前建议的方法,它有很好的文档记录教程中的详细示例和 API,它允许自变量默认不排序(任何顺序)(参见assume_sorted
API 中的参数)。
>>> from scipy.interpolate import interp1d
>>> f = interp1d(X, Y, kind='quadratic')
>>> f(Xsmooth)
array([ 711.74 , 720.14123457, 726.06049383, 729.49777778,
730.45308642, 728.92641975, 724.91777778, 718.4271605 ,
709.4545679 , 698. ])
如果数据排名不足,它也会引发错误。
>>> f = interp1d(X, Y, kind='cubic')
ValueError:x 和 y 数组必须至少有 4 个条目