假设0 *无限= 0,如何在NumPy中乘法(.outer())? [英] How to multiply.outer() in NumPy while assuming 0 * infinity = 0?

查看:91
本文介绍了假设0 *无限= 0,如何在NumPy中乘法(.outer())?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在多维数组上使用numpy.multiply.outer,我真的需要它假设它看到的任何0 * infinity都为零.如何有效地做到这一点?

I'm trying to use numpy.multiply.outer on multidimensional arrays, and I really need it to assume that any 0 * infinity it sees evaluates to zero. How can I do this efficiently?

>>> import numpy
>>> numpy.multiply.outer([0.], [float('inf')])
Warning (from warnings module):
  File "__main__", line 2
RuntimeWarning: invalid value encountered in multiply
array([[ nan]])

推荐答案

您是否需要担心nan值的其他来源?如果没有,您总是可以在一个单独的步骤中进行修复:

Do you need to worry about other sources of nan values? If not, you could always just fix up in a separate step:

import numpy as np

r = np.multiply.outer([0.], [float('inf')])
np.where(np.isnan(r), 0, r)

要禁止显示警告,由您决定.

Up to you if you want to suppress the warnings.

这篇关于假设0 *无限= 0,如何在NumPy中乘法(.outer())?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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