Python scipy.sparse——压缩稀疏矩阵和还原

当矩阵中有很多零元素时,为了节省内存可以对矩阵压缩,既然有压缩,肯定可以解压缩。压缩分为行优先压缩和列优先压缩;同理,解压缩也有行优先和列优先之分。本文主要是记录下python包scipy中提供的方法。

压缩和解压缩的函数接口是一样的,根据传递的参数进行区分。

主要的接口有两个:
        1. csr_matrix(): 列优先

        2. csc_matrix():行优先

根据第三个字母的不同,可以很容易的看出是行优先还是列优先;

如果是压缩稀疏矩阵,只需要传递待压缩的矩阵就好,用法如下:

csr_matrix(arr) 

csc_matrix(arr) 

import numpy as np
from scipy.sparse import *
matrix=np.array([[0,2,0,1,0],[0,1,0,0,0]])
# 列压缩
col_compressed_mat= csc_matrix(matrix)
# 行压缩
row_compressed_mat = csr_matrix(matrix)

# 还原压缩前的矩阵
# col_compressed_mat.toarray()

接下来看看如何解压缩。

还原列压缩的矩阵

这就需要利用到压缩后生成的三个值,即如下代码的输出

import numpy as np
from scipy.sparse import *
matrix=np.array([[0, 2, 0, 1, 0],[0, 1, 0, 0, 0]])
col_compressed_mat= csc_matrix(matrix)
print("data: ", col_compressed_mat.data)
print("indices: ", col_compressed_mat.indices)
print("indptr: ", col_compressed_mat.indptr)

# 输出如下
# data:  [2 1 1]
# indices:  [0 1 0]
# indptr:  [0 0 2 2 3 3]

其中data表示矩阵中的非零值,indices表示data中三个非零值所属的行,通过indptr表示每一列中非零元素在indices中的索引范围:他们三种满足如下的关系:

        对于每一列而言,假设它的下标为i,那么该列中非零元素的行索引为indices[indptr[i]:indptr[i+1]],对应的值为data[indptr[i]:indptr[i+1]]。

以上述矩阵第0列为例:它对应的行的索引为indptr[0]=1, indptr[1]=0,即indices[0:0],说明该列的值都为0;

以上述矩阵第1列为例:它对应的行的索引为indptr[1]=0, indptr[2]=2,即indices[0:2],对应的行是0、1,相应的值为data[0:2],即2、1。

同理可以推出其他列的值。

下面看看利用data、indices、indptr三个值去解压缩矩阵

import numpy as np
from scipy.sparse import *

data = [2, 1, 1]
indices = [0, 1, 0]
indptr = [0, 0, 2, 2, 3, 3]
recover_matrix = csc_matrix((data, indices, indptr)).toarray()

print(recover_matrix)

# 输出如下
# [[0 2 0 1 0]
# [0 1 0 0 0]]

同理,也可以推出行压缩时,data indices indptr三者的关系,即对于每一行而言,假设它的下标为i,那么该行中非零元素的列索引为indices[indptr[i]:indptr[i+1]],对应的值为data[indptr[i]:indptr[i+1]]。

另外一点需要注意的是在还原压缩矩阵时,尽量通过shape参数提供原始的行列数,否者可能会缺失数据。

# 列压缩
import numpy as np
from scipy.sparse import *
matrix=np.array([[0, 2, 0, 1, 0],[0, 1, 0, 0, 0], [0, 0, 0, 0, 0]])
print("原始稀疏矩阵:")
print(matrix)
col_compressed_mat= csc_matrix(matrix)

recover_matrix = csc_matrix((col_compressed_mat.data, col_compressed_mat.indices, col_compressed_mat.indptr)).toarray()

print("省略shape参数,还原压缩前的稀疏矩阵:")
print(recover_matrix)
a = 0

print("提供shape参数,还原压缩前的稀疏矩阵:")
print(csc_matrix((col_compressed_mat.data, col_compressed_mat.indices, col_compressed_mat.indptr), shape=[3, 5]).toarray())

# 输出如下
# 原始稀疏矩阵:
# [[0 2 0 1 0]
#  [0 1 0 0 0]
#  [0 0 0 0 0]]
# 省略shape参数,还原压缩前的稀疏矩阵:
# [[0 2 0 1 0]
#  [0 1 0 0 0]]
# 提供shape参数,还原压缩前的稀疏矩阵:
# [[0 2 0 1 0]
#  [0 1 0 0 0]
#  [0 0 0 0 0]]



# 行压缩
import numpy as np
from scipy.sparse import *
matrix=np.array([[0, 2, 0, 1, 0],[0, 1, 0, 0, 0], [0, 0, 0, 0, 0]])
print("原始稀疏矩阵:")
print(matrix)
col_compressed_mat= csr_matrix(matrix)
# print("data: ", col_compressed_mat.data)
# print("indices: ", col_compressed_mat.indices)
# print("indptr: ", col_compressed_mat.indptr)

# data = [2, 1, 1]
# indices = [0, 1, 0]
# indptr = [0, 0, 2, 2, 3, 3]
recover_matrix = csr_matrix((col_compressed_mat.data, col_compressed_mat.indices, col_compressed_mat.indptr)).toarray()

# recover_matrix = csc_matrix((data, (indices, indptr)))
print("省略shape参数,还原压缩前的稀疏矩阵:")
print(recover_matrix)
a = 0

print("提供shape参数,还原压缩前的稀疏矩阵:")
print(csr_matrix((col_compressed_mat.data, col_compressed_mat.indices, col_compressed_mat.indptr), shape=[3, 5]).toarray())

# 输出如下
# 原始稀疏矩阵:
# [[0 2 0 1 0]
#  [0 1 0 0 0]
#  [0 0 0 0 0]]
# 省略shape参数,还原压缩前的稀疏矩阵:
# [[0 2 0 1]
#  [0 1 0 0]
#  [0 0 0 0]]
# 提供shape参数,还原压缩前的稀疏矩阵:
# [[0 2 0 1 0]
#  [0 1 0 0 0]
#  [0 0 0 0 0]]
# 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值