Python

# How to Fix the “Invalid Value Encountered in True_Divide” Warning

Generally, the values are not divisible by 0 and infinity. In Python, NumPy is a module which is used for numerical analysis and performs the mathematical operations on arrays. When you try to divide by NaN/0/Inf, you get the “nan” result with the “Invalid value encountered in true_divide” warning.

In this guide, we will discuss how to hide this warning by setting the invalid parameter to “ignore” in the numpy.seterr. Anyway, this warning won’t stop producing the output; we will get rid of this with numpy.seterr.

## Reproducing the Warning

First, produce this warning in two different scenarios.

## Scenario 1: Divide by 0

Create two NumPy arrays with five integers in each array. The last elements in both arrays are 0s. Let’s execute the following code and you will see the output along with this warning:

import numpy

first=numpy.array([12,90,45,67,0])
second=numpy.array([10,2,3,4,0])
print(first)
print(second)

Output:

You can see that the result for the last element is “nan”. The warning is returned due to 0.

## Scenario 2: Divide by infinity

Create two NumPy arrays with five values in each array. The last elements in both arrays are infinity. Let’s execute the following code and you will see the output along with this warning:

import numpy

first=numpy.array([12,90,45,67,numpy.inf])
second=numpy.array([10,2,3,4,numpy.inf])
print(first)
print(second)

# Divide the values present in the first array with the values present in the second array
numpy.divide(first, second)

Output:

You can see that the result for the last element is “nan”. The warning is returned due to the infinity value.

## Solution: Use the Numpy.Seterr

Since the warning won’t stop the output, we will get rid of the warning, but the output will be “nan”. Basically, numpy.seterr is used to handle the floating-point errors.

Syntax:

Let’s see the syntax of numpy.seterr with all parameters. We will discuss only the “all” parameter with examples.

numpy.seterr(all, divide, over, under, invalid)
1. all – Specifying this parameter is better because all the errors are handled by this parameter itself. The possible values are:
1. ignore: Warning will not be displayed.
2. raise: Raise the error.
3. call: Call the function using the seterrcall function.
4. warn: Warning will be displayed.
5. print: Warning will be displayed to stdout.
6. log: Record the error in the log object that is specified by the “seterrcall” function.
2. divide – If you want to treat the division by zero error, specify the parameter with any of the previous values (in the all parameter).
3. over – If you want to treat the floating-point overflow, specify the parameter with any of the previous values (in the all parameter).
4. under – If you want to treat the floating-point underflow, specify the parameter with any of the previous values (in the all parameter).
5. invalid – If you want to treat the invalid floating-point operation, specify the parameter with any of the previous values (in the all parameter).

## Example 1: All = “Ignore”

Ignore the warning while dividing the values that are present in two arrays by setting the “all” parameter to “ignore”.

import numpy

# with all='ignore'
numpy.seterr(all='ignore')

first=numpy.array([12,0])
second=numpy.array([10,0])
print(first)
print(second,"\n")

print("Result: ",numpy.divide(first, second))

Output:

The output is displayed without any warning.

## Example 2: All = “Warn”

Include the warning while dividing the values that are present in two arrays by setting the “all” parameter to “warn”.

import numpy

# with all='warn'
numpy.seterr(all='warn')

first=numpy.array([12,0])
second=numpy.array([10,0])
print(first)
print(second,"\n")

print("Result: ",numpy.divide(first, second))

Output:

The output is displayed with warning.

## Example 3: All = “Log”

Include the log while dividing the values that are present in two arrays by setting the “all” parameter to “log”.

import numpy

# with all='warn'
numpy.seterr(all='log')

first=numpy.array([12,0])
second=numpy.array([10,0])
print(first)
print(second,"\n")

print("Result: ",numpy.divide(first, second))

Output:

The output is displayed with logged error.

## Conclusion

We fixed the “invalid value encountered in true_divide” warning by ignoring it using the numpy.seterr() method. To ignore this warning, pass the “all” parameter with “ignore”. In this guide, we also learned the different values that are passed to this parameter. Now, try passing the “divide” parameter with the same values and see the outputs.