从scipy CSR稀疏矩阵访问值,列索引和row_ptr数据 [英] Access value, column index, and row_ptr data from scipy CSR sparse matrix
问题描述
我有一个大的矩阵,希望将其转换为稀疏的CSR格式.
I have a large matrix that I would like to convert to sparse CSR format.
当我这样做时:
import scipy as sp
Ks = sp.sparse.csr_matrix(A)
print Ks
我在A密集的地方
(0, 0) -2116689024.0
(0, 1) 394620032.0
(0, 2) -588142656.0
(0, 12) 1567432448.0
(0, 14) -36273164.0
(0, 24) 233332608.0
(0, 25) 23677192.0
(0, 26) -315783392.0
(0, 45) 157961968.0
(0, 46) 173632816.0
等...
我可以使用以下方法获取行索引,列索引和值的向量:
I can get vectors of row index, column index, and value using:
Knz = Ks.nonzero()
sparserows = Knz[0]
sparsecols = Knz[1]
#The Non-Zero Value of K at each (Row,Col)
vals = np.empty(sparserows.shape).astype(np.float)
for i in range(len(sparserows)):
vals[i] = K[sparserows[i],sparsecols[i]]
但是可以提取稀疏CSR格式(值,列索引,行指针)中包含的向量吗?
But is it possible to extract the vectors supposedly contained in the sparse CSR format (Value, Column Index, Row Pointer)?
SciPy的文档解释说,可以从这三个向量生成CSR矩阵,但我想相反地将这三个向量取出.
SciPy's documentation explains that a CSR matrix could be generated from those three vectors, but I would like to do the opposite, get those three vectors out.
我想念什么?
感谢您的时间!
推荐答案
value = Ks.data
column_index = Ks.indices
row_pointers = Ks.indptr
我相信这些属性是未记录的,可能会使其发生变化,但是我已经在scipy的多个版本中使用了它们.
I believe these attributes are undocumented which may make them subject to change, but I've used them on several versions of scipy.
这篇关于从scipy CSR稀疏矩阵访问值,列索引和row_ptr数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!