根据一列中的值屏蔽 2D numpy 数组

发布于 2024-10-11 11:23:07 字数 276 浏览 9 评论 0原文

假设我有以下 numpy 数组:

a = [[1, 5, 6],
     [2, 4, 1],
     [3, 1, 5]]

我想屏蔽第一列中包含 1 的所有行。也就是说,我想

   [[--, --, --],
     [2, 4, 1],
     [3, 1, 5]]

使用 numpy 掩码数组操作可以做到这一点吗?一个人怎样才能做到呢?

谢谢。

Suppose I have the following numpy array:

a = [[1, 5, 6],
     [2, 4, 1],
     [3, 1, 5]]

I want to mask all the rows which have 1 in the first column. That is, I want

   [[--, --, --],
     [2, 4, 1],
     [3, 1, 5]]

Is this possible to do using numpy masked array operations? How can one do it?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2024-10-18 11:23:07
import numpy as np

a = np.array([[1, 5, 6],
              [2, 4, 1],
              [3, 1, 5]])

np.ma.MaskedArray(a, mask=(np.ones_like(a)*(a[:,0]==1)).T)

# Returns: 
masked_array(data =
 [[-- -- --]
 [2 4 1]
 [3 1 5]],
             mask =
 [[ True  True  True]
 [False False False]
 [False False False]])
import numpy as np

a = np.array([[1, 5, 6],
              [2, 4, 1],
              [3, 1, 5]])

np.ma.MaskedArray(a, mask=(np.ones_like(a)*(a[:,0]==1)).T)

# Returns: 
masked_array(data =
 [[-- -- --]
 [2 4 1]
 [3 1 5]],
             mask =
 [[ True  True  True]
 [False False False]
 [False False False]])
ㄟ。诗瑗 2024-10-18 11:23:07

您可以通过以下方式创建所需的掩码

mask = numpy.repeat(a[:,0]==1, a.shape[1])

和掩码数组

masked_a = numpy.ma.array(a, mask=numpy.repeat(a[:,0]==1, a.shape[1]))

You can create the desired mask by

mask = numpy.repeat(a[:,0]==1, a.shape[1])

and the masked array by

masked_a = numpy.ma.array(a, mask=numpy.repeat(a[:,0]==1, a.shape[1]))
断舍离 2024-10-18 11:23:07

您可以简单地创建一个空掩码,然后使用 numpy-broadcasting (如 @eumiro 所示),但使用元素和按位“或”运算符 |

>>> a = np.array([[1, 5, 6], [2, 4, 1], [3, 1, 5]])

>>> mask = np.zeros(a.shape, bool) | (a[:, 0] == 1)[:, None]

>>> np.ma.array(a, mask=mask)
masked_array(data =
 [[-- -- --]
 [2 4 1]
 [3 1 5]],
             mask =
 [[ True  True  True]
 [False False False]
 [False False False]],
       fill_value = 999999)

进一步解释:

>>> # select first column
>>> a[:, 0]  
array([1, 2, 3])

>>> # where the first column is 1
>>> a[:, 0] == 1  
array([ True, False, False], dtype=bool)

>>> # added dimension so that it correctly broadcasts to the empty mask
>>> (a[:, 0] == 1)[:, None]  
array([[ True],
       [False],
       [False]], dtype=bool)

>>> # create the final mask
>>> np.zeros(a.shape, bool) | (a[:, 0] == 1)[:, None]  
array([[ True,  True,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)

这种方法的另一个优点是它不需要使用潜在昂贵的乘法或 np.repeat ,所以它应该相当快。

You could simply create an empty mask and then use numpy-broadcasting (like @eumiro showed) but using the element- and bitwise "or" operator |:

>>> a = np.array([[1, 5, 6], [2, 4, 1], [3, 1, 5]])

>>> mask = np.zeros(a.shape, bool) | (a[:, 0] == 1)[:, None]

>>> np.ma.array(a, mask=mask)
masked_array(data =
 [[-- -- --]
 [2 4 1]
 [3 1 5]],
             mask =
 [[ True  True  True]
 [False False False]
 [False False False]],
       fill_value = 999999)

A bit further explanation:

>>> # select first column
>>> a[:, 0]  
array([1, 2, 3])

>>> # where the first column is 1
>>> a[:, 0] == 1  
array([ True, False, False], dtype=bool)

>>> # added dimension so that it correctly broadcasts to the empty mask
>>> (a[:, 0] == 1)[:, None]  
array([[ True],
       [False],
       [False]], dtype=bool)

>>> # create the final mask
>>> np.zeros(a.shape, bool) | (a[:, 0] == 1)[:, None]  
array([[ True,  True,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)

One further advantage of this approach is that it doesn't need to use potentially expensive multiplications or np.repeat so it should be quite fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文