# [Numpy-discussion] Histograms via indirect index arrays

Piotr Luszczek luszczek at cs.utk.edu
Fri Mar 17 06:44:02 CST 2006

On Friday 17 March 2006 03:59, Travis Oliphant wrote:
> > Hey,
> >
> > First of all, thanks for the new release.
> >
> > Here's another question regarding something I cannot quite
> > understand:
> >
> > Suppose you want to update bins for a histogram, you might think
> > you could do something like:
> >
> >   g = zeros(4,Int)
> >   x = array([0.2, 0.2])
> >   idx = floor(x/0.1).astype(int)
> >   g[idx] += 1
> >
> > Here idx becomes
> >
> >    array([2, 2])
> >
> > In this case, I would naively expect g to end up like
> >
> >   array([0, 0, 2, 0])                     (1)
> >
> > but instead you get
> >
> >   array([0, 0, 1, 0])                     (2)
> >
> > Is this intended? Just being plain novice-like naive, I would
> > expect the slice operation g[idx] += 1 to do something like
>
> Yes, this is intended (sort of --- this particular example isn't the
> reason for the behavior though).
>
> The issue is that the code g[idx] +=1 is equivalent in Python to
>
> g[idx] = g[idx] + 1

This is not what I read at:

http://docs.python.org/ref/numeric-types.html

Quote:

These methods should attempt to do the operation in-place (modifying
self) and return the result (which could be, but does not have to be,
self).

What you describe is "lack of attempt" in which case the "fall back"
behavior gets triggered.

> Then g[idx] returns array([0,0]) as it should.  This new copy of the
> array data then gets added to 1 resulting in array([1,1]).  This
> array is then set into element 2 of g twice just as if you had done
>
> g[2] = 1
> g[2] = 1
>
> Then end effect is you get a 1 out of the result.
>
> Perhaps a little counter to the way you were thinking of the problem,
> but very much how it must be due to the way Python translates the
> statement g[idx] += 1 in this case.
>
> There are, of course, other ways to do what you want to do.

Histograms are just the trivial instantiation of the problem (the
problem being unintuitive behavior at least for Mads and me).

If in-place arithmetic didn't make copies it would land its nicely
to sparse matrix computations. Efficient, compact and in Python.
And there are also other histogram-like computations but do not
use '+' but other operators.

Piotr

> -Travis
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language that extends applications into web and mobile media. Attend
> the live webcast and join the prime developer group breaking into
> this new coding territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121
>642 _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion