Suppose I have the following numpy array:
假设我有下面的numpy数组:
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
我想掩盖第一列中有1的所有行。这是我想要的
[[--, --, --],
[2, 4, 1],
[3, 1, 5]]
Is this possible to do using numpy masked array operations? How can one do it?
是否可以使用numpy掩蔽数组操作?怎么做呢?
Thanks.
谢谢。
3 个解决方案
#1
7
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]])
#2
3
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]))
#3
0
You could simply create an empty mask and then use numpy-broadcasting (like @eumiro showed) but using the element- and bitwise "or" operator |
:
您可以简单地创建一个空掩码,然后使用numpy-broadcast(如@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)
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.
这种方法的另一个优点是,它不需要使用可能昂贵的乘法或np。重复,这样应该很快。
#1
7
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]])
#2
3
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]))
#3
0
You could simply create an empty mask and then use numpy-broadcasting (like @eumiro showed) but using the element- and bitwise "or" operator |
:
您可以简单地创建一个空掩码,然后使用numpy-broadcast(如@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)
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.
这种方法的另一个优点是,它不需要使用可能昂贵的乘法或np。重复,这样应该很快。