AppDividend
Latest Code Tutorials

np.divide: What is Numpy divide() Function in Python

Instead of the Python traditional’ floor division‘, the np.divide() method returns a true division. True division adjusts the output type to present the best answer, regardless of input types.

np.divide

The np.divide() is a numpy library function used to perform division amongst the elements of the first array by the elements of the second array. The process of division occurs element-wise between the two arrays. The numpy divide() function takes two arrays as arguments and returns the same size as the input array.

For the element-wise division, the shape of both the arrays needs to be the same. It is a well-known fact that division by zero is not possible. So, the elements in the second array must be non-zero. Otherwise, it raises an error.

Syntax

numpy.divide (arr_A, arr_B)

The divide() function takes up to two main parameters:

  1. arr_A: array-like, input array_A, which acts as the dividend.
  2. arr_B: array-like, input array_B, which acts as the divisor.

Return Value

The divide() function returns an array of the same size as the input array containing the quotient values when elements of arr_A are divided element-wise by the elements of arr_B.

Programming Example

Program to show the working of numpy.divide()

# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)

Output

Shape of first array, arr_A is:  (4,)

Shape of second array, arr_B is: (4,)

Output obtained is:  [4.  4.5  4.66666667 4.25]

Shape of output array, out is:  (4,)

Explanation

In the program div1.py, we have taken two numpy arrays named arr_A and arr_B consisting of different array elements; we have passed these two arrays as parameters inside the np.divide() method, first parameter arr_A acts as the dividend whose elements needs to be divided and arr_B acts as the divisor.

The elements of arr_A are divided into element-wise fashion by the elements of arr_B. The result of a division is stored in a variable named out, a numpy array of the same shape as the input arrays, and contains the quotient values obtained after division.

Program to see the np.divide() method to divide an array element with a scalar value.

See the following code.

# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
print("Elements in arr_A are: ", arr_A)

# Second Parameter is a scalar value
scal_val = 6
print("\nScalar value is: ", scal_val)

out = np.divide(arr_A, scal_val)
print("\nOutput obtained is: ", out)

Output

Elements in arr_A are: [ 8 18 28 34 ]

Scalar value is:  6

Output obtained is: [1.33333333   3.  4.66666667  5.66666667]

Explanation

In the program div2.py, we have taken a numpy input array named arr_A, which performs the function of dividend and a scalar value, i.e., 6, which will act as the divisor, we have passed arr_A as the first parameter and the scalar value as the second parameter inside np.divide() method, the scalar value will be broadcasted into an array of the same shape as arr_A. Then the division is performed element-wise as usual.

The result of the division is stored in a variable named out, which is a numpy array of the same shape as an input array and contains the quotient values obtained after division.

That’s it for this tutorial.

See also

Numpy bincount()

Numpy float to int array

Numpy random randn()

Numpy isposinf()

Numpy isneginf()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.