[Numpy-discussion] np.bincount() won't accept list or array from np.random.beta and acts like len
Sat Aug 9 15:40:32 CDT 2008
On Sat, Aug 9, 2008 at 15:19, Michael <firstname.lastname@example.org> wrote:
> Hi list,
> I have found np.bincount() not to behave as expected when provided with
> data from np.random.beta.
As the docstring states, bincount() only works on sequences of
non-negative integers, not floats. beta() returns floats.
> It works fine with lists, but not the type returned by np.random.beta...
> there seems to be a block on casting too. When it does give output on
> np.array it just reports the number of items, like len()
beta() yields float numbers in the range [0..1]. When you give
bincount() a list of such floats, it makes an integer array out of
them without warning (when you give it an array, it can easily check
the type and prevent you from giving it floats). Thus you get an array
of all 0s since casting to integers truncates to the next lowest
integer. bincount() will give you an array of counts such that
counts is the number of 0s in the input; in this case, that is the
length of the input.
> x = np.array([1,1,1,1,2,2,4,4,5,6,6,6])
> print np.bincount(x)
> prior = np.random.beta(2,7,1000)
> a = prior.tolist()
> print type(a)
> print np.bincount(a)
> b = [ round(i,2) for i in a ]
> print b; print type(b)
> print np.bincount(b)
This still doesn't work because round() gives you floats.
> #c = np.array(a)
> #print np.bincount(c); print type(c)
> i know its easily solved but bincount is probably faster than making a
> pass over the probabilities and hashing them as strings etc
You need to convert them to integers first with digitize(). Or just
use histogram(), which does all this for you.
"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