[Solved] RuntimeWarning: invalid value encountered in true_divide

Suppose you attempt to divide the NumPy arrays elements using the divide() method with invalid values such as 0/0, NaN Infinity, zero, etc. you will encounter a RuntimeWarning: invalid value encountered in true_divide.

In this article, we will take a look at what exactly is RuntimeWarning: invalid value encountered in true_divide and how to resolve this error with examples.

What is RuntimeWarning: invalid value encountered in true_divide?

If you are working with NumPy arrays and attempting to perform division of one NumPy array values over another NumPy array values and if you have invalid values which lead to NaN, Infinity then Python interpreter will raise invalid value encountered in true_divide warning during Runtime.

Note: This is an absolute warning message and not an error. The code will still get executed successfully with the warning message.

Let us try to reproduce this error with a simple example.

import numpy as np

# define 2 NumPy arrays
a = np.array([8, 2, 9, 0])
b = np.array([4, 2, 3, 0])

# divide both the numpy arrays
print(np.divide(a, b))

Output

main.py:8: RuntimeWarning: invalid value encountered in true_divide
  print(np.divide(a, b))
[ 2.  1.  3. nan]

If you look at the above code, we have two NumPy arrays, and we are performing the division of both the array values using the NumPy divide() method.

The NumPy divide() method will return the quotient value after the division. Hence in our case following division takes place.

  1. 8/4 =2 (This would be treated as a valid operation)
  2. 2/2 =1 (This would be treated as a valid operation)
  3. 9/3 =3 (This would be treated as a valid operation)
  4. 0/0 =infinity (This would be treated as an invalid operation as 0 divisible by 0 would lead to nan; hence we get the warning).

How to fix RuntimeWarning: invalid value encountered in true_divide?

 As it’s just a warning, the NumPy has a seterr() method where we can suppress this warning.

Syntax seterr()

np.seterr(invalid='ignore')

The above method tells NumPy to suppress all the warning messages with the term that has “invalid.” Ensue that the seterr() method is called before the divide() method so that if there are any warnings it will ignore.

Let’s modify the code and suppress the “invalid” messages by using the NumPy seterr() method.

import numpy as np

# define 2 NumPy arrays
a = np.array([8, 2, 9, 0])
b = np.array([4, 2, 3, 0])

# ignore the invalid warning message
np.seterr(invalid='ignore')

# divide both the numpy arrays
print(np.divide(a, b))

Output

[ 2.  1.  3. nan]

The code will run without showing any warning and still provide the same output.

Conclusion

If we perform an invalid division operation between the elements of NumPy arrays For an example:- 0/0 we encounter RuntimeWarning: invalid value encountered in true_divide

Since this is not an error and only a warning we can resolve this RuntimeWarning by suppressing it. This can be done using np.seterr(invalid='ignore') method and it will ignore all the warnings that have “invalid” term in it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like