Search for the calculation that NaN generates

I have a moderately large portion (several thousand lines) of Python / Numpy / Scipy code that throws NaN with specific inputs. I searched and found some of the usual suspects ( log(0) , etc.), but none of the obvious ones seem to be the culprit in this case.

Is there a relatively painless way (for example, besides entering an exception handling code for each potential culprit) to find out where these NaN come from?

+8
python debugging numpy scipy nan
source share
2 answers

I would start with numpy.seterr .

In this case, an invalid operation is defined as "the result is not an expressed number, as a rule, indicates that NaN has been created." By default, this seems to be set to ignore.

+9
source share

You can use numpy.seterr to set the behavior of floating-point error handling globally for all numpy procedures. This should allow you to determine where in the code from which they arise (or at least where numpy sees them for the first time).

+3
source share

All Articles