Fix runtimewarning invalid value encountered in true_divide

Python’s NumPy library is the foundation for numerical computation. Numerical Python, popularly referred to as “NumPy” is a library that includes multidimensional array objects as well as a collection of methods for controlling them. It allows you to conduct series of mathematical computations on arrays.

Now you might be wondering why we are talking about NumPy right? Well, the “RuntimeWarning: invalid value encountered in true_divide” error is an error we encounter while using NumPy and this article is meant to dissect what the error means and how to troubleshoot it.

What causes the runtimewarning invalid value encountered in true_divide error?

This error appears when you try to divide a NumPy array by an erroneous value (like NaN, Inf, etc.). Divide(), which is included in the NumPy package and permits division between corresponding members of two arrays, is the best technique to carry out division tasks between NumPy arrays.

It’s important to note that this is only a warning, and the code will continue to run. It will instead only supply a NAN (Not A Number) or INF (Infinity) value.

The following example shows a practical instance of a code that can produce this error.

runtimewarning invalid value encountered in true_divide

Typing a code like this will give the following output

runtimewarning invalid value encountered in true_divide

Notice how the error is the first to be displayed right? Well, there are times when you will or have already faced this specific error. When you do, do not panic.

Let’s talk about why the error occurred in the code above. NumPy divides each value in “a” by the matching value in “b,” yet this results in a RuntimeWarning. This is due to the fact that the previous division operation was zero divided by zero, which ended in a nan value. Let’s have a look at how to explain it:

The items of Array a are divided by the items of Array b. As a result, the quotient value is returned.

  • 7/3 (Valid Operation)
  • 3/4 (Valid Operation)
  • 6/9 (Valid Operation)
  • 5/8 (Valid Operation)
  • 0/0 (Invalid Operation)

The last operation is invalid because we are trying to perform a division operation between two zeros. That’s why a Warning is thrown and returns the result as Not a Number (nan).

How to fix the RuntimeWarning: invalid value encountered in true_divide Error

This is the moment you’ve probably been waiting for. As previously stated, this RuntimeWarning is merely a warning and does not prohibit the code from being executed.

However, we may suppress this sort of warning by using the seterr method, which accepts invalid as an input and assigns ignore as a value. It can then conceal the warning message that contains the word invalid. The seterr method’s syntax is as follows:

fix the RuntimeWarning

Applying this to our earlier piece of code, we get the following:

How to fix the RuntimeWarning

This tells NumPy to ignore any warnings that contain an “invalid” message. As a result, if we execute the program again, we won’t see any warnings.

The output will now change to

Observe that the RuntimeWarning is gone. We can now go ahead and debug the remaining part of the code (in this case).

The very last value in the output is still returned as a nan, but no warning sign is provided this time.

Note that the position in which the seterr method is placed is also vital. Assuming we place it after the entire code operations as below, then we still won’t be free from the same RuntimeWarning:


Doing it like that will still present the same error and that’s because the compiler hasn’t been given a prior notification to exclude the errors associated with the division of zeros.

The final part of the code that will actually print the output is shown below:

The output will now be:

Leave a Comment