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,
arr | The array in Python to which values will be added. This array is modified in place. |
indices | The 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. |
values | The values are to be added at the specified indices in the array 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.
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.
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.
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:
- Python NumPy Minimum
- NumPy average function in Python
- NumPy unique function in Python
- np.max function in Python NumPy
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.