Quantcast
Channel: Filter numpy array by two conditions. More than one element is ambiguous - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by mgilson for Filter numpy array by two conditions. More than one element is ambiguous

$
0
0

python's and looks at the boolean value on either side of the condition. Because of design decisions in numpy, arrays with more than 1 value don't have a boolean value (it raises ValueError as you've seen. The solution is to use the np.logical_and function.

mask = np.logical_and(data[:, 0] == ptype, data[:, 8] <= radius)np.sum(data[mask, 7])

Note that & will work as well in this case as you have arrays of booleans -- However, I don't like to use that one in general as typically (and with numpy as well), & means bitwise and rather than logical and.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>