Equivalence between accumarray and aggregate?
See original GitHub issueI 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:
- Created 4 years ago
- Comments:9
Glad to help: glad you it working.
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: