分母中的元素可能为零时的高效逐元素矩阵除法 [英] Efficient element-wise matrix division when elements in denominator may be zero
问题描述
我正在使用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 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.
这篇关于分母中的元素可能为零时的高效逐元素矩阵除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!