AppDividend
Latest Code Tutorials

# Numpy true_divide(): How to Use np true_divide() in Python

Numpy true_divide() function is used to divide two arrays of the same size. If we have two arrays arr1 and arr2, then true_divide will divide values of arr2 by values of arr1, but we will get a floor result.

## Numpy true_divide()

Numpy true_divide() function works similar to the Python floor_divide() function, the only difference is, this function uses  / operator instead of // operator pair with a remainder operator (%). The simplified equation is: b = a % b + b * (a / b) up to round off. We can divide array elements by any scalar also.

### Syntax

```numpy.true_divide(arr1,arr2,out=None)
```

### Parameters

The np true_divide() function takes mainly two parameters:

1. arr1: This is the input array that acts like a dividend.
2. arr2: This is an input array that acts as a divisor.
3. out: This is an optional field. A place the result will be stored in. If given, the shape to which the inputs broadcast must be in. If a freshly-allocated array is returned unless received or None. A tuple (possible as a keyword argument only) must have a length equal to the output number.

### Return Value

The true_divide() function returns the true division of arr1 and arr2 (arr1/arr2).

### Programming Example of np true_divide()

#### Program to show the working of true_divide() when both inputs are arrays.

See the following code.

```# 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, 5, 4])
# Shape of second array, arr_B is:
print("Shape of second array, arr_B is:", arr_B.shape)

out = np.true_divide(arr_A, arr_B)
print("Output obtained is: ", out)
# Shape of output array, out is:
print("Shape 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 5.6 8.5]
Shape of output array, out is:  (4,)```

### Explanation

In this program, 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.true_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 the floor_division is stored in a variable named out, which is a numpy array of the same shape as the input arrays and contains the quotient values obtained after division.

## When divisor is a scalar

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("Scalar value is: ", scal_val)

out = np.true_divide(arr_A, scal_val)
print("Output 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, 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.true_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 the input array and contains the quotient values obtained after division.

Finally, Numpy true_divide() function is over.