Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeWarning: invalid value encountered in reduce

After updating numpy to version 1.14.1 I get the following warning message after executing any command (e.g. typing 1+1):

/home/username/anaconda3/lib/python3.6/site-packages/numpy
/core/_methods.py:26: RuntimeWarning: invalid value encountered in reduce
return umr_maximum(a, axis, None, out, keepdims)

Does anyone now what the problem is and how I can fix it?

like image 632
Ethunxxx Avatar asked Feb 27 '18 16:02

Ethunxxx


1 Answers

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 reduce
return 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.

like image 161
Anwarvic Avatar answered Oct 23 '22 02:10

Anwarvic