开发者

Finding the calculation that generates a NaN

开发者 https://www.devze.com 2023-03-10 03:20 出处:网络
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

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?


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".


You can use numpy.seterr to set floating point error handling behaviour globally for all numpy routines. That should let you pinpoint where in the code they are arising from (or a least where numpy see them for the first time).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号