RuntimeWarning: invalid value encountered in reduce
Asked Answered
W

3

11

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?

Wistrup answered 27/2, 2018 at 16:6 Comment(0)
T
15

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.

Tergum answered 9/8, 2018 at 12:33 Comment(1)
Thanks for your answers! The things is that I don't use any matrix at all. I just typed 1+1 and that's it...Wistrup
M
1

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

Message answered 19/4, 2021 at 16:41 Comment(0)
L
0

Sometimes this error has nothing to do with your original matrix. You can check and remove all nan and inf values. But if you use something like scipy.stats.kstest() and depending upon the distribution list that you give it, you can get this exact error. For example if you test the argus distribution you can get:

/site-packages/scipy/stats/_continuous_distns.py:719: RuntimeWarning: invalid value encountered in sqrt sk = 2*(b-a)np.sqrt(a + b + 1) / (a + b + 2) / np.sqrt(ab)

If you test for the logistic distribution you can get:

/site-packages/scipy/stats/_continuous_distns.py:6111: RuntimeWarning: invalid value encountered in log lndata = np.log(data - loc)

or, also testing logistic distribution:

/site-packages/numpy/core/fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

...and many more.

Lagniappe answered 29/10, 2023 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.