scipy.sparse 稀疏矩阵的转化

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])
scipy.sparse.csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()
output:
array([[1, 0, 4],
       [0, 0, 5],
       [2, 3, 6]])

转换成稀疏矩阵有3种方式:   crc_matrix, csr_matrix, coo_matrix

crc_matrix 矩阵是按照列存储的

indptr 是每列数据的对应的data的下标,即 indptr[0] - indptr[1], indptr[2] - indptr[1], indptr[3] - indptr[2]  数据有3列

indices 是 对应的行的下标大小

第一列:

    data[ indptr[0] : indptr[1] ] =  data[0:2] =  1, 2

    indices[indptr[0] : indptr[1] ] =  indices[0:2] =  0, 2

   output:

    [

                [1],

                [0],

                [2]

    ]

第二列:

    data[ indptr[1] : indptr[2] ]  =  data[2:3]  = 3

    indices[indptr[1] : indptr[2] ] =  indices[2:3] = 2

output(加上第一列):

[

                [1, 0],

                [0, 0],

                [2, 3]

    ]

第三列:

    data[ indptr[2] : indptr[3] ] = data[3:6] = 4,5,6 

    indices[indptr[2] : indptr[3] ] = indices[3:6] = 0,1,2

output(加上1,2列):   

[

                [1, 0,  4],

                [0, 0,  5],

                [2, 3,  6]

    ]

同理,csr_matrix 是按行的方式来的

row= np.array([0,2,0,4])
col= np.array([0,1,2,4])
data=np.array([1,2,3,4])
scipy.sparse.coo_matrix((data, (row,col)),shape=(5,5)).toarray()
output:
 array([[1, 0, 3, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 2, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 4]])

coo_matrix 很简单,就是row与col对应的位置上有值,其它都为0

猜你喜欢

转载自blog.csdn.net/weixin_39594447/article/details/86680126