[Numpy-discussion] Smart way to do this?
Nathaniel Smith
njs@pobox....
Sat Feb 23 13:22:42 CST 2013
On Sat, Feb 23, 2013 at 4:51 PM, Jose Amoreira <ljmamoreira@gmail.com> wrote:
> On Saturday, February 23, 2013 00:45:55 Brett Olsen wrote:
>
>> a = np.ones(30)
>
>> idx = np.array([2, 3, 2])
>
>> a += 2 * np.bincount(idx, minlength=len(a))
>
>>
>
>> >>> a
>
>>
>
>> array([ 1., 1., 5., 3., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
>
>> 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
>
>> 1., 1., 1., 1.])
>
>>
>
>
>
>
>
> Hi!
>
> OK, but is there any reason why Santhu's first option doesn't work?
> Shouldn't it work?
It can't -- python limitation, unfixable by numpy. The += gets divided
into two operations, and there's no way for numpy doing the "+" part
to "see" that the two locations you are adding the value to are really
"the same" location.
There's a pull request to add a new ufunc method to solve this:
https://github.com/numpy/numpy/pull/2821
So if/when that gets merged you'll be able to do:
np.add.at(a, idx, 2)
But it still needs some tweaking and has been stalled for a bit, so
might be a good opportunity for someone to take on if they're
interested in seeing this functionality...
-n
More information about the NumPy-Discussion
mailing list