NumPy Array Addition

NumPy add() is a mathematical function and is used to calculate the addition between two NumPy arrays. This function adds given arrays element-wise. The add() function returns a scalar or nd-array. If shapes of two arrays are not same, that is arr.shape!=arr1.shape, they must be broadcastable to a common shape. In this article, I will explain how to use the NumPy add() function with examples.

1. Quick Examples of Array Addition.

If you are in a hurry, below are some quick examples of NumPy array addition.


# Below are a quick examples

# Example 1: Get addition values using add()
arr = [2, 4, 6, 8, 12, 14] 
arr2 = np.add(arr, 12)

# Example 2: Use numpy.add() function to add two numbers
arr = 12
arr1 = 25
arr2 = np.add(arr, arr1)  

# Example 3: Use numpy add() function with two input arrays
arr = np.array([2, 6, 9])
arr1 = np.array([5, 8, 12])
arr2 = np.add(arr, arr1) 
 
# Example 4: Use numpy.ndarray.add() function
arr2 = arr.__add__(arr1)

# Example 5: Use + operator
arr2 = arr + arr1   

# Example 6: add numpy two multi-dimensional arrays
arr = [[18, 25, 37], [5, -7, 15]]
arr1 = [[4, 8, 12], [-13, 24, 17]]
arr2 = np.add(arr, arr1)  

2. Syntax of NumPy add() Function

Following is the syntax of the numpy.add() function.


# Syntax of numpy.add()
numpy.add(arr, arr1, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = 

2.1 Parameters of add()

Following are the parameters of the add() function.

  • arr – The first input array or object works as an addition.
  • arr1 – Second input array or object which works as an addition.
  • out – It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple must have a length equal to the number of outputs.
  • where – This condition is broadcast over the input. True value means to calculate the unfunc at that position, whereas the False indicates to leave the value in the output alone.
  • **kwargs – Allows passing keyword variable length of argument to a function. Used when we want to handle a named argument in a function.
  • order – {‘C’, ‘F’, ‘A’, ‘K’}, optional: ‘C’: means to flatten in row-major using C-style order. ‘F’: means to flatten in column-major (Fortran- style) order. ‘A’: means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. K’: means to flatten an in the order the elements occur in memory. By default, the ‘C’ index order is used.
  • dtype – It is an optional parameter that specifies the data type of the returned array.

2.2 Return Value of NumPy add()

It returns nd-array. If both arr and arr1 are scalars then it returns a scalar.

3. Usage of NumPy add() Function

The numpy.add() is a mathematical function and is used to calculate the addition between two NumPy arrays. It will return the nd-array.

4. Add NumPy Array by scalar (Single Value)

We can add the array with a scalar value for that, we have to take an array named arr and the scalar value is 12 then we will pass the array and scalar value as an argument in numpy.add() function. It will return the nd-array.


import numpy as np

# creating an 1D input array
arr = [2, 4, 6, 8, 12, 14] 

# Get addition values using add()
arr2 = np.add(arr, 12)
print(arr2)

# Output
# [14 16 18 20 24 26]

5. Use numpy.add() Function To Add Two Numbers

If either arr or arr1 is 0-D(scalar) then numpy.add(arr,arr1) is equivalent to adding two numbers (a +b).


# creating two numbers
arr = 12
arr1 = 25

# Use numpy.add() function to add two numbers
arr2 = np.add(arr, arr1)  
print(arr2)

# Output
# 37

6. Use NumPy add() Function with Two Input Arrays

Add our two NumPy arrays using numpy.add() function. It returns the addition of two arrays.


# Create NumPy 2-D array
arr = np.array([2, 6, 9])
arr1 = np.array([5, 8, 12])

# Use numpy add() function with two input arrays
arr2 = np.add(arr, arr1)  
print(arr2)

# Output
# [ 7 14 21]

7. Use numpy.ndarray.add() Function

We can use numpy.ndarray.add() function is used to add some value to every element of the array. We can use it to perform vector addition by passing the second array to this function.


# Use numpy.ndarray.add() function
arr2 = arr.__add__(arr1) 
print(arr2)

Yields the same output as above.

8. Use + Operator

We can also use the arithmetic + operator to calculate the sum of two arrays. The result is the same as the above.


# Use + operator
arr2 = arr + arr1  
print(arr2)

9. Add NumPy Two Multi-Dimensional Arrays

Using the add() function we can add the two multi-dimensional arrays elementwise. The resultant array is the same shape as the input arrays.


# creating two multi-dimensional input array
arr = [[18, 25, 37], [5, -7, 15]]
arr1 = [[4, 8, 12], [-13, 24, 17]]

# add numpy two multi-dimensional arrays
arr2 = np.add(arr, arr1)  
print(arr2)

# Output
# [[22 33 49]
#  [-8 17 32]]

Conclusion

In this article, I have explained how to use numpy.add() function and using how to calculate the addition of NumPy array.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing NumPy Array Addition