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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值