Numpy add() Function:
The numpy add function computes the sum of the two arrays. It computes the addition of two arrays, say l1 and l2, element by element. The numpy.add() function is a universal function, which means that it accepts several parameters that allow you to optimize its work based on the algorithm’s specifics.
Syntax:
numpy.add(l1, l2, out=None)
Parameters:
l1 and l2: (Required)
Required. The arrays that will be added must be specified. They must be broadcastable to a common shape if l1.shape!= l2.shape
out:
This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
Return Value:
The addition between l1 and l2 is returned by the add function. The nd-add() array’s method can be a scalar. It is determined by the l1 and l2 variables. Numpy is used if l1 and l2 are both scalar. A scalar value will be returned by add(). Otherwise, an nd-array will be returned.
NumPy add() Function in Python
Approach:
- Import NumPy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Create some sample arrays of different shapes to test the add() function.
- Pass the first array and some random number to add() function of NumPy module and print the result.
- Here it adds the second argument for each element of the array.
- Print the result array.
- Pass the first array and second array to add() function of NumPy module and print the result.
- Here it adds the second array elements for the first array element.
- Print the result array.
- Similarly, test it with other arrays of different shapes.
- The Exit of the Program.
Below is the implementation:
# Import NumPy module using the import keyword. import numpy as np # Pass some random list as an argument to the array() function to create an array. # Store it in a variable. arry1 = np.array([[30, 60], [90, 120], [150, 180]]) # Create some sample arrays of different shapes to test the add() function. arry2 = np.array([[10, 20]]) arry3 = np.array([[100], [200], [300]]) arry4 = np.array([[55, 65], [75, 85], [95, 105]]) # Pass the first array and some random number to add() function of NumPy module and print the result. # Here it adds the second argument for each element of the array. print('Adding random number say 10 to the first array : ') print('Adding 10 to first array gives :\n', np.add(arry1, 10)) # Print the result array. # Pass the first array and second array to add() function of NumPy module and print the result. # Here it adds the second array elements for the first array element. # Print the result array. print('Adding first array and second array : ') print(np.add(arry1, arry2)) # Similarly, test it with other arrays of different shapes. print('Adding first array and third array : ') print(np.add(arry1, arry3)) print('Adding first array and fourth array : ') print(np.add(arry1, arry4))
Output:
Adding random number say 3 to the first array : Adding 10 to first array gives : [[ 40 70] [100 130] [160 190]] Adding first array and second array : [[ 40 80] [100 140] [160 200]] Adding first array and third array : [[130 160] [290 320] [450 480]] Adding first array and fourth array : [[ 85 125] [165 205] [245 285]]