且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-26 15:41:17

插值函数scipy.interpolate.interp1d还可用于插值的矢量值数据(尽管不适用于矢量值参数数据).因此,只要x是标量,就可以直接使用它.

The interpolation function scipy.interpolate.interp1d also works on vector-valued data for the interpolant (not for vector-valued argument data though). Thus, as long as x is scalar, you can use it directly.

以下代码是 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.        ])

请注意,参数向量x中没有2,因此在此示例中,y中第一个分量的插值误差.

Note that 2 is not in the argument vector x, thus the interpolation error for the first component in y in this example.