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
248 views
in Technique[技术] by (71.8m points)

python - Why do Numpy.all() and any() give wrong results if you use generator expressions?

Working with somebody else's code I stumbled across this gotcha. So what is the explanation for numpy's behavior?

In [1]: import numpy as np

In [2]: foo = [False, False]

In [3]: print np.any(x == True for x in foo)
True  # <- bad numpy!

In [4]: print np.all(x == True for x in foo)
True  # <- bad numpy!

In [5]: print np.all(foo)
False  # <- correct result

p.s. I got the list comprehension code from here: Check if list contains only item x

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

np.any and np.all don't work on generators. They need sequences. When given a non-sequence, they treat this as any other object and call bool on it (or do something equivalent), which will return True:

>>> false = [False]
>>> np.array(x for x in false)
array(<generator object <genexpr> at 0x31193c0>, dtype=object)
>>> bool(x for x in false)
True

List comprehensions work, though:

>>> np.all([x for x in false])
False
>>> np.any([x for x in false])
False

I advise using Python's built-in any and all when generators are expected, since they are typically faster than using NumPy and list comprehensions (because of a double conversion, first to list, then to array).


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

...