[Numpy-discussion] numpy all unexpected result (generator)
Tue Jan 31 08:07:31 CST 2012
On Tue, Jan 31, 2012 at 13:26, Neal Becker <firstname.lastname@example.org> wrote:
> I was just bitten by this unexpected behavior:
> In : all ([i> 0 for i in xrange (10)])
> Out: False
> In : all (i> 0 for i in xrange (10))
> Out: True
> Turns out:
> In : all is numpy.all
> Out: True
> So numpy.all doesn't seem to do what I would expect when given a generator.
Expected behavior. numpy.all(), like nearly all numpy functions,
converts the input to an array using numpy.asarray(). numpy.asarray()
knows nothing special about generators and other iterables that are
not sequences, so it thinks it's a single scalar object. This scalar
object happens to have a __nonzero__() method that returns True like
most Python objects that don't override this.
In order to use generic iterators that are not sequences, you need to
explicitly use numpy.fromiter() to convert them to ndarrays. asarray()
and array() can't do it in general because they need to autodiscover
the shape and dtype all at the same time.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion