numpy.ufunc.at#
method
- ufunc.at(a,indices,b=None,/)#
Performs unbuffered in place operation on operand ‘a’ for elementsspecified by ‘indices’. For addition ufunc, this method is equivalent to
a[indices]+=b, except that results are accumulated for elements thatare indexed more than once. For example,a[[0,0]]+=1will onlyincrement the first element once because of buffering, whereasadd.at(a,[0,0],1)will increment the first element twice.- Parameters:
- aarray_like
The array to perform in place operation on.
- indicesarray_like or tuple
Array like index object or slice object for indexing into firstoperand. If first operand has multiple dimensions, indices can be atuple of array like index objects or slice objects.
- barray_like
Second operand for ufuncs requiring two operands. Operand must bebroadcastable over first operand after indexing or slicing.
Examples
Set items 0 and 1 to their negative values:
>>>importnumpyasnp>>>a=np.array([1,2,3,4])>>>np.negative.at(a,[0,1])>>>aarray([-1, -2, 3, 4])
Increment items 0 and 1, and increment item 2 twice:
>>>a=np.array([1,2,3,4])>>>np.add.at(a,[0,1,2,2],1)>>>aarray([2, 3, 5, 4])
Add items 0 and 1 in first array to second array,and store results in first array:
>>>a=np.array([1,2,3,4])>>>b=np.array([1,2])>>>np.add.at(a,[0,1],b)>>>aarray([2, 4, 3, 4])