我有一个一维函数需要花费很多时间来计算一个大的“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是太慢了......
那可能会给边缘带来怪异 – reptilicus 2013-03-01 17:31:02
@reptilicus为什么?我似乎不明白你的意思,对不起... – Jaime 2013-03-01 17:34:27
我在想,插值会导致原始数组边缘的值不同,一旦数组变平,然后插值然后重新变形。基本上,原始数组的左边将插入下一行的右边缘,这可能会或可能不会导致问题。 – reptilicus 2013-03-01 17:45:08