Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the calculation that generates a NaN

I have a moderately large piece (a few thousand lines) of Python/Numpy/Scipy code that is throwing up NaNs with certain inputs. I've looked for, and found, some of the usual suspects (log(0) and the like), but none of the obvious ones seem to be the culprits in this case.

Is there a relatively painless way (i.e., apart from putting exception handling code around each potential culprit), to find out where these NaNs are coming from?

like image 502
Chinmay Kanchi Avatar asked Feb 03 '23 17:02

Chinmay Kanchi


1 Answers

I would start with numpy.seterr.

There, an invalid operation is defined as "result is not an expressible number, typically indicates that a NaN was produced". By default, this seems to be set to "ignore".

like image 91
NPE Avatar answered Feb 05 '23 15:02

NPE