有没有办法使用 NumPy 对向量值函数进行插值?/SciPy ?
有很多针对标量值函数的产品,我想我可以使用其中之一来分别估计向量的每个分量,但有没有更有效的方法?
具体来说,我有一个函数 f(x) = V
,其中 x
是标量,V
是向量。我还有一个 xs
及其对应的 Vs
的集合。我想用它来插入和估计任意 x
的 V
。
最佳答案
插值函数 scipy.interpolate.interp1d
也适用于插值的向量值数据(尽管不适用于向量值参数数据)。因此,只要x
是标量,就可以直接使用。
以下代码是对 the scipy documentation 中给出的示例的轻微扩展:
>>> from scipy.interpolate import interp1d
>>> x = np.linspace(0, 10, 10)
>>> y = np.array([np.exp(-x/3.0), 2*x])
>>> f = interp1d(x, y)
>>> f(2)
array([ 0.51950421, 4. ])
>>> np.array([np.exp(-2/3.0), 2*2])
array([ 0.51341712, 4. ])
请注意,2 不在参数向量 x
中,因此本例中 y
中第一个分量的插值误差。
关于python - 使用 NumPy/SciPy 的向量值函数插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11129958/