Python NumPy divide() Function

Python NumPy divide() function is used to divide the two arrays with the same shape or divide one array with a single numeric value. This function provides several parameters that allow the user to specify what value to divide with. Use numpy.divide() function to divided the first array elements (arr1) with the second array elements (arr2). Both arr1 and arr2 must have the same shape and the element in arr2 must not be zero; otherwise, it will raise an error.

In this article, I will explain how to use the NumPy divide() function with examples.

1. Quick Examples of Python NumPy divide() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy divide() function.


# Below are a quick examples

# Example 1: Use numpy.divide() to divide
# one dimensional array with scalar
arr = [8, 27, 15, 18, 22, 34] 
arr1 = 4
arr2 = np.divide(arr, arr1)

# Example 2: Find the division values of 
# two input arrays 
arr = [16, 28, 33, 38, 45]
arr1 = [4, 8, 13, 5, 3]
arr2 = np.divide(arr, arr1)

# Example 3: Divide arrays using / operator
arr = np.array([16, 28, 33, 38, 45])
arr1 = np.array([4, 8, 13, 5, 3])
arr2 = arr/arr1

# Example 4: Get division values
# between two multi-dimensional arrays
arr = [[15, 23, 32], [9, -17, 25]]
arr1 = [[3, 5, 8], [-2, 4, 7]]
arr2 = np.divide(arr, arr1) 

# Example 5: Divide the array values by zero
arr = [16, 28, 33, 38, 45]
arr1 = [4, 8, 0, 5, 0]
arr2 = np.divide(arr, arr1) 

2. Syntax of Python NumPy divide()

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


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

2.1 Parameters of divide()

  • arr1 – First input array or object works as a dividend.
  • arr2 – Second input array or object which works as a divisor.
  • 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.
  • where – This condition is broadcast over the input. True value means to calculate the unfunc at that position, whereas if the False value means to leave the value in output only.

2.2 Return Value of NumPy round()

It returns an array that contains division values of an input array.

3. Usage of NumPy divide() Function

The numpy.divide() is a mathematical function and is used to calculate the division between two NumPy arrays. Returns a true division of the inputs, element-wise.

4 Divide NumPy Array by scalar (Single Value)

We can divide the array with a scalar value for that, we have to take an array named arr as a dividend and the scalar value is 4 which indicates the divisor. Now we will pass the array and scalar value as an argument in numpy.divide() function. First, we have to create an array and then, apply this function.


import numpy as np
# creating an 1D input array
arr = [8, 27, 15, 18, 22, 34] 
# Get division values using divide()
arr2 = np.divide(arr,4)
print(arr2)

# Output
# [2.   6.75 3.75 4.5  5.5  8.5 ]

5. Divide NumPy Array Elementwise

Using the divide() function we can divide two arrays elementwise. First, let’s create two NumPy arrays which are a dividend array named arr, and the divisor array named arr1 using numpy.array() function. We can divide the arr by the arr1 using the numpy.divide() function. Let’s take an example,


# creating two input array
arr = np.array([16, 28, 33, 38, 45])
arr1 =np.array( [4, 8, 13, 5, 3])
# Get the division values use numpy.divide() 
arr2 = np.divide(arr, arr1)
print(arr2)

# Output
# [ 4.          3.5         2.53846154  7.6        15.        ]

6. Divide NumPy Arrays With the / Operator

You can also use the / operator to divide two arrays. The result is the same as the above.


# creating two input arrays
arr = np.array([16, 28, 33, 38, 45])
arr1 = np.array([4, 8, 13, 5, 3])
# Find the division with the / operator
arr2 = arr/arr1
print(arr2)

# Output :
# [ 4.          3.5         2.53846154  7.6        15.        ]

7. Divide NumPy Two Multi-Dimensional Arrays

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


# creating two multi-dimensional input array
arr = [[15, 23, 32], [9, -17, 25]]
arr1 = [[3, 5, 8], [-2, 4, 7]]
# Get division values of two multi-dimensional arrays
arr2 = np.divide(arr, arr1) 
print (arr2) 

# Output
# [[ 5.          4.6         4.        ]
# [-4.5        -4.25        3.57142857]]

8. Divide NumPy Array by Using 0

As I mentioned above, the divisor array must not be zero otherwise it will raise an error. In this example, I have declared some of the divisor array elements are zero. Let’s see what happens.


arr = [16, 28, 33, 38, 45]
arr1 = [4, 8, 0, 5, 0]
  
# Use numpy division 
arr2 = np.divide(arr, arr1) 
print (arr2) 

# Output
# RuntimeWarning: divide by zero encountered in true_divide
#   arr2 = np.divide(arr, arr1)
# [4.  3.5 inf 7.6 inf]

Conclusion

In this article, I have explained how to use numpy.divide() function and using this how to calculate true division values of NumPy arrays.

Happy Learning!!

You May Also Like

References

numpy divide

Leave a Reply

You are currently viewing Python NumPy divide() Function