What is the numpy.true_divide() function in Python?

Overview

In Python, the true_divide() function is used to return the element-wise true division of inputs x1 and x2.

Syntax

numpy.true_divide(x1, x2, out=None, where=True)

Parameters

x1 (required): It represents the dividend array.

x2 (required): It represents the divisor array.

out (optional): It represents the location where the result is stored.

where (optional): It represents the condition that is broadcasted over the input.

kwargs (optional): It allows us to pass a keyword-only argument to the function.

Return value

This function returns an array or a scalar (when both inputs are scalars).

Note: In Python, / is the true division operator, and the true_divide() function is equivalent to it.

Example

import numpy as np
# creating a dividend array
mydividend = np.arange(5) + 1
# ceating a divisor
mydivisor = 4
# implementing the true_divide() function
myarray = np.true_divide(mydividend, mydivisor)
# printing the result
print(myarray)

Explanation

  • Line 1: We import the numpy module.

  • Line 4: We create a dividend array mydividend.

  • Line 7: We create a divisor variable mydivisor.

  • Line 10: We invoke the true_divide() function on the arrays, and assign the results to a variable myarray.

  • Line 13: We print myarray.

Free Resources