0

I have a 4*5 NumPy array and I want to retrieve rows in which all elements are less than 5.

arr = np.array([[0,2,3,4,5],[1,2,4,1,3], [2,2,5,4,6], [0,2,3,4,3]])
arr[np.where(arr[:,:] <= 4)] 

expected output:

[[1,2,4,1,3],[0,2,3,4,3]]

actual output:

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

Any help is appreciated!

1 Answer 1

1

This actually quite simple. Just convert the entire array to booleans (each value is True if it's less than 5, False otherwise), and use np.all with axis=1 to return True for each row where all items are True:

>>> arr[np.all(arr < 5, axis=1)]
array([[1, 2, 4, 1, 3],
       [0, 2, 3, 4, 3]])
Sign up to request clarification or add additional context in comments.

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.