Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

python - How do I boolean mask an array using chained comparisons?

How can I filter a numpy array using a pair of inequalities, such as:

>>> a = np.arange(10)
>>> a[a <= 6]
array([0, 1, 2, 3, 4, 5, 6])
>>> a[3 < a]
array([4, 5, 6, 7, 8, 9])
>>>
>>> a[3 < a <= 6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous.
 Use a.any() or a.all()

I get the same response if I try a.all(3 < a <= 6)

np.array([x for x in a if 3 < x <= 6]) works, but it seems nasty. What's the right way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to do:

a[(3 < a) & (a <= 6)]

It's a "wart" in python. In python (3 < a <=6) is translated to ((3 < a) and (a <= 6)). However numpy arrays don't work with the and operation because python doesn't allow overloading of the and and or operators. Because of that numpy uses & and |. There was some discussion about fixing this about a year ago, but I haven't seem much about it since.

http://mail.python.org/pipermail/python-dev/2012-March/117510.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...