mat = # some CSR matrix
i = # the index of whatever row you want
start, stop = mat.indptr[i], mat.indptr[i+1]
noncopy_row_i = scipy.sparse.csr_matrix((mat.data[start:stop],
mat.indices[start:stop],
numpy.array([0, stop-start])),
shape=(1, mat.shape[1]))
2 回答
您可以利用CSR表示直接对基础数组进行切片,并使用新的CSR矩阵共享数据:
Numpy支持不同类型的os sparces矩阵:https://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information
可能
coo_matrix
会给出更快的元素查找,但你也可以放松一些其他的操作 . 我认为最好的方法是对您的数据和算法进行基准测试 .