向量值多元函数中的插值 [英] Interpolation in vector-valued multi-variate function
问题描述
在Python中,我正在尝试构造一个例程,该例程可以在多维(5+)参数空间中的矢量值数据中进行插值.即我有一个函数,该函数接受多个输入变量并返回多个输出变量.目前,向量的每个元素都有一个调用.数据在一个列文件中,所以我用
In Python, I'm trying to construct a routine that interpolates in vector-valued data in a multi-dimensional (5+) parameter space. i.e. I have a function that takes a number of input variables and returns a number of output variables. At the moment, there is one call for each element of the vector. The data is in a columned file, so I retrieve it with
import numpy
[x_data,y_data,f1_data,f2_data] = numpy.loadtxt('data',unpack=True)
然后,我使用SciPy的函数实例化各个插值器,例如
Then, I instantiate individual interpolators using SciPy's functions, like
from scipy import interpolate
f1 = interpolate.LinearNDInterpolator((x_data,y_data),f1_data)
f2 = interpolate.LinearNDInterpolator((x_data,y_data),f2_data)
...
现在,当我进行插值调用时,我必须为每个值f1
,f2
等进行插值,即使实际上它应该可以作为一个操作来实现.而且我猜想进行一次插值比进行五次或更多插值要快.
Now, when I make the interpolation call, I have to interpolate for each value f1
, f2
, etc. even though really it should be achievable as one operation. And I'm guessing that making one interpolation should be faster than making 5 or more.
有没有一种方法可以构造矢量(或数组)值的插值器?
我尝试用构造插值器
f = interpolate.LinearNDInterpolator((x_data,y_data),(f1_data,f2_data,...))
但它返回错误
ValueError:不同数量的值和点
ValueError: different number of values and points
我还阅读了interp1d
处理.
I've also read this question and answer but it's about a vector-valued function of a scalar, which can apparently be handled by interp1d
.
推荐答案
scipy.interpolate.LinearNDInterpolator
希望以行优先顺序接收其数据:例如,在您的情况下,第一个参数需要是一个成对的数组,而不是一对数组.由于在加载数据时已转置了数据,因此必须将其再次转回,然后再将其传递到LinearNDInterpolator
.尝试类似的东西:
scipy.interpolate.LinearNDInterpolator
expects to receive its data in row-major order: for example in your case, the first argument needs to be an array of pairs, not a pair of arrays. Since you transposed your data when you loaded it, you'll have to transpose it back again before passing it to LinearNDInterpolator
. Try something like:
points = numpy.array((x, y)).T
values = numpy.array((f1, f2)).T
f = interpolate.LinearNDInterpolator(points, values)
这篇关于向量值多元函数中的插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!