I guess the answer is close at hand, but I can't see it :-(
I have a boolean mask array of length n:
a = np.array([True, True, True, False, False])
I have a 2d array with n columns:
b = np.array([[1,2,3,4,5], [1,2,3,4,5]])
I want a new array which contains only the "True"-values, eg:
c = ([[1,2,3], [1,2,3]])
c = a * b
does not work because it contains also "0" for the false columns what I don't want
c = np.delete(b, a, 1) does not work
Any suggestions? Thanks!
You probably want something like this:
Note that for this kind of indexing to work, it needs to be an
ndarray
, like you were using, not alist
, or it'll interpret theFalse
andTrue
as0
and1
and give you those columns:You can use numpy.ma module and use np.ma.masked_array function to do so.
x = np.array([1, 2, 3, -1, 5])
mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])
return : masked_array(data=[1, 2, 3, --, 5], mask=[False, False, False, True, False], fill_value=999999)