【发布时间】:2013-02-16 06:15:25
【问题描述】:
我有一个一维函数,它需要花费大量时间来计算一个包含“x”值的大型二维数组,因此使用 SciPy 工具创建一个插值函数然后使用它计算 y 非常容易,这会快得多。但是,我不能对一维以上的数组使用插值函数。
例子:
# First, I create the interpolation function in the domain I want to work
x = np.arange(1, 100, 0.1)
f = exp(x) # a complicated function
f_int = sp.interpolate.InterpolatedUnivariateSpline(x, f, k=2)
# Now, in the code I do that
x = [[13, ..., 1], [99, ..., 45], [33, ..., 98] ..., [15, ..., 65]]
y = f_int(x)
# Which I want that it returns y = [[f_int(13), ..., f_int(1)], ..., [f_int(15), ..., f_int(65)]]
但返回:
ValueError: object too deep for desired array
我知道我可以遍历所有 x 成员,但我不知道这是否是一个更好的选择...
谢谢!
编辑:
这样的功能也可以完成这项工作:
def vector_op(function, values):
orig_shape = values.shape
values = np.reshape(values, values.size)
return np.reshape(function(values), orig_shape)
我已经尝试过 np.vectorize 但它太慢了...
【问题讨论】:
标签: python numpy scipy interpolation