带有屏蔽数据的 Scipy 插值? [英] Scipy interpolation with masked data?

查看:25
本文介绍了带有屏蔽数据的 Scipy 插值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入一个包含屏蔽数据的二维数组.我使用了一些 SciPy 模块的可用方法,包括 interp2dbisplrep/bisplev 以及 RectBivariateSpline.作为附加信息,我的数据是一个规则数组,这意味着网格具有相同的维度(在本例中为 1ºX1º).

I am trying to interpolate a 2D array that contents masked data. I have used some of the SciPy module's methods available, including interp2d, bisplrep/bisplev, as well as RectBivariateSpline. As an additional information, my data is a regular array, which means that grids have the same dimension (in this case 1ºX1º).

话虽如此,有没有什么办法可以用Python在数组中插入避免屏蔽数据?我还是新手,使用 Python 和 NumPy/SciPy 模块.

Having said that, is there any way to interpolate avoiding masked data in an array with Python? I am still new using Python and NumPy/SciPy modules.

推荐答案

您实际上可以使用每个接受 x, y, z 的函数(对于 interp2d 可能还有其他)与您的屏蔽数据.但是您需要明确创建一个 mgrid:

You can actually use every function that accepts x, y, z (which is the case for interp2d and probably the others as well) with your masked data. But you need to explicitly create a mgrid:

z = ... # Your data
x, y = np.mgrid[0:z.shape[0], 0:z.shape[1]]

然后你需要删除所有这些坐标中的所有掩码值:

Then you need to delete all masked values in all of these coordinates:

x = x[~z.mask]
y = y[~z.mask]
z = z[~z.mask]

通过这些最终的 x, y, z,您可以调用每个指定的函数(接受不完整的网格,因此 RectBivariateSpline 将不起作用).但是请注意,其中一些使用插值框,因此如果由于掩码而丢弃数据的区域太大,则插值将在那里失败(导致 np.nan 或 0).但是,如果发生这种情况,您可能会调整参数以对此进行补偿.

With these final x, y, z you can call every of your specified functions (that accepts incomplete grids, so RectBivariateSpline won't work). Notice however that some of these use interpolation boxes so if there is a too big region where you discarded the data because of your mask the interpolation will fail there (resulting in np.nan or 0). But you might tweak the parameters to compensate for that, if it happens.

data = np.random.randint(0, 10, (5,5))
mask = np.random.uniform(0,1,(5,5)) > 0.5
z = np.ma.array(data, mask=mask)
x, y = np.mgrid[0:z.shape[0], 0:z.shape[1]]
x1 = x[~z.mask]
y1 = y[~z.mask]
z1 = z[~z.mask]
interp2d(x1, y1, z1)(np.arange(z.shape[0]), np.arange(z.shape[1]))

array([[  1.1356716 ,   2.45313727,   3.77060294,   6.09790177, 9.31328935],
       [  3.91917937,   4.        ,   4.08082063,   3.98508121, 3.73406764],
       [ 42.1933738 ,  25.0966869 ,   8.        ,   0.        , 0.        ],
       [  1.55118338,   3.        ,   4.44881662,   4.73544593, 4.        ],
       [  5.        ,   8.        ,  11.        ,   9.34152525, 3.58619652]])

你可以看到 0 的小区域,因为掩码有很多掩码值:

you can see the small area of 0's because the mask had there many masked values:

mask
array([[False,  True,  True,  True, False],
       [False, False,  True, False, False],
       [ True,  True, False,  True,  True],
       [False,  True, False,  True,  True],
       [False,  True, False, False,  True]], dtype=bool)

data
array([[2, 4, 4, 5, 5],
       [1, 4, 1, 3, 8],
       [9, 1, 8, 0, 9],
       [7, 2, 0, 3, 4],
       [9, 6, 0, 4, 4]])

这篇关于带有屏蔽数据的 Scipy 插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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