我的问题扩展了这里看到的代码响应:在Python中插入一个3d数组.如何避免for循环?.相关的原始解决方案代码如下:
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)
Run Code Online (Sandbox Code Playgroud)
当x_new是一个常数1-d数组时,上述方法很有效,但是如果我的x_new 不是常数1-d数组,而是取决于另一个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 …Run Code Online (Sandbox Code Playgroud)