使用 NumPy/SciPy 进行向量值函数插值

问题描述 投票:0回答:2

有没有办法使用 NumPy/SciPy 来插值向量值函数?

有很多适用于标量值函数的产品,我想我可以使用其中之一来分别估计向量的每个分量,但是有没有一种方法可以更有效地做到这一点?

详细说明,我有一个函数

f(x) = V
,其中
x
是标量,
V
是向量。我还收集了
xs
及其对应的
Vs
。我想用它来插值和估计任意
V
x

python numpy scipy interpolation
2个回答
8
投票

插值函数

scipy.interpolate.interp1d
也适用于插值的向量值数据(但不适用于向量值参数数据)。因此,只要
x
是标量,就可以直接使用它。

以下代码是 scipy 文档中给出的示例的轻微扩展:

>>> 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
中第一个分量的插值错误。


0
投票

您还可以像这样矢量化

numpy.interp
函数:

interp = np.vectorize(np.interp, signature='(a),(b),(b)->(a)')
xp = np.linspace(0, 10, 10)
yp = np.random.randn(xp.size, 3)
x = np.array([2.])
interp(x, xp, yp.T).T
© www.soinside.com 2019 - 2024. All rights reserved.