CSR(compressed sparse row matrix)

来源:互联网 发布:php个人中心源码 编辑:程序博客网 时间:2024/06/02 04:19
https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
CSR(compressed sparse row matrix)

import numpy as np
from scipy.sparse import csr_matrix

indptr= np.array([0,2,3,6])
indices= np.array([0,2,2,0,1,2])
data      =np.array([1,2,3,4,5,6])
csr_matrix((data,indices,indptr),shape=(3,3)).toarray()

上述代码的输出结果为;
 array([[1, 0, 2], 
            [0, 0, 3], 
            [4, 5, 6]])
indices中的中的0代表data中数据1在某行的第0位置,indices中2代表data中2在某行的第2位置
indices中的中的2代表data中数据3在某行的第2位置,indices中0代表data中4在某行的第0位置
indices中的中的1代表data中数据5在某行的第1位置,indices中2代表data中6在某行的第2位置

indptr中的0是默认数据,2-0=2代表第一行有两个数据,3-2=1代表第2行有1个数据,6-3=3代表
第三行有三个数据。

这种储存形式的需要的储存量:2*nnz+n+1个数据。其中nnz(number of non-zero)代表矩阵中
不为0的数据的个数,n为列数。

CSC(compressed sparse column)跟CSR差不多,就是一个转置的关系。