![](https://secure.gravatar.com/avatar/73887ba4b19ace28921b1851f89c957a.jpg?s=120&d=mm&r=g)
Hi, Does anyone know what is the equivalent function of MATLAB's "accumarray()" in scipy/numpy, with the exact same functionality ? Thanks
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Fri, Mar 26, 2010 at 10:39 AM, Mohammad Abdollahi <m.abdollahi@gmail.com> wrote:
Hi,
Does anyone know what is the equivalent function of MATLAB's "accumarray()" in scipy/numpy, with the exact same functionality ?
A description or link would be helpful. I never used accumarray http://www.mathworks.com/access/helpdesk/help/techdoc/ref/accumarray.html but it looks similar to np.bincount(labels, weights=values) I don't know what the equivalent options are? Josef
Thanks
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/39916bae984cb93b797efd2b175f59c0.jpg?s=120&d=mm&r=g)
On 3/26/2010 10:39 AM, Mohammad Abdollahi wrote:
Does anyone know what is the equivalent function of MATLAB's "accumarray()" in scipy/numpy, with the exact same functionality ?
That certainly gets the basic accumarray functionality: >>> vals = 101+np.arange(5) >>> subs = np.array([1,2,4,2,4])-1 >>> np.bincount(subs,vals) array([ 101., 206., 0., 208.]) Here's another way that maybe easier to adapt to whatever the OP needs: >>> from collections import defaultdict >>> d = defaultdict(float) >>> for i, cat in enumerate(subs): ... d[cat] += vals[i] ... >>> d defaultdict(<type 'float'>, {0: 101.0, 1: 206.0, 3: 208.0}) That should actually be pretty fast, though not as fast as bincount of course. What's the use case? hth, Alan Isaac
![](https://secure.gravatar.com/avatar/73887ba4b19ace28921b1851f89c957a.jpg?s=120&d=mm&r=g)
thank you for your replys, but both of these methods are for the case when the subs argument is a vector. but apparently they can not be used when subs is a matrix. i.e. the output of the function is meant to be a matrix too. right ? On Fri, Mar 26, 2010 at 4:25 PM, Alan G Isaac <aisaac@american.edu> wrote:
On 3/26/2010 10:39 AM, Mohammad Abdollahi wrote:
Does anyone know what is the equivalent function of MATLAB's "accumarray()" in scipy/numpy, with the exact same functionality ?
That certainly gets the basic accumarray functionality:
>>> vals = 101+np.arange(5) >>> subs = np.array([1,2,4,2,4])-1 >>> np.bincount(subs,vals) array([ 101., 206., 0., 208.])
Here's another way that maybe easier to adapt to whatever the OP needs:
>>> from collections import defaultdict >>> d = defaultdict(float) >>> for i, cat in enumerate(subs): ... d[cat] += vals[i] ... >>> d defaultdict(<type 'float'>, {0: 101.0, 1: 206.0, 3: 208.0})
That should actually be pretty fast, though not as fast as bincount of course.
What's the use case?
hth, Alan Isaac
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/da4564c0cd535285f53473571cf285d9.jpg?s=120&d=mm&r=g)
On Wed, Mar 31, 2010 at 12:28, Mohammad Abdollahi <m.abdollahi@gmail.com> wrote:
but apparently they can not be used when subs is a matrix. i.e. the output of the function is meant to be a matrix too. right ?
Can you please explain what you wanna do? Maybe there is another function that does what you want or it is done in another way. Numpy is not a 1:1 matlab clone! greetings harald
![](https://secure.gravatar.com/avatar/73887ba4b19ace28921b1851f89c957a.jpg?s=120&d=mm&r=g)
Dera Herlad Thank you for your reply. so what i'm trying to do is to create a re-assigned spectrogram. which means that i have a 2-d matrix representing my weighted spectrogram that i want to operate on this matrix in a way that the value of each element in matrix will be reassigned based on the value of the neighbouring elemnts of it. (i.e the sum of the values of some neighbours). for that purpose i was using accumarray in matlab which is very efficient that the trivial way of using for loops that comes to mind first. and the way i used the accumarray in matlab was sth like this : (very simple example)
subs = [1:6;6:-1:1]
subs = 1 2 3 4 5 6 6 5 4 3 2 1
val = 10:15
val = 10 11 12 13 14 15
accumarray(subs',val)
ans = 0 0 0 0 0 10 0 0 0 0 11 0 0 0 0 12 0 0 0 0 13 0 0 0 0 14 0 0 0 0 15 0 0 0 0 0 regards Mohammad On Wed, Mar 31, 2010 at 12:33 PM, Harald Schilly <harald.schilly@gmail.com>wrote:
On Wed, Mar 31, 2010 at 12:28, Mohammad Abdollahi <m.abdollahi@gmail.com> wrote:
but apparently they can not be used when subs is a matrix. i.e. the output of the function is meant to be a matrix too. right ?
Can you please explain what you wanna do? Maybe there is another function that does what you want or it is done in another way. Numpy is not a 1:1 matlab clone!
greetings harald _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/b80364cc6fbb2eac7dcfdb7738bae083.jpg?s=120&d=mm&r=g)
On Mar 31, 2010, at 7:20 AM, Mohammad Abdollahi wrote:
Dera Herlad
Thank you for your reply. so what i'm trying to do is to create a re-assigned spectrogram. which means that i have a 2-d matrix representing my weighted spectrogram that i want to operate on this matrix in a way that the value of each element in matrix will be reassigned based on the value of the neighbouring elemnts of it. (i.e the sum of the values of some neighbours). for that purpose i was using accumarray in matlab which is very efficient that the trivial way of using for loops that comes to mind first. and the way i used the accumarray in matlab was sth like this : (very simple example)
subs = [1:6;6:-1:1]
subs =
1 2 3 4 5 6 6 5 4 3 2 1
val = 10:15
val =
10 11 12 13 14 15
accumarray(subs',val)
ans =
0 0 0 0 0 10 0 0 0 0 11 0 0 0 0 12 0 0 0 0 13 0 0 0 0 14 0 0 0 0 15 0 0 0 0 0
Hey Mohammad, You can reproduce the above example with the following numpy code:
import numpy as np
def accumarray(idx, val): num_rows, num_cols = idx.max(axis=1) + 1 new_array = np.zeros((num_rows, num_cols)) new_array[tuple(idx)] = val return new_array
idx = np.array([np.arange(6), np.arange(6)[::-1]]) val = np.arange(10,16) print accumarray(idx, val)
Your actual use of accumarray may not be satisfied by the above, but it shouldn't be too difficult to modify it to suit your needs. Cheers, -Tony
![](https://secure.gravatar.com/avatar/b0f62d137f9ea1d0b6cc4e7e6f61b119.jpg?s=120&d=mm&r=g)
I've added an accumarray-like function to the Cookbook: http://www.scipy.org/Cookbook/AccumarrayLike Warren Tony S Yu wrote:
On Mar 31, 2010, at 7:20 AM, Mohammad Abdollahi wrote:
Dera Herlad
Thank you for your reply. so what i'm trying to do is to create a re-assigned spectrogram. which means that i have a 2-d matrix representing my weighted spectrogram that i want to operate on this matrix in a way that the value of each element in matrix will be reassigned based on the value of the neighbouring elemnts of it. (i.e the sum of the values of some neighbours). for that purpose i was using accumarray in matlab which is very efficient that the trivial way of using for loops that comes to mind first. and the way i used the accumarray in matlab was sth like this : (very simple example)
subs = [1:6;6:-1:1]
subs =
1 2 3 4 5 6 6 5 4 3 2 1
val = 10:15
val =
10 11 12 13 14 15
accumarray(subs',val)
ans =
0 0 0 0 0 10 0 0 0 0 11 0 0 0 0 12 0 0 0 0 13 0 0 0 0 14 0 0 0 0 15 0 0 0 0 0
Hey Mohammad,
You can reproduce the above example with the following numpy code:
import numpy as np
def accumarray(idx, val): num_rows, num_cols = idx.max(axis=1) + 1 new_array = np.zeros((num_rows, num_cols)) new_array[tuple(idx)] = val return new_array
idx = np.array([np.arange(6), np.arange(6)[::-1]]) val = np.arange(10,16) print accumarray(idx, val)
Your actual use of accumarray may not be satisfied by the above, but it shouldn't be too difficult to modify it to suit your needs.
Cheers, -Tony
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/32d0b01c2c9ff4abdad2e0f3f0041400.jpg?s=120&d=mm&r=g)
Hi all, Warren Weckesser <warren.weckesser <at> enthought.com> writes:
I've added an accumarray-like function to the Cookbook:
http://www.scipy.org/Cookbook/AccumarrayLike
Warren
Excellent accumulator! Would it be possible to add support for sparse arrays as well? Along the lines: def accum(..., dtype=None): => def accum(..., dtype=None, sparse= False): # Create the output array. out= np.empty(size, dtype=dtype) ... => # Create the output array. if sparse: out= sparse(size, dtype=dtype) # just an idea, caller may provide more details ... else: out= np.empty(size, dtype=dtype) ... Regards, eat
participants (7)
-
Alan G Isaac
-
eat
-
Harald Schilly
-
josef.pktd@gmail.com
-
Mohammad Abdollahi
-
Tony S Yu
-
Warren Weckesser