我的问题扩展了在此处看到的代码响应:Interpolating a 3d array in Python. How to avoid for loops?。有关原始解决方案代码如下:在Python中插值3d数组扩展
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是一个常数1-d阵列,但如果我的x_new 也不是什么恒定的1-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]
感谢您的任何和所有帮助!