Quantcast
Viewing latest article 1
Browse Latest Browse All 3

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

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 latest article 1
Browse Latest Browse All 3

Trending Articles