question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Equivalence between accumarray and aggregate?

See original GitHub issue

I need to translate in Python the following Matlab instruction coming from here:

binned_data = accumarray(bins(all(bins>0,2),:),1/nrows,M(ones(1,ncols)));

I tried to replace it with

binned_data = aggregate( bins[ all(bins > 0, 1), : ], 1 / nrows, size = M * ones( (1, ncols) ) )

using all and ones from numpy, and aggregate from numpy_groupies.

However, I get the following error message:

binned_data = numpy_groupies.aggregate( bins[ numpy.all(bins > 0, 1), : ], 1 / nrows, size = M * numpy.ones( (1, ncols) ) ) File “/usr/local/lib/python3.5/dist-packages/numpy_groupies/aggregate_numpy.py”, line 288, in aggregate _impl_dict=_impl_dict, _nansqueeze=True, **kwargs) File “/usr/local/lib/python3.5/dist-packages/numpy_groupies/aggregate_numpy.py”, line 257, in _aggregate_base size=size, order=order, axis=axis) File “/usr/local/lib/python3.5/dist-packages/numpy_groupies/utils_numpy.py”, line 200, in input_validation raise TypeError(“group_idx must be of integer type”) TypeError: group_idx must be of integer type

Do you understand why?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Joeb34commented, May 3, 2019

Glad to help: glad you it working.

0reactions
cdoucetcommented, May 2, 2019

Thank you so much for your help!

Thanks to your help, I think I managed to get the same behavior as Matlab’s accumarray by modifying a little bit your code. Actually, I made a mistake in handling the first argument as it represents indices (as you suggested). The correct way of translating this argument in Python seems to be the following:

first = bins[all(bins > 0, 1), :].astype(int) first[:] = [x-1 for x in first]

As you mentioned, array indices start at 0 in python and at 1 in matlab. Therefore, it is necessary to substract 1 to indices coming from matlab. The first line is also necessary as 0’s are not expected in matlab and they do not have the same meaning as in python (see description of subs argument here).

With this modification, the size can maintained to [128, 128]. Thus, the final code is the following:

from numpy import all
from numpy import amax
from numpy import array
from numpy import loadtxt
from numpy import max
from numpy import savetxt

from numpy_groupies import aggregate

if __name__ == "__main__":
    
    # input data
    bins = loadtxt('bins.txt', delimiter='\t')
    nrows, ncols = bins.shape

    # computation of arguments
    first = bins[all(bins > 0, 1), :].astype(int) # beware 0's from matlab have not the same meaning as in python
    first[:] = [x-1 for x in first] # arrays start at 0 in python and at 1 in matlab
    second = 1 / nrows
    third = max(bins, axis =0).astype(int)

    # aggregation
    binned_data = aggregate(first.T, second, size = third)

    # functional test
    binned_data_ref = loadtxt('binned_data.txt', delimiter='\t')
    assert amax( abs( binned_data - binned_data_ref ) ) <= 1e-6
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a MATLAB accumarray equivalent in numpy?
I'm looking for a fast solution to MATLAB's accumarray in numpy. The accumarray accumulates the elements of an array which belong to the ......
Read more >
Under-appreciated accumarray » Loren on the Art of MATLAB
The MATLAB function accumarray seems to be under-appreciated. accumarray allows you to aggregate items in an array in the way that you specify....
Read more >
Accumarray Examples - Fan Wang
Accumarry Basic Example. There are three unique values in ar_a, sum up the probabilities for each of the unique states. This is equivalent....
Read more >
porting Python fn to MATLAB
user-specified aggregate function of each of these groups, and returns ... "aggregate" function to each of these equivalence classes.
Read more >
help with understanding the use of accumarray() : r/matlab
%The subs mapping tells accumarray where to aggregate the data. Each row corresponds to a single element of val >subs=[1 2; % val(1)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found