面膜基于价值观二维numpy的阵列中的一列 [英] mask a 2D numpy array based on values in one column

查看:228
本文介绍了面膜基于价值观二维numpy的阵列中的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下numpy的数组:

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

我要屏蔽所有具有 1 在第一列的行。也就是说,我想

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

这可能使用numpy的屏蔽数组操作呢?怎样才能做到这一点?

感谢。


解决方案

 导入numpy的是NP一个= np.array([[1,5,6],
              [2,4,1],
              [3,1,5]])np.ma.MaskedArray(一,掩模=(np.ones_like(一)*(一个[:,0] == 1))T)的#返回:
masked_array(数据=
 [ - - - ]
 [2 4 1]
 [3 1 5],
             面膜=
 [真真真]
 [假假假]
 [假假假])

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.

解决方案

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]])

这篇关于面膜基于价值观二维numpy的阵列中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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