# [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.

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 ## Python String split()

Table of Contents Hide SyntaxParametersReturn ValueExample 1: How to split() string in PythonExample 2: String split() using both separator and maxsplit argument The Python String split() method is a built-in… ## [Solved] AttributeError: ‘float’ object has no attribute ‘get’

Table of Contents Hide What is AttributeError: ‘float’ object has no attribute ‘get’?How to fix AttributeError: ‘float’ object has no attribute ‘get’?Solution 1 – Call the get() method on valid… ## How to enable CORS on Django REST Framework?

If we are building an API layer using the Django REST framework and accessing these APIs in the front-end application we need to enable the CORS on Django Rest Framework… ## Python TypeError: ‘int’ object is not subscriptable

Table of Contents Hide What is Subscriptable in Python?How do you make an object Subscriptable?How to Fix TypeError: ‘int’ object is not subscriptable?SolutionConclusion In Python, we use Integers to store… ## Python ValueError: cannot reindex from a duplicate axis

Table of Contents Hide Verify if your DataFrame Index contains Duplicate valuesTest which values in an index is duplicateDrop rows with duplicate index valuesPrevent duplicate values in a DataFrame indexOverwrite… ## numpy.ndarray.flatten() function

Table of Contents Hide SyntaxParametersReturn ValueExample 1: How to flatten an array using NumPy ndarray.flatten() methodExample 2: Flattening an array using numpy.flatten() method with C-style orderingExample 3: Flattening an array…