[Numpy-discussion] bincount question

Friedrich Romstedt friedrichromstedt at gmail.com
Sat Jan 8 16:32:48 EST 2011


2010/12/21 Alan G Isaac <alan.isaac at gmail.com>:
> ::
>
>     >>> np.bincount([])
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     ValueError: The first argument cannot be empty.
>
> Why not?
> (I.e., why isn't an empty array the right answer?)

>From the (i.e. "a", or, even more precise, "my") mathematical pov:

Define the "bincount" sequence, which will mostly consist of trailing
zeros for large indices.  Then, the return value is the smallest
sequence, s.t. there are no non-zero items left outside the return
chunk of the sequence, and of course it must include the zeroth
bincount sequence element.

So, yes, [] would be the correct answer.

>From the algorithmic point of view:

Define the length of the sequence returned by the max() of the array
handed in + 1.  So, since max([]) is undefined, such is bincount in
that case.

I'm a bit in favour of the mathematical approach.  But unfortunately,
I cannot fix it, although I think it will break nothing because
nothing should rely on this corner case yielding an Exception (but I
might be proven wrong, I don't really know).

In any case, it might be worth documenting this, by adding it to the
ValueError section of the "Raises" part
(http://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html?highlight=bincount#numpy.bincount).
 This is something I might be able to do.

Friedrich



More information about the NumPy-Discussion mailing list