我有一個一維函數需要花費很多時間來計算一個大的「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