本文介绍了分母中的元素可能为零时的高效逐元素矩阵除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用numpy使用Python 2.7.6进行编程.我在两个Numpy矩阵V/np.dot(W,H)之间进行了这种划分.有时会发生分母具有等于0的某些单元格值的情况,因此出现运行时错误.我想以有效的方式实施安全划分.我该如何编写执行矩阵除法的代码,并针对分母等于0的元素在输出Matrix中放入0?

I'm programming with Python 2.7.6 using numpy. I have this division between two numpy matrixes V/np.dot(W,H). Sometimes happens that the denominator has some cell values equal to 0, so i get a Runtime error. I would like to implement a safe division in a efficient way. How can i write a code that performs the Matrix division and for the elements where the denominator is equal to 0 puts 0 in the output Matrix?

推荐答案

Numpy实际上允许您设置除零错误时要执行的操作-请参见设置.不过,我认为这是一个全局标志-我不知道有一个更本地化的解决方案-如果这是一个问题,我想您可以在安全划分之前和之后设置seterr.

Numpy actually allows you to set what you'd like to do in the case of a divide by zero error - see seterr. I believe this is a global flag, though - I'm not aware of a more localized solution - if it's an issue I suppose you can just set seterr before and after your safe division.

这篇关于分母中的元素可能为零时的高效逐元素矩阵除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:57