# [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 TypeError: ‘NoneType’ object is not subscriptable

If you subscript any object with None value, Python will raise TypeError: ‘NoneType’ object is not subscriptable exception. The term subscript means retrieving the values using indexing. In this tutorial,… ## How to Check and Print Python Version?

In this tutorial, you will learn how to print the version of the current Python installation from the script. Print Python Version using sys Module The sys module comes as… ## ValueError: invalid literal for int() with base 10

ValueError occurs when we pass an invalid argument type. The error is raised when we call int() function with string argument which Python cannot parse and throws ValueError: invalid literal for… ## TypeError: ‘builtin_function_or_method’ object is not subscriptable

In Python, Built-in functions are not subscriptable, if we use the built-in functions as an array to perform operations such as indexing, you will encounter TypeError: ‘builtin_function_or_method’ object is not subscriptable.… ## Python Program to Measure the Elapsed Time in Python

There are multiple ways to measure the elapsed time in Python. The modules that are available are time, timeit, and Datetime to measure elapsed time. Using time Module We can use the time module… ## Python List extend()

Table of Contents Hide Syntax of List extend() extend() ParametersReturn Value from List extend()Difference between append() and extend() method in PythonExample 1: How to use List extend() MethodExample 2: Add Elements…