【发布时间】:2017-08-28 06:22:50
【问题描述】:
我的问题扩展了此处看到的代码响应:Interpolating a 3d array in Python. How to avoid for loops?。相关原解决方案代码如下:
import numpy as np
from scipy.interpolate import interp1d
array = np.random.randint(0, 9, size=(100, 100, 100))
x = np.linspace(0, 100, 100)
x_new = np.linspace(0, 100, 1000)
new_array = interp1d(x, array, axis=0)(x_new)
new_array.shape # -> (1000, 100, 100)
当 x_new 是一个常量一维数组时,上述方法效果很好,但如果我的 x_new 不是一个常量一维数组,而是取决于纬度/经度维度的索引怎么办在另一个 3-d 数组中。我的 x_new 大小为 355x195x192(时间 x 纬度 x 长),现在我正在通过纬度和经度维度进行双重循环。由于每个纬度/经度对的 x_new 都不同,我怎样才能避免如下所示的循环?不幸的是,我的循环过程需要几个小时......
x_new=(np.argsort(np.argsort(modell, 0), 0).astype(float) + 1) / np.size(modell, 0)
## x_new is shape 355x195x192
## pobs is shape 355x1
## prism_aligned_tmax_sorted is shape 355x195x192
interp_func = interpolate.interp1d(pobs, prism_aligned_tmax_sorted,axis=0)
tmaxmod = np.empty((355, 195, 192,))
tmaxmod[:] = np.NAN
for latt in range(0, 195):
for lonn in range(0, 192):
temp = interp_func(x_new[:,latt,lonn])
tmaxmod[:,latt,lonn] = temp[:,latt,lonn]
感谢您的所有帮助!
【问题讨论】:
标签: python arrays numpy scipy interpolation