# np.add.at() function in Python [3 examples]

In this NumPy tutorial, I will explain what the np.add.at() function in Python is, its syntax, parameters required, and some use cases. I will also explain what the np.add.reduce() function in Python with example.

To understand np.add.at() in Python, it is a specialized NumPy function for unbuffered, in-place addition at specific array indices, including handling repeated indices. Like, np.add.at(arr, [index1, index2], [value1, value2]) adds values at the specified indices of arr. Meanwhile, np.add.reduce() is used for reducing an array’s dimensions by summing its elements along a given axis, such as np.add.reduce(arr, axis=0) for column-wise summation in a 2D array.

The np.add.at() function in Python is a specialized method offered by NumPy. This is used to perform element-wise operations on arrays. The add.at() method provides a way to perform unbuffered in-place addition on an array at specified indices.

The basic syntax of np.add.at() function in Python is as follows:

``numpy.add.at(arr, indices, values)``

Here,

## NumPy add element to array use cases

The key feature of np.add.at() function in Python is its ability to perform in-place, unbuffered addition at specified indices. This means that if an index is repeated in indices, the corresponding value in values is added multiple times.

Let’s see some use cases of the np.add.at() function in Python:

Here, we will try to do simple addition with all the parameters of the np.add.at() function in Python.:

``````import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)``````

Output:

``[11  2 13  4 15]``

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

### 2. NumPy add at function in Python with Repeated Indices

Here, we will try to add arrays with repeated indices in Python.

``````import numpy as np

arr = np.array([1, 2, 3, 4, 5])
np.add.at(arr, [0, 0, 2, 2], [1, 2, 3, 4])
print(arr)``````

Output:

``[ 4  2 10  4  5]``

The output from running the code in PyCharm is visually represented in the screenshot below.

The numpy.add.reduce() function in Python applies the add operation repeatedly to the elements of an array, effectively reducing the array’s dimension by one.

Syntax:

``numpy.add.reduce(array, axis=0, dtype=None, out=None, keepdims=False, initial)``

For example:

``````import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print("Total Sum:", total_sum)

print("Sum along axis 0:", sum_along_axis0)

print("Sum along axis 1:", sum_along_axis1)``````

Output:

``````Total Sum: [5 7 9]
Sum along axis 0: [5 7 9]
Sum along axis 1: [ 6 15]``````

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

## Conclusion

To summarize, the np.add.at() function in Python is used for unbuffered in-place addition at specified indices of an array, useful for handling repeated indices and accumulating values. And also the np.add.reduce() function, applies addition across an array’s elements to reduce its dimension, ideal for summing values along a specified axis.

Both functions are integral for efficient numerical operations in Python.

You may also like to read: