Reputation: 2442
I am taking the output from AUTO numerical continuation package, and need to filter out results that have negative values of the variables, as they are non-physical. So if I have, for example:
>>> a = np.array([[0,1,2,3,4],[-1,-0.5,0,0.5,1],[-3,-4,-5,0.1,0.2]])
I would like to be left with:
>>> b
array([[ 3. , 4. ],
[ 0.5, 1. ],
[ 0.1, 0.2]])
But when I try numpy.where
I get:
>>> b = a[:,(np.where(a[1]>=0) and np.where(a[2]>=0))]
>>> b
array([[[ 3. , 4. ]],
[[ 0.5, 1. ]],
[[ 0.1, 0.2]]])
>>> b.shape
(3, 1, 2)
That is, it adds another unwanted axis to the array. What am I doing wrong?
Upvotes: 3
Views: 6377
Reputation: 15909
If what you want are fully positive columns, then @Yakym's answer is the way to go as it is probably faster. However, if it was just an example and you want to threshold certain columns, you can do it by sightly modifying your example:
>>> a[:, (a[1] >= 0) & (a[2] >= 0)]
array([[ 3. , 4. ],
[ 0.5, 1. ],
[ 0.1, 0.2]])
Here (a[1] >= 0)
and (a[2] >= 2)
create boolean mask that are merged by the &
(boolean/logical and) operator and used to index the array a
.
Upvotes: 1
Reputation: 11602
Assuming all you want to do is to remove columns that have one or more negative values, you could do this:
a = np.array([[0,1,2,3,4],[-1,-0.5,0,0.5,1],[-3,-4,-5,0.1,0.2]])
b = a[:,a.min(axis=0)>=0]
Upvotes: 8