在给定要拒绝的索引的情况下拒绝 3D 数组中的特殊值



我想扩展我已经在Stackoverflow上讨论过的问题。它正在处理 2D numpy arrays,我想对 3D 数组做同样的事情。

我有一个坐标数组 coord ,形状为 (3, 4, 2) ,其中包含我想在数据数组中屏蔽的每个切片的所有坐标 Values2.坐标数组中有被屏蔽的值,以便为每个切片保持相同的维度。

import numpy as np
coord = np.array([[[0, 0], [0, 0], [0, 0], [0, 0]], [[2, 0], [2, 2], [0, 0], [0, 0]], [[2, 2], [2, 0], [2, 1], [2, 2]]])
# Some 0-entries are masked values : [[[nan, nan], [nan, nan], [nan, nan], [nan, nan]], [[2, 0], [2, 2], [nan, nan], [nan, nan]], [[2, 2], [2, 0], [2, 1], [2, 2]]]
Values2 = np.array([[[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]],
       [[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]],
       [[8., 7., 6.],
       [5., 4., 3.],
       [2., 1., 0.]]])    

如果我尝试使用引用的 SO 问题中的代码采用类似的方法,我们有类似的东西,但它似乎不适合 3D 情况,我自己也不知道如何适应它。

i, j = coord.T
mask = np.zeros(Values.shape, bool)
mask[i,j] = True
m = np.ma.array(Values, mask=mask)

组合coord数组和Values2数组所需的输出为:

Array_expected = ([[[ 0.,  1.,  2.],
        [ 3.,  4.,  5.],
        [ 6.,  7.,  8.]],
       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ nan,  1.,  nan]],
       [[ 8.,  7.,  6.],
        [ 5.,  4.,  3.],
        [ nan,  nan,  nan]]])

但是nan条目不应更改 Values2 中存在的值。

我看到这个问题是你上一个问题的后续。

您应该认为从该问题中获得的掩码(与您的coords阵列相关联)同样适用于您的 3D 阵列。因此,与其使用要在数组中屏蔽的元素索引,不如只使用(布尔)掩码

您从上一个问题中获得的面具看起来像:

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

(我在一个维度上进行了更改,因为您上一个问题和这个问题之间的维度不匹配)。

现在,只需使用 Values2 数组创建一个新的屏蔽数组:

>>> result = np.ma.masked_array(Values2, mask=mask)
>>> result 
masked_array(data =
 [[[0.0 1.0 2.0]
  [3.0 4.0 5.0]
  [6.0 7.0 8.0]]
 [[1.0 1.0 1.0]
  [1.0 1.0 1.0]
  [-- 1.0 --]]
 [[8.0 7.0 6.0]
  [5.0 4.0 3.0]
  [-- -- --]]],
             mask =
 [[[False False False]
  [False False False]
  [False False False]]
 [[False False False]
  [False False False]
  [ True False  True]]
 [[False False False]
  [False False False]
  [ True  True  True]]],
       fill_value = 1e+20)

如果您确实不需要遮罩,因此想要填充遮罩条目,则应使用 filled 方法:

>>> np.ma.filled(result, np.nan)
array([[[  0.,   1.,   2.],
        [  3.,   4.,   5.],
        [  6.,   7.,   8.]],
       [[  1.,   1.,   1.],
        [  1.,   1.,   1.],
        [ nan,   1.,  nan]],
       [[  8.,   7.,   6.],
        [  5.,   4.,   3.],
        [ nan,  nan,  nan]]])

关于屏蔽数组的文档充满了可以进一步帮助您的好示例。

使用reshape的解决方案,当然不是更有效的,但它有效:

coord2 = coord.reshape((coord.shape[0]*coord.shape[1],2))
i, j = coord2.T
i_reshape = i.reshape((coord.shape[0],coord.shape[1])).T
j_reshape = j.reshape((coord.shape[0],coord.shape[1])).T

mask = np.zeros(Values2.shape, bool)
for i in range(i_reshape.shape[1]):
    mask[i,i_reshape[:,i],j_reshape[:,i]] = True
m = np.ma.array(Values2, mask=mask)

我得到:

 m
Out[848]: 
masked_array(data =
 [[[-- 1.0 2.0]
  [3.0 4.0 5.0]
  [6.0 7.0 8.0]]
 [[-- 1.0 1.0]
  [1.0 1.0 1.0]
  [-- 1.0 --]]
 [[8.0 7.0 6.0]
  [5.0 4.0 3.0]
  [-- -- --]]],
             mask =
 [[[ True False False]
  [False False False]
  [False False False]]
 [[ True False False]
  [False False False]
  [ True False  True]]
 [[False False False]
  [False False False]
  [ True  True  True]]],
       fill_value = 1e+20)

最新更新