numpy.true_divide() in Python: Get Element-wise True Division of Array

The numpy.true_divide() function in Python is used to perform element-wise true division of two arrays and it is similar to the ‘/’ operator. However, it is designed to handle division with more flexibility and precision, especially when dealing with integer or boolean arrays.

In this article, we will understand Python numpy.true_divide() in detail using its syntax and various examples. Let’s get started.

Also Read: numpy.float_power() in Python – Get Element-wise Power of Array

Syntax of numpy.true_divide() Function

Let us get started by understanding the fundamental constituents of the numpy.true_divide() function with the help of its syntax given below.

Syntax:

numpy.true_divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None)

Parameters:

  • x1: This is the numerator array or the scalar value.
  • x2: This is the denominator array or the scalar value.
  • out (optional): This is an optional parameter that specifies the location where the result should be stored. If not provided, a new array will be created.
  • where (optional): This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.
  • casting (optional): This parameter controls what kind of data casting may occur. The default is ‘same_kind’, which means that only casting between similar kinds (e.g., float to float) is allowed.
  • order (optional): Specifies the memory layout of the result. The default is ‘K’ which means the order is determined by the input array’s memory layout.
  • dtype (optional): This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.

Return:

The numpy.true_divide() function returns an array with the same shape as the input arrays, where each element is the result of the corresponding elements in x1 divided by x2. By default, the elements in the output array are floating-point numbers

Examples of numpy.true_divide() Function

Let’s see examples of using the Python numpy.true_divide() function to divide scalarsarrays, and an array by a scalar.

Dividing Scalars

Let’s first start with a simple example. Here we will pass both the input parameters: x1 and x2 as scalars into the numpy.true_divide() function.

import numpy as np
sc1 = 8
sc2 = 2
result= np.true_divide(sc1, sc2)
print(result)

In the above code, we first imported the NumPy library as np and we took two scalar values: sc1 and sc2. We passed these values into numpy.true_divide() function to do the division of the sc1 by the sc2.

Output:

Dividing Scalars Output

In the output, we can see that sc1 was divided by sc2 and we got 4.0 as a result. We got 4.0 instead of 4 here because the numpy.true_divide() function returns floating-point values by default, even when the division results in a whole number.

Dividing 1D Arrays

In this example, we will pass two 1D arrays as input in the numpy.true_divide() function so that the element-wise division can be performed between the arrays.

import numpy as np
ar1 = np.array([10, 20, 30])
ar2 = np.array([2, 4, 6])
result = np.true_divide(ar1, ar2)
print(result)

Here we have created two 1D arrays: ar1 and ar2 by using the NumPy library then we have passed these arrays into the numpy.true_divide() function which gives us the array with the same shape by performing the element-wise division between ar1 and ar2 means the first element of the array ar1 will be divided by the first element of the array ar2 and so on.

Output:

Dividing 1D Arrays Output

Dividing 2D Arrays

In this example, we will take our input arrays as a two-dimensional array and pass them into the numpy.true_divide() function to get the element-wise division.

import numpy as np
ar1 = np.array([[1, 2, 3], [4, 5, 6]])
ar2 = np.array([[2, 2, 2], [3, 3, 3]])
result = np.true_divide(ar1, ar2)
print(result)

Here we have created two 2D arrays: ar1 and ar2 by using the NumPy library. We then passed them in the numpy.true_divide() function to perform element-wise division between them.

Output:

Dividing 2D Arrays Output

Dividing 1D Array by a Scalar

In this example, we will pick the first input as a one-dimensional array and the second input as a scalar value and then pass both of them into the numpy.true_divide() function where the scalar value will divide each element of the 1D array resulting in a 1D array with the same shape.

import numpy as np
ar = np.array([2, 4, 5])
sc= 10
result = np.true_divide(ar, sc)
print(result)

Output:

Dividing 1D Array by a Scalar Output

We can see in the output that the scalar value sc which was 10 divided each element from the array ar such as the first element of the array was 2 got divided by 10 which gives us 0.2 and so on.

Dividing 2D Array by a Scalar

Here the only change from the above example is we will take a two-dimensional array in place of a one-dimensional array.

import numpy as np
ar = np.array([[10, 20, 30], [40, 50, 60]])
sc = 2
result = np.true_divide(ar, sc)
print(result)

Output:

Dividing 2D Array by a Scalar Output

Summary

Now that we have reached the end of this article, hope it has provided a clear understanding of the different ways to find the element-wise division of scalar values as well as the arrays by using the numpy.true_divide() function from the Python NumPy library. For individuals who want to advance in Python, CodeforGeek has a ton of other entertaining and equally educational articles that could be quite beneficial, so be sure to check them out.

Reference

https://numpy.org/doc/stable/reference/generated/numpy.true_divide.html

Priyanshu Singh
Priyanshu Singh
Articles: 44