将1x1稀疏矩阵转换为标量

时间:2014-02-16 17:39:39

标签: python matrix scipy sparse-matrix

矩阵中行和列的点积为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。

1 个答案:

答案 0 :(得分:3)

M[3,:].dot(M[:,5])[0,0]

另外,调用csr_matrix.sum这样的风格不是很好:它是一个实例方法,所以应该调用M[3,:].dot(M[:,5]).sum()