Slicing Sparse Matrices in Scipy — Which Types Work Best?

前端 未结 1 1658
情话喂你
情话喂你 2020-12-05 03:12

The SciPy Sparse Matrix tutorial is very good -- but it actually leaves the section on slicing un(der)developed (still in outline form -- see section: \"Handling Sparse Matr

相关标签:
1条回答
  • 2020-12-05 03:25

    Ok, so I'm pretty sure the "right" way to do this is: if you are slicing columns, use tocsc() and slice using a list/array of integers. Boolean vectors does not seem to do the trick with sparse matrices -- the way it does with ndarrays in numpy. Which means the answer is.

    indices = np.where(bool_vect)[0]
    out1 = M.tocsc()[:,indices]
    out2 = M.tocsr()[indices,:]
    

    But question: is this the best way? Is this in place?

    In practice this does seem to be happening in place -- and it is much faster than prior attempts (using lil_matrix).

    0 讨论(0)
提交回复
热议问题