样条插值
Spline interpolation
我在对以下集合执行样条插值时遇到困难:
import numpy
SOURCE = numpy.array([[1,2,3],[3,4,5], [9,10,11]])
from scipy.interpolate import griddata
from scipy.interpolate import interp1d
input = [0.5,2,3,6,9,15]
线性插值工作正常,但是当我用三次方替换线性时,出现错误:
f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="linear", axis=0, bounds_error=False)
f(input)
f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="cubic", axis=0, bounds_error=False)
ValueError: The number of derivatives at boundaries does not match: expected 1, got 0+0
如何执行此三次插值?
您的 SOURCE
数据太短。三次样条至少需要四个点进行插值,但您只提供了三个。如果您向 SOURCE
添加一个值,它应该或多或少按预期工作:
>>> SOURCE = numpy.array([[1,2,3],[3,4,5], [9,10,11], [12,13,14]]) # added an extra value
>>> f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="cubic", axis=0, bounds_error=False)
>>> f(input)
array([[nan, nan],
[ 3., 4.],
[ 4., 5.],
[ 7., 8.],
[10., 11.],
[nan, nan]])
我在对以下集合执行样条插值时遇到困难:
import numpy
SOURCE = numpy.array([[1,2,3],[3,4,5], [9,10,11]])
from scipy.interpolate import griddata
from scipy.interpolate import interp1d
input = [0.5,2,3,6,9,15]
线性插值工作正常,但是当我用三次方替换线性时,出现错误:
f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="linear", axis=0, bounds_error=False)
f(input)
f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="cubic", axis=0, bounds_error=False)
ValueError: The number of derivatives at boundaries does not match: expected 1, got 0+0
如何执行此三次插值?
您的 SOURCE
数据太短。三次样条至少需要四个点进行插值,但您只提供了三个。如果您向 SOURCE
添加一个值,它应该或多或少按预期工作:
>>> SOURCE = numpy.array([[1,2,3],[3,4,5], [9,10,11], [12,13,14]]) # added an extra value
>>> f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="cubic", axis=0, bounds_error=False)
>>> f(input)
array([[nan, nan],
[ 3., 4.],
[ 4., 5.],
[ 7., 8.],
[10., 11.],
[nan, nan]])