Python NumPy Array Operations

Python NumPy array operations are used to add(), substract(), multiply() and divide() two arrays. Python has a wide range of standard arithmetic operations, these help to perform normal functions of addition, subtraction, multiplication, and division. The arithmetic operations take a minimum of two arrays as input and these arrays must be either of the same shape or should conform to array broadcasting rules.

In this article, I will explain various functions to perform arithmetic operations on NumPy arrays with examples.

1. Quick Examples of Python NumPy Array Operations

If you are in a hurry, below are some quick examples of how to Python NumPy Array Operations.


# Below are the quick examples

# Example 1: Adding the two arrays
arr2 = np.add(arr, arr1)
print(arr2)

# Example 2: Subtracting the two arrays
arr2 = np.subtract(arr, arr1)
print(arr2)
  
# Example 3: Multiplying the two arrays
arr2 = np.multiply(arr, arr1)
print(arr2)
  
# Example 4: Dividing the two arrays
arr2 = np.divide(arr, arr1)
print(arr2)

# Example 5: Use numpy.power() to calculating exponents of an array of numbers
arr = np.array([5, 3, 6, 9, 2, 4]) 
arr2 = np.power(arr,3)

# Example 6: Use numpy.power() with two arrays
arr = [2, 4, 6, 5, 3]
arr1 = [2, 3, 5, 4, 1]
arr2 = np.power(arr,arr1)

# Example 7: Use numpy.reciprocal() function
arr = np.array([50, 1.34, 3, 1, 25]) 
arr2 = np.reciprocal(arr)

# Example 8: Use reciprocal function
arr = np.array([75], dtype = int) 
arr2 = np.reciprocal(arr) 

# Example 9: Use numpy.mod() function
arr = np.array([7,16, 25]) 
arr1 = np.array([4,8,6]) 
arr2 = np.mod(arr,arr1) 

# Example 10: Use numpy.remainder() function
arr = np.array([7,16, 25]) 
arr1 = np.array([4,8,6]) 
arr2 = np.remainder(arr,arr1) 

2. Arithmetic Operations on NumPy array

Python contains a wide range of standard arithmetic operations. These operations help to perform normal functions like addition, subtraction, multiplication, and division such as, add(), subtract(), multiply(), and divide() respectively. The input arrays to these operations must be either of the same shapes or should conform to array broadcasting rules. In order to apply the arithmetic operations on the NumPy array, we have to initialize the array. Let’s create the NumPy array.


import numpy as np 
  
# Initializing the array
arr = np.arange(12, dtype = np.float_).reshape(3, 4) 
arr1 = np.array([6, 12, 15, 18]) 
print(arr)
print(arr1)

# Output of arr:
# [[ 0.  1.  2.  3.]
#  [ 4.  5.  6.  7.]
# [ 8.  9. 10. 11.]]

# Output of arr1:
# [ 6 12 15 18]

2.1 Adding Two Arrays

numpy.add() function is used to return the addition of two arrays element-wise. If the shape of two arrays is not the same, that is arr1.shape != arr2.shape, they must be broadcastable to a common shape (which may be the shape of one or the other).

Following is the syntax of the numpy.add()


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

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


# Add two arrays
arr2 = np.add(arr, arr1)
print(arr2)

# Output
# [[ 6. 13. 17. 21.]
#  [10. 17. 21. 25.]
# [14. 21. 25. 29.]]

2.2 Subtracting the Two Arrays

numpy.subtract() function is used to return the difference of two arrays element-wise. Following is the syntax of numpy.subtract()


# Syntax of numpy.subtract
numpy.subtract(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘subtract’)

when we use numpy.subtract() function on arr, arr1 NumPy arrays, we will get a difference of arr2 array.


# Subtracting the two arrays
arr2 = np.subtract(arr, arr1)
print(arr2)

# Output
# [[ -6. -11. -13. -15.]
# [ -2.  -7.  -9. -11.]
# [  2.  -3.  -5.  -7.]]

2.3 Multiplying Two Arrays

numpy.multiply() function is used to return the product of two arrays element-wise. Following is the syntax of the numpy.multiply()


# Syntax of numpy.multiply()
numpy.multiply(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘multiply’)

Using numpy.multiply() function on arr, arr1 NumPy arrays, we will get a product of arr2 NumPy array.


# Multiplying the two arrays
arr2 = np.multiply(arr, arr1)
print(arr2)

# Output
# [[  0.  12.  30.  54.]
# [ 24.  60.  90. 126.]
# [ 48. 108. 150. 198.]]

2.4 Dividing The Two Arrays

Array element from the first array is divided by elements from the second array (all happens element-wise). Both arr1 and arr2 must have the same shape and the element in arr2 must not be zero, otherwise, it will raise an error.

Following is the Syntax of the numpy.divide().


# Syntax of numpy.divide()
numpy.divide(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) 

Here, the arr1 parameter is an input array that works as a dividend and arr2 is also an input array that works as the divisor. It returns an array with arr1/arr2(element-wise) as elements of the output array.


# Dividing the two arrays
arr2 = np.divide(arr, arr1)
print(arr2)

# Output
# [[0.         0.08333333 0.13333333 0.16666667]
# [0.66666667 0.41666667 0.4        0.38888889]
# [1.33333333 0.75       0.66666667 0.61111111]]

3. Use numpy.power() Arthimetic Operation

Array element from the first array is raised to the power of element from the second array(all happens element-wise). Both arr1 and arr2 must have the same shape and each element in arr1 must be raised to the corresponding positive value from arr2 otherwise, it will raise a Value Error.

Following is the syntax of the numpy.power().


# Syntax of numpy.power
numpy.power(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) 

Here, arr1 parameter is a base and arr2 parameter is a exponent. Let’ s take an example,


# Creating a 1D input array
arr = np.array([5, 3, 6, 9, 2, 4]) 

# Use numpy.power() to calculating exponents of an array of numbers
arr2 = np.power(arr,3)
print(arr2)

# Output
# [125  27 216 729   8  64]

From the below, we have declared two arrays (lists) naming ‘arr’ and ‘arr1’. The array ‘arr’ consists of elements [2,4,6,5,3], and array ‘arr1’ consists of [2,3,5,4,1] respectively. Here the first array ‘arr’ is going to be the array of bases, and the second array ‘arr1’ will be the list of exponents. After that, you have calculated the result of ‘arr’ to the power ‘arr1’ with the use of numpy.power() function.


# Creating an 2D input array
arr = [2, 4, 6, 5, 3]
arr1 = [2, 3, 5, 4, 1]

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

# Output
# [ 4   64 7776  625  3]

4. Use numpy.reciprocal() Operation

numpy.reciprocal() function is used to return the reciprocal of the argument, element-wise. Following is the syntax of the numpy.reciprocal()


# Syntax of numpy.reciprocal
numpy.reciprocal(x, /, out=None, *, where=True)

Use numpy.reciprocal() function we will get reciprocal of specified argument.


# creating a 1D input array
arr = np.array([50, 1.34, 3, 1, 25]) 

# Use numpy.reciprocal() function
arr2 = np.reciprocal(arr)
print(arr2)  

# Output
# [0.02       0.74626866 0.33333333 1.     0.04  ]

For integer arguments with an absolute value larger than 1, the result is always zero because of the way Python handles integer division. For integer zero the result is an overflow.


# Creating a 1D input array
arr = np.array([75], dtype = int) 

# Use reciprocal function
arr2 = np.reciprocal(arr) 
print(arr2)

# Output
[0]

5. Use numpy.mod() Operation

NumPy.mod() function is used to return the element-wise remainder of the division between two arrays arr1 and arr2 i.e. arr1 % arr2. It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers.

Following is the syntax of numpy.mod().


# Syntax
numpy.mod(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘remainder’)

# Creating an 2D input array
arr = np.array([7,16, 25]) 
arr1 = np.array([4,8,6]) 

# Use numpy.mod() function
arr2 = np.mod(arr,arr1) 
print(arr2)

# Output
# [3 0 1]

Alternatively use numpy.remainder() function to produce the same result.


# Use numpy.remainder() function
arr2 = np.remainder(arr,arr1) 
print(arr2)

# Output
# [3 0 1]

Conclusion

In this article, I have explained how to use NumPy arrays operations by using arithmetic operations like numpy.power(), numpy.reciprocal() and numpy.mod() functions with examples.

Happy Learning!!

Related Articles

References

Leave a Reply

You are currently viewing Python NumPy Array Operations