numpy:在true_divide中遇到无效的值 [英] numpy: Invalid value encountered in true_divide

查看:658
本文介绍了numpy:在true_divide中遇到无效的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个numpy数组,并且试图将一个数组除以另一个,同时,我想确保除数为0的条目应仅替换为0.

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.

所以,我做类似的事情:

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])

但是,这两个都返回空数组,这意味着数组中的所有值都是有限的.因此,我不确定无效值来自何处.我假设在np.where函数中检查b_0> 0会处理除以0的问题.

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.

两个阵列的形状分别是(96、96、55、64)和(96、96、55、1)

The shape of the two arrays are (96, 96, 55, 64) and (96, 96, 55, 1)

推荐答案

您可能有NANINFNINF漂浮在某处.试试这个:

You may have a NAN, INF, or NINF floating around somewhere. Try this:

np.isfinite(diff_images).all()
np.isfinite(b_0).all()

如果其中一个或两个都返回False,则可能是运行时错误的原因.

If one or both of those returns False, that's likely the cause of the runtime error.

这篇关于numpy:在true_divide中遇到无效的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆