I want to accumulate elements of a vector (x) to an array (f) based on an index list (ind). For example: x=[1,2,3,4,5,6] ind=[1,3,9,3,4,1] f=np.zeros(10) What I want would be produced by the loop for i=range(6): f[ind[i]]=f[ind[i]]+x[i] The answer is f=array([ 0., 7., 0., 6., 5., 0., 0., 0., 0., 3.]) When I try to use implicit arguments f[ind]=f[ind]+x I get f=array([ 0., 6., 0., 4., 5., 0., 0., 0., 0., 3.]) So it takes the last value of x that is pointed to by ind and adds it to f, but its the wrong answer when there are repeats of the same entry in ind (e.g. 3 or 1) I realize my code is incorrect, but is there a way to make numpy accumulate without using loops? I would have thought so but I cannot find anything in the documentation. Would much appreciate any help  probably a really simple question. Thanks Tony  Tony Ladd Chemical Engineering Department University of Florida Gainesville, Florida 326116005 USA Email: tladd"(AT)"che.ufl.edu Web http://ladd.che.ufl.edu Tel: (352)3926509 FAX: (352)3929514
On Tue, Feb 19, 2013 at 10:00 AM, Tony Ladd
I want to accumulate elements of a vector (x) to an array (f) based on an index list (ind).
For example:
x=[1,2,3,4,5,6] ind=[1,3,9,3,4,1] f=np.zeros(10)
What I want would be produced by the loop
for i=range(6): f[ind[i]]=f[ind[i]]+x[i]
The answer is f=array([ 0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])
When I try to use implicit arguments
f[ind]=f[ind]+x
I get f=array([ 0., 6., 0., 4., 5., 0., 0., 0., 0., 3.])
So it takes the last value of x that is pointed to by ind and adds it to f, but its the wrong answer when there are repeats of the same entry in ind (e.g. 3 or 1)
I realize my code is incorrect, but is there a way to make numpy accumulate without using loops? I would have thought so but I cannot find anything in the documentation.
Would much appreciate any help  probably a really simple question.
Thanks
Tony
I believe you are looking for the equivalent of accumarray in Matlab? Try this: http://www.scipy.org/Cookbook/AccumarrayLike It is a bit touchy about lists and 1D numpy arrays, but it does the job. Also, I think somebody posted an optimized version for simple sums recently to this list. Cheers! Ben Root
Thanks to all for a very quick response. np.bincount does what I need. Tony On 02/19/2013 10:04 AM, Benjamin Root wrote:
On Tue, Feb 19, 2013 at 10:00 AM, Tony Ladd
mailto:tladd@che.ufl.edu> wrote: I want to accumulate elements of a vector (x) to an array (f) based on an index list (ind).
For example:
x=[1,2,3,4,5,6] ind=[1,3,9,3,4,1] f=np.zeros(10)
What I want would be produced by the loop
for i=range(6): f[ind[i]]=f[ind[i]]+x[i]
The answer is f=array([ 0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])
When I try to use implicit arguments
f[ind]=f[ind]+x
I get f=array([ 0., 6., 0., 4., 5., 0., 0., 0., 0., 3.])
So it takes the last value of x that is pointed to by ind and adds it to f, but its the wrong answer when there are repeats of the same entry in ind (e.g. 3 or 1)
I realize my code is incorrect, but is there a way to make numpy accumulate without using loops? I would have thought so but I cannot find anything in the documentation.
Would much appreciate any help  probably a really simple question.
Thanks
Tony
I believe you are looking for the equivalent of accumarray in Matlab?
Try this:
http://www.scipy.org/Cookbook/AccumarrayLike
It is a bit touchy about lists and 1D numpy arrays, but it does the job. Also, I think somebody posted an optimized version for simple sums recently to this list.
Cheers! Ben Root
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
 Tony Ladd Chemical Engineering Department University of Florida Gainesville, Florida 326116005 USA Email: tladd"(AT)"che.ufl.edu Web http://ladd.che.ufl.edu Tel: (352)3926509 FAX: (352)3929514
Dear Tony,
I would suggest to look at this post already mentioned by Benjamin .....
maybe it fits with your needs!
http://numpydiscussion.10968.n7.nabble.com/Preallocatearraytd4870.html
Cheers,
Eraldo
On Tue, Feb 19, 2013 at 4:24 PM, Tony Ladd
Thanks to all for a very quick response. np.bincount does what I need.
Tony
On 02/19/2013 10:04 AM, Benjamin Root wrote:
On Tue, Feb 19, 2013 at 10:00 AM, Tony Ladd
mailto:tladd@che.ufl.edu> wrote: I want to accumulate elements of a vector (x) to an array (f) based
on
an index list (ind).
For example:
x=[1,2,3,4,5,6] ind=[1,3,9,3,4,1] f=np.zeros(10)
What I want would be produced by the loop
for i=range(6): f[ind[i]]=f[ind[i]]+x[i]
The answer is f=array([ 0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])
When I try to use implicit arguments
f[ind]=f[ind]+x
I get f=array([ 0., 6., 0., 4., 5., 0., 0., 0., 0., 3.])
So it takes the last value of x that is pointed to by ind and adds it to f, but its the wrong answer when there are repeats of the same entry in ind (e.g. 3 or 1)
I realize my code is incorrect, but is there a way to make numpy accumulate without using loops? I would have thought so but I cannot find anything in the documentation.
Would much appreciate any help  probably a really simple question.
Thanks
Tony
I believe you are looking for the equivalent of accumarray in Matlab?
Try this:
http://www.scipy.org/Cookbook/AccumarrayLike
It is a bit touchy about lists and 1D numpy arrays, but it does the job. Also, I think somebody posted an optimized version for simple sums recently to this list.
Cheers! Ben Root
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
 Tony Ladd
Chemical Engineering Department University of Florida Gainesville, Florida 326116005 USA
Email: tladd"(AT)"che.ufl.edu Web http://ladd.che.ufl.edu
Tel: (352)3926509 FAX: (352)3929514
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
On Tue, 20130219 at 10:00 0500, Tony Ladd wrote:
I want to accumulate elements of a vector (x) to an array (f) based on an index list (ind).
For example:
x=[1,2,3,4,5,6] ind=[1,3,9,3,4,1] f=np.zeros(10)
What I want would be produced by the loop
for i=range(6): f[ind[i]]=f[ind[i]]+x[i]
The answer is f=array([ 0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])
When I try to use implicit arguments
f[ind]=f[ind]+x
I get f=array([ 0., 6., 0., 4., 5., 0., 0., 0., 0., 3.])
So it takes the last value of x that is pointed to by ind and adds it to f, but its the wrong answer when there are repeats of the same entry in ind (e.g. 3 or 1)
I realize my code is incorrect, but is there a way to make numpy accumulate without using loops? I would have thought so but I cannot find anything in the documentation.
You might be interested in this: https://github.com/numpy/numpy/pull/2821 But anyway, you should however be able to do what you want to do using np.bincount with the weights keyword argument. Regards, Sebastian
Would much appreciate any help  probably a really simple question.
Thanks
Tony
participants (4)

Benjamin Root

Eraldo Pomponi

Sebastian Berg

Tony Ladd