RuntimeWarning: invalid value encountered in reduce RuntimeWarning: invalid value encountered in reduce numpy numpy

RuntimeWarning: invalid value encountered in reduce


I know I'm about five months late, but my answer might be helpful to some other people.

First, the warning indicates that the matrix, that you are running reduce over or any other function that runs reduce internally, has some invalid values. These invalid values are mostly NaN or inf. I created a small snippet to explain what I mean!!

In the following snippet, I will create a variable x that has some invalid values in it, then run a function that uses reduce internally like numpy.amax().

>>> import numpy as np>>>>>> x = np.array([[0.2, 0.7], [np.nan, np.nan]])>>> print(np.amax(x, axis=0)) RuntimeWarning: invalid value encountered in reducereturn ufunc.reduce(obj, axis, dtype, out, **passkwargs)[nan nan]

So, my advice is to double check the matrix that's causing this problem.. I don't know if this is the same case with you, but it worked for me!!

In the next time, when you encounter any problem with your code... it's always a good idea to provide the code that causing the problem.


because some values in your array has the value of inf or NaN. Check your array before applying further actions.