0

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 (i.e. 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 (i.e. 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

2 Answers 2

1

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]]
Sign up to request clarification or add additional context in comments.

Comments

0

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]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.