NumPy divide()

Created with Sketch.

NumPy divide()

Summary: in this tutorial, you’ll learn how to use the numpy divide() function or the / operator to find the quotient of two equal-sized arrays, element-wise.

Introduction to the Numpy subtract function

The / operator or divide() function returns the quotient of two equal-sized arrays by performing element-wise division.

Let’s take some examples of using the / operator and divide() function.

Using NumPy divide() function and / operator to find the quotient of two 1D arrays

The following example uses the / operator to find the quotient of two 1-D arrays:

import numpy as np

a = np.array([8, 6])
b = np.array([2, 3])

c = a/b
print(c)

 

Output:

[4. 2.]

 

numpy divide 1d arrays

How it works.

First, create two 1D arrays with two numbers in each:

a = np.array([8, 6])
b = np.array([2, 3])

 

Second, find the quotient of a/b by using the * operator:

c = a / b

 

The / operator returns the quotient of each element in array a with the corresponding element in array b:

[8/2, 6/3] = [4,2]

 

Similarly, you can use the divide() function to get the quotient of two 1D arrays as follows:

import numpy as np

a = np.array([8, 6])
b = np.array([2, 3])

c = np.divide(a, b)
print(c)

 

Output:

[4. 2.]

 

Using NumPy divide() function and / operator to get the quotient of two 2D arrays

The following example uses the / operator to find the quotient of two 2D arrays:

import numpy as np

a = np.array([[10, 8], [6, 4]])
b = np.array([[5, 2], [2, 1]])

c = a/b
print(c)

 

Output:

[[2. 4.]
[3. 4.]]

 

In this example, the / operator performs element-wise division:

[[ 10/5 8/2]
[3*7 4*8]]

 

Likewise, you can use the divide() function to find the products of two 2D arrays:

import numpy as np

a = np.array([[10, 8], [6, 4]])
b = np.array([[5, 2], [2, 1]])

c = np.divide(a, b)
print(c)

 

numpy divide 2d arrays

Output:

[[2. 4.]
[3. 4.]]

 

Summary

  • Use the * operator or divide() function to find the quotient of two equal-sized arrays.

Leave a Reply

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