Python numpy:在 true_divide 中遇到无效值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27842884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy: Invalid value encountered in true_divide
提问by Luca
I have two numpy arrays and I am trying to divide one with the other and at the same time, I want to make sure that the entries where the divisor is 0, should just be replaced with 0.
我有两个 numpy 数组,我试图将一个与另一个分开,同时,我想确保除数为 0 的条目应该只替换为 0。
So, I do something like:
所以,我做这样的事情:
log_norm_images = np.where(b_0 > 0, np.divide(diff_images, b_0), 0)
This gives me a run time warning of:
这给了我一个运行时警告:
RuntimeWarning: invalid value encountered in true_divide
Now, I wanted to see what was going on and I did the following:
现在,我想看看发生了什么,我做了以下事情:
xx = np.isfinite(diff_images)
print (xx[xx == False])
xx = np.isfinite(b_0)
print (xx[xx == False])
However, both of these return empty arrays meaning that all the values in the arrays are finite. So, I am not sure where the invalid value is coming from. I am assuming checking b_0 > 0 in the np.where function takes care of the divide by 0.
但是,这两个都返回空数组,这意味着数组中的所有值都是有限的。所以,我不确定无效值的来源。我假设在 np.where 函数中检查 b_0 > 0 处理除以 0。
The shape of the two arrays are (96, 96, 55, 64) and (96, 96, 55, 1)
两个数组的形状分别是(96, 96, 55, 64)和(96, 96, 55, 1)
采纳答案by rchang
You may have a NAN
, INF
, or NINF
floating around somewhere. Try this:
您可能有一个NAN
、INF
或NINF
漂浮在某处。尝试这个:
np.isfinite(diff_images).all()
np.isfinite(b_0).all()
If one or both of those returns False
, that's likely the cause of the runtime error.
如果其中之一或两者都返回False
,则可能是运行时错误的原因。