根据一列中的值屏蔽 2D numpy 数组 [英] mask a 2D numpy array based on values in one column
本文介绍了根据一列中的值屏蔽 2D numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下 numpy 数组:
Suppose I have the following numpy array:
a = [[1, 5, 6],
[2, 4, 1],
[3, 1, 5]]
我想屏蔽第一列中有 1
的所有行.也就是说,我想要
I want to mask all the rows which have 1
in the first column. That is, I want
[[--, --, --],
[2, 4, 1],
[3, 1, 5]]
这是否可以使用 numpy 掩码数组操作来完成?怎么办?
Is this possible to do using numpy masked array operations? How can one do it?
谢谢.
推荐答案
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]])
这篇关于根据一列中的值屏蔽 2D numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文