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

## Python String index()

Table of Contents Hide SyntaxParametersReturn ValueDifference between index() method and find() methodExample 1: Find the index of a string in PythonExample 2: index() method if the stringExample 3: index() method…

Table of Contents Hide pip: command not foundHow to check if pip is installed correctly?Installing pip the right wayInstalling pip for Python 2 Pip is a recursive acronym for either…

## Python bytearray()

Table of Contents Hide bytearray() Syntax bytearray() Parametersbytearray() Return ValueExample 1: Array of bytes of given integer sizeExample 2: Array of bytes from a stringExample 3: Array of bytes from an…

## TypeError: method() takes 1 positional argument but 2 were given

If you define a method inside a class, you should add self as the first argument. If you forget the self argument, then Python will raise TypeError: method() takes 1 positional…

## Python TypeError: ‘int’ object is not iterable

If you have read our previous article, the ‘NoneType’ object is not iterable. You already know why Python throws ‘typeerror‘, and it occurs basically during the iterations like for and while…

## How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…