在numpy和scipy中将Python警告/错误跟踪到行号 [英] Tracing Python warnings/errors to a line number in numpy and scipy
问题描述
我遇到了错误:
Warning: invalid value encountered in log
从Python开始,我相信该错误是由numpy(使用1.5.0版)引发的.但是,由于我在多个地方调用了"log"函数,因此我不确定错误是从哪里来的.有没有办法让numpy打印生成此错误的行号?
From Python and I believe the error is thrown by numpy (using version 1.5.0). However, since I am calling the "log" function in several places, I'm not sure where the error is coming from. Is there a way to get numpy to print the line number that generated this error?
我认为该警告是由于取一个足够小以四舍五入为0或较小(负数)的数字的对数引起的.是对的吗?这些警告的通常来源是什么?
I assume the warning is caused by taking the log of a number that is small enough to be rounded to 0 or smaller (negative). Is that right? What is the usual origin of these warnings?
推荐答案
将np.seterr(invalid='raise')
放入代码中(在错误的log
调用之前)
将导致numpy引发异常,而不是发出警告.
这将为您提供回溯错误消息,并告诉您发生错误时Python正在执行的行.
Putting np.seterr(invalid='raise')
in your code (before the errant log
call)
will cause numpy to raise an exception instead of issuing a warning.
That will give you a traceback error message and tell you the line Python was executing when the error occurred.
这篇关于在numpy和scipy中将Python警告/错误跟踪到行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!