插值Python中的三维阵列。如何避免for循环? [英] Interpolating a 3d array in Python. How to avoid for loops?

查看:849
本文介绍了插值Python中的三维阵列。如何避免for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要插在第一轴的数组。目前我做它像这样的例子:

I have an array which I want to interpolate over the 1st axes. At the moment I am doing it like this example:

import numpy as np
from scipy.interpolate import interp1d

array = np.random.randint(0, 9, size=(100, 100, 100))
new_array = np.zeros((1000, 100, 100))
x = np.arange(0, 100, 1)
x_new = np.arange(0, 100, 0.1)

for i in x:
    for j in x:
        f = interp1d(x, array[:, i, j])
        new_array[:, i, j] = f(xnew)

我用重presents10年5天的平均在一个域中的每个纬度和经度值的数据。我想创建日常值的数组。

The data I use represents 10 years of 5-day averaged values for each latitude and longitude in a domain. I want to create an array of daily values.

我也用样条试过。我真的不知道他们是如何工作的,但它不是要快得多。

I have also tried using splines. I don't really know how they work but it was not much faster.

有没有办法做到这一点,而不使用循环?
如果循环必须使用,是否有其他方法可以加速这个吗?

Is there a way to do this without using for loops? If for loops must be used, are there other ways to speed this up?

感谢您事先的任何建议。

Thank you in advance for any suggestions.

推荐答案

您可以指定一个轴参数interp1d:

You can specify an axis argument to interp1d:


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)

这篇关于插值Python中的三维阵列。如何避免for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆