Oxonon
Oxonon

Reputation: 281

numpy remove row where any value matches condition

I have RGB values in range [0,1] in an array as such:

[[0.2, 0.2, 0.3], [0.1, 0.1, 0.1], [0.4, 0.3, 0.5]]

I would like to remove any rows where any value is below 0.15 (any colour is less than 0.15 in intensity). That is, I'd like the above array to change to:

[[0.2, 0.2, 0.3], [0.4, 0.3, 0.5]]

I'm trying things along the lines of:

od = od[any(od, axis=1) > 0.15]

How should I be doing this? Why does the above do nothing?

Upvotes: 0

Views: 554

Answers (1)

CT Zhu
CT Zhu

Reputation: 54330

Use: any():

In [146]:

arr = np.array([[0.2, 0.2, 0.3], [0.1, 0.1, 0.1], [0.4, 0.3, 0.5], [0.4, 0.3, 0.5]])
arr
Out[146]:
array([[ 0.2,  0.2,  0.3],
       [ 0.1,  0.1,  0.1],
       [ 0.4,  0.3,  0.5],
       [ 0.4,  0.3,  0.5]])
In [147]:

arr[~(arr<0.15).any(1)]
Out[147]:
array([[ 0.2,  0.2,  0.3],
       [ 0.4,  0.3,  0.5],
       [ 0.4,  0.3,  0.5]])

Upvotes: 2

Related Questions