简体   繁体   中英

Python numpy boolean masking on 2d np array

I have two 2d numpy arrays X and Y that look like:

X = np.array([[1,2,3,4],[4,5,6,7],[4,3,2,1],[7,8,9,0]]
            )
Y = np.array([[0,0,0],[1,2,4],[1,1,1], [0,0,0]]
            )

I want to remove all the arrays in Y that are all 0's (ie np.zeros), and all the corresponding arrays at the same index in the X array.

So, for these two X,Y arrays, I'd like back:

X = np.array([[4,5,6,7],[4,3,2,1]]
            )
Y = np.array([[1,2,4],[1,1,1]]
            )

X and Y will always have the same length, and X and Y will always be rectangular (ie every array within X will have the same length, and every array within Y will have the same length).

I tried using a loop but that doesn't seem to be as effective for large X and Y

Create a boolean array indicating any non zero element for each row and then filter with boolean array indexing :

any_zero = (Y != 0).any(1)

X[any_zero]
#[[4 5 6 7]
# [4 3 2 1]]

Y[any_zero]    
#[[1 2 4]
# [1 1 1]]

First you will need to get a mask of which rows of Y are all zeros. This can be done with the any method and setting the axis to 1

Y.any(axis = 1)

Will return array([False, True, True, False]) You can use this array to get which rows you want to return from X and Y

X[Y.any(axis = 1)]

will return

array([[4, 5, 6, 7],
       [4, 3, 2, 1]])

and

Y[Y.any(axis = 1)]

will return

array([[1, 2, 4],
       [1, 1, 1]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM