Python numpy.divide() Function Explained [7 Examples]

Python numpy.divide() Function with code examples of python code

In this article, we’ll understand the role and implementation of Python numpy.divide() function with the help of easy and proper code examples. Examples include applying the divide function on arrays of the same shape as well as different shapes, handling inf and nan values, and many more. These examples will be explained step by step so you don’t find it hard to understand the working of the Numpy divide function.

What is Python Numpy divide() Function

This function performs division between the Numpy arrays. This division is element-wise and it specified that the first item of one array will be used for division with the first item of another array, the second item of one array, and the second item of another array will be divided with each other, and so on.

Please make sure that give the same shape to both the arrays on which you are planning to apply the divide() function or can be broadcasted to the same shape.

Python numpy.divide() Function with code examples of python code

numpy divide function of python with code examples preactically in jupyter notebook

Python numpy.divide() Function with code examples of python programming

Syntax of Divide Function

numpy.divide(x1, x2, out=None)

Explanation of the Parameters

  1. The first parameter can be passed an array or a value that is scalar. In division, we’ve learned the nominator and denominator concepts. This first parameter specifies the nominator used in the division operation.
  2. We can pass a scalar value or an array to the second parameter. In division operation, it’ll represent the denominator.
  3. By using the optional third parameter(out), we can specify an output array where the resulting value will be stored. If this parameter is not specified, then by default the result will be stored inside a newly created array.

Implementing numpy.divide() Function (7 Examples)

Example 1: Apply divide Function on 1D(one dimensional) arrays

import numpy as np

array1DFirst = np.array([20, 28, 300])
array1DSecond = np.array([4, 7, 30])

result = np.divide(array1DFirst, array1DSecond)
print(result)

Output

[ 5.  4. 10.]

Explanation of Code

  • First, we imported the Numpy library.
  • In the second step, two 1D arrays have been generated using the Numpy array function.
  • In the third one, the divide function is called and these two Numpy arrays have been passed to this function as its parameters.
  • Finally, printing the result gives us an array that shows the result of the element-wise division of the specified 2 arrays.

Example 2: Divison of Numpy Array with Scalar Value (Explaining Broadcasting)

oneDarray = np.array([100, 500, 400])
scalarVal = 50

res = np.divide(oneDarray, scalarVal)
print(res)

Output

[ 2. 10.  8.]

Code Explanation

In this code, we created a 1D array and specified a scalar value. Then we passed the array and scalar value to the divide function. The output shows that this value ‘50‘ has been used with each item of the array to perform division.

Example 3: Division of Numpy Array with the scalar value of (0)

oneDarr = np.array([10, 20, 40])
sVal = 0

res = np.divide(oneDarr, sVal)
print(res)

Output

[inf inf inf]

Explanation

If you use the number zero(0) as a scalar value and implement it inside numpy.divide() function then a runtime warning will be executed and depending on the context, inf (infinity) or nan (not a number) will be generated as seen in the above output.

Example 4: Division between 1D and 2D arrays (Broadcasting)

a1 = np.array([[30, 50], [70, 90]])
a2 = np.array([10, 20])

result = np.divide(a1, a2[:, np.newaxis]) # arr2 broadcasting to gett shape (2, 1)
print(result)

Output

[[3.  5. ]
 [3.5 4.5]]

Code Explanation

In this example code, we have 2 arrays. The first one is 2D(two-dimensional) and the second one is 1D(one-dimensional). The specified 2D array has the shape of (2,2) while the above 1D array has the shape of (2,).

By getting the shape of (2,1) to 1D array through broadcasting, we can easily perform division of element-wise between the specified 1D and 2D array. The displayed result can be seen in the output section.

Example 5: Handle the infinity and NaN values and Zero Division

array1 = np.array([20, 10, 66])
array2 = np.array([0, 5, 3])

result = np.divide(array1, array2)
print(result)

result[np.isinf(result)] = np.nan  # infinity and NaN values handled
print(result)

Output

[inf  2. 22.]
[nan  2. 22.]      # inf replaced with nan

Code Explanation

In this Python code example, we have a value 0(zero) in one of the above arrays. By using it in the division Numpy function, we got an inf(infinity) value.

In the second part of that code, what we have done is that we called the isinf() function of Numpy to check if the result has any inf values present in it. Also, replace these inf values with NaN(not a number). Now this is how the inf values are handled in a professional manner.

Example 6: Numpy Divide function applied on Complex Numbers

ar1 = np.array([4 + 6j, 8 + 3j])
ar2 = np.array([1 + 2j, 2 + 1j])

result = np.divide(ar1, ar2)
print(result)

Output

[3.2-0.4j 3.8-0.4j]

In this simple Python code example, we’ve arrays having complex numbers. Then we applied the division using the Numpy divide() function which is element-wise. The result in the output section displays an array that includes the complex numbers.

Example 7: Parameter ‘out’ of the divide() Function

arrayFir = np.array([1,2,3])
arraySec = np.array([7,8,9] )
output = np.zeros_like(arrayFir,dtype=np.float64) # zeros array creation with same shape as first array
np.divide(arrayFir,arraySec,out=output)
print(output)

Output

[0.14285714 0.25       0.33333333]

Explaining the code

In this code, we specified two 1D arrays and passed them to the divide function.

We’ve used np.zeros_like() to get the correct shape and type of data. We’ve set the data type to float64 as the resulting values will be in that format in this code.

We used the out parameter of the divide function and we passed it the ‘output‘ which is an array that we created. It’ll assign the resulting value of the divide() function to the specified output array.

Conclusion

In conclusion, we’ve learned the divide() function in detail with easy and properly explained Python code examples. We’ve applied the divide function of Numpy to arrays of the same and different shapes. Also, we discussed the ‘out‘ parameter of the divide function practically. Comment if you still have any queries related to the Python Numpy divide function.

Thank you very much for reading it.

Leave a Comment

Your email address will not be published. Required fields are marked *