Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid "RuntimeWarning: invalid value encountered in divide" in NumPy?

Tags:

I am trying to avoid the warning RuntimeWarning: invalid value encountered in divide in NumPy.

I thought I could do:

import numpy as np

A=np.array([0.0])
print A.dtype
with np.errstate(divide='ignore'):
    B=A/A
print B

but this gives:

float64
./t.py:9: RuntimeWarning: invalid value encountered in divide
  B=A/A
[ nan]

If I replace B=A/A with np.float64(1.0) / 0.0 it gives no warning.

like image 549
Håkon Hægland Avatar asked Sep 07 '14 11:09

Håkon Hægland


People also ask

How do you handle Runtimewarning invalid value encountered in true<UNK>Divide?

Solution: We can fix this Runtime Warning by using seterr method which takes invalid as a parameter and assign ignore as a value to it. By that, it can hide the warning message which contains invalid in that.

What is Runtimewarning invalid value encountered in Double_scalars?

Why Is the Runtimewarning: Invalid Value Encountered in double_scalars Error Happening? The runtimewarning: invalid value encountered in double_scalars error happens when web developers try to perform certain mathematical operations that include numbers at the opposite end of the spectrum.


1 Answers

You need to set invalid rather than divide:

with np.errstate(invalid='ignore'):
                 ^^^^^^^
like image 85
NPE Avatar answered Sep 28 '22 09:09

NPE