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.

np.add.at() function in Python

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.

NumPy add.at() function’s syntax

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

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

np.add.at() function’s parameter

Here,

arrThe array in Python to which values will be added. This array is modified in place.
indicesThe locations in the Python array where values should be added. This can be a single integer, a list of integers, or a tuple of integer arrays.
valuesThe values are to be added at the specified indices in the array in Python.
List of parameters required in the np.add.at() function in Python.

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:

1. np.add.at in Python for Simple Addition

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])
np.add.at(arr, [0, 2, 4], 10)
print(arr)

Output:

[11  2 13  4 15]

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

np.add.at() function in Python

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.

numpy add two arrays using np add at in Python

np.add.reduce() function in Python

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]])

total_sum = np.add.reduce(arr)
print("Total Sum:", total_sum)

sum_along_axis0 = np.add.reduce(arr, axis=0)
print("Sum along axis 0:", sum_along_axis0)

sum_along_axis1 = np.add.reduce(arr, axis=1)
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.

add two numpy arrays in Python

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: