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:
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:
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.
- all – Specifying this parameter is better because all the errors are handled by this parameter itself. The possible values are:
- ignore: Warning will not be displayed.
- raise: Raise the error.
- call: Call the function using the seterrcall function.
- warn: Warning will be displayed.
- print: Warning will be displayed to stdout.
- log: Record the error in the log object that is specified by the “seterrcall” function.
- divide – If you want to treat the division by zero error, specify the parameter with any of the previous values (in the all parameter).
- over – If you want to treat the floating-point overflow, specify the parameter with any of the previous values (in the all parameter).
- under – If you want to treat the floating-point underflow, specify the parameter with any of the previous values (in the all parameter).
- 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”.
# 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”.
# 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”.
# 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.