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)
Code language: Python (python)
Output:
[4. 2.]
Code language: Python (python)
How it works.
First, create two 1D arrays with two numbers in each:
a = np.array([8, 6])
b = np.array([2, 3])
Code language: Python (python)
Second, find the quotient of a/b by using the *
operator:
c = a / b
Code language: Python (python)
The /
operator returns the quotient of each element in array a with the corresponding element in array b:
[8/2, 6/3] = [4,2]
Code language: Python (python)
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)
Code language: Python (python)
Output:
[4. 2.]
Code language: Python (python)
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)
Code language: Python (python)
Output:
[[2. 4.]
[3. 4.]]
Code language: Python (python)
In this example, the /
operator performs element-wise division:
[[ 10/5 8/2]
[3*7 4*8]]
Code language: Python (python)
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)
Code language: Python (python)
Output:
[[2. 4.]
[3. 4.]]
Code language: Python (python)
Summary
- Use the
*
operator ordivide()
function to find the quotient of two equal-sized arrays.