Mask out specific values from an array

三世轮回 提交于 2019-12-28 07:01:10

问题


Example:

I have an array:

array([[1, 2, 0, 3, 4],
       [0, 4, 2, 1, 3],
       [4, 3, 2, 0, 1],
       [4, 2, 3, 0, 1],
       [1, 0, 2, 3, 4],
       [4, 3, 2, 0, 1]], dtype=int64)

I have a set (variable length, order doesn't matter) of "bad" values:

{2, 3}

I want to return the mask that hides these values:

array([[False,  True, False,  True, False],
       [False, False,  True, False,  True],
       [False,  True,  True, False, False],
       [False,  True,  True, False, False],
       [False, False,  True,  True, False],
       [False,  True,  True, False, False]], dtype=bool)

What's the simplest way to do this in NumPy?


回答1:


Use np.in1d that gives us a flattened mask of such matching occurrences and then reshape back to input array shape for the desired output, like so -

np.in1d(a,[2,3]).reshape(a.shape)

Note that we need to feed in the numbers to be searched as a list or an array.

Sample run -

In [5]: a
Out[5]: 
array([[1, 2, 0, 3, 4],
       [0, 4, 2, 1, 3],
       [4, 3, 2, 0, 1],
       [4, 2, 3, 0, 1],
       [1, 0, 2, 3, 4],
       [4, 3, 2, 0, 1]])

In [6]: np.in1d(a,[2,3]).reshape(a.shape)
Out[6]: 
array([[False,  True, False,  True, False],
       [False, False,  True, False,  True],
       [False,  True,  True, False, False],
       [False,  True,  True, False, False],
       [False, False,  True,  True, False],
       [False,  True,  True, False, False]], dtype=bool)

2018 Edition : numpy.isin

Use NumPy built-in np.isin (introduced in 1.13.0) that keeps the shape and hence doesn't require us to reshape afterwards -

In [153]: np.isin(a,[2,3])
Out[153]: 
array([[False,  True, False,  True, False],
       [False, False,  True, False,  True],
       [False,  True,  True, False, False],
       [False,  True,  True, False, False],
       [False, False,  True,  True, False],
       [False,  True,  True, False, False]])



回答2:


In [965]: np.any([x==i for i in (2,3)],axis=0)
Out[965]: 
array([[False,  True, False,  True, False],
       [False, False,  True, False,  True],
       [False,  True,  True, False, False],
       [False,  True,  True, False, False],
       [False, False,  True,  True, False],
       [False,  True,  True, False, False]], dtype=bool)

This does iterate, but if the (2,3) set is small (relative to the size of x) this is relatively fast. In fact for small arr2, np.in1d does this:

        mask = np.zeros(len(ar1), dtype=np.bool)
        for a in ar2:
            mask |= (ar1 == a)

Making a masked array from this:

In [970]: np.ma.MaskedArray(x,mask)
Out[970]: 
masked_array(data =
 [[1 -- 0 -- 4]
 [0 4 -- 1 --]
 [4 -- -- 0 1]
 [4 -- -- 0 1]
 [1 0 -- -- 4]
 [4 -- -- 0 1]],
             mask =
 [[False  True False  True False]
 [False False  True False  True]
 [False  True  True False False]
 [False  True  True False False]
 [False False  True  True False]
 [False  True  True False False]],
       fill_value = 999999)



回答3:


There might be simpler ways than this. But this can be one way:

import numpy as np

a = np.array([[1, 2, 0, 3, 4],
       [0, 4, 2, 1, 3],
       [4, 3, 2, 0, 1],
       [4, 2, 3, 0, 1],
       [1, 0, 2, 3, 4],
       [4, 3, 2, 0, 1]], dtype=np.int64)

f = np.vectorize(lambda x: x in {2,3})
print f(a)

Output:

[[False  True False  True False]
 [False False  True False  True]
 [False  True  True False False]
 [False  True  True False False]
 [False False  True  True False]
 [False  True  True False False]]


来源:https://stackoverflow.com/questions/41318942/mask-out-specific-values-from-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!