矩阵中行和列的点积为1x1 csr_matrix
。如何有效地将其转换为标量?现在我使用sum
。下面的M
是一个方阵:
dot_product_result = csr_matrix.sum(M.getrow(3).dot(M.getcol(5)))
Here's the lil_matrix
documentation.
更新当我尝试以下解决方案时,我收到此错误:
raise NotImplementedError('adding a nonzero scalar to a '
NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported
这是我正在使用的实际代码:
e = rating - Q.getrow(mid).dot(P.getcol(uid))[0]
iteration_error += e
我将iteration_error
初始化为0.0。
答案 0 :(得分:3)
M[3,:].dot(M[:,5])[0,0]
另外,调用csr_matrix.sum
这样的风格不是很好:它是一个实例方法,所以应该调用M[3,:].dot(M[:,5]).sum()