屏蔽numpy数组的特定列 [英] Mask specific columns of a numpy array

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

问题描述

我有一个(60,1000)维的二维numpy数组A.
说,我有一个变量idx=array([3,72,403, 512, 698]).

I have a 2D numpy array A of (60,1000) dimensions.
Say, I have a variable idx=array([3,72,403, 512, 698]).

现在,我要屏蔽idx中指定的列中的所有元素.这些列中的值可能会出现在其他列中,但不应屏蔽它们.

Now, I want to mask all the elements in the columns specified in idx. The values in these columns might appear in other columns but they shouldn't be masked.

将寻求任何帮助.

推荐答案

In [22]: A = np.random.rand(5, 10)

In [23]: idx = np.array([1, 3, 5])

In [24]: m = np.zeros_like(A)

In [25]: m[:,idx] = 1

In [26]: Am = np.ma.masked_array(A, m)

In [27]: Am
Out[27]: 
masked_array(data =
 [[0.680447483547 -- 0.290757600047 -- 0.0718559525615 -- 0.334352145502
  0.0861242618662 0.527068091963 0.136280743038]
 [0.729374999214 -- 0.76026650048 -- 0.656082247985 -- 0.492464543871
  0.903026937193 0.0792660503403 0.892132409419]
 [0.0845266821684 -- 0.838838594048 -- 0.396344231382 -- 0.703748703373
  0.380441396691 0.010521007806 0.344945867845]
 [0.7501401585 -- 0.0685427000113 -- 0.587100320511 -- 0.780160645327
  0.276328587928 0.0665949459004 0.604174142611]
 [0.599926798275 -- 0.686378805503 -- 0.776940069716 -- 0.0452833614622
  0.598622591094 0.942843765543 0.528082379918]],
             mask =
 [[False  True False  True False  True False False False False]
 [False  True False  True False  True False False False False]
 [False  True False  True False  True False False False False]
 [False  True False  True False  True False False False False]
 [False  True False  True False  True False False False False]],
       fill_value = 1e+20)

这篇关于屏蔽numpy数组的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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