【问题标题】:Convert 1x1 sparse matrix to scalar将 1x1 稀疏矩阵转换为标量
【发布时间】:2014-02-16 17:39:38
【问题描述】:

矩阵中行和列的点积是 1x1 csr_matrix。如何有效地将其转换为标量?现在我使用sumM下面是方阵:

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。

【问题讨论】:

    标签: python matrix scipy sparse-matrix


    【解决方案1】:

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

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

    【讨论】:

    • 我按照您的建议将Q.getrow(mid) 转换为Q[mid, :]。现在我收到这个警告:SparseEfficiencyWarning: Indexing into a lil_matrix with multiple indices is slow. Pre-converting to CSC or CSR beforehand is more efficient.。我应该改用getrow 吗?
    • 你应该衡量哪个更快。
    • Yowza,getrow 比索引快大约 20 倍!这是我写的测试:lpaste.net/100041
    • 谢谢!你能回答这个吗? stackoverflow.com/questions/21817378/…
    猜你喜欢
    • 2023-04-10
    • 2021-11-25
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多