scipy csr_matrix和csc_matrix函数详解

概述

在用python进行科学运算时,常常需要把一个稀疏的np.array压缩,这时候就用到scipy库中的sparse.csr_matrix(csr:Compressed Sparse Row marix) 和sparse.csc_matric(csc:Compressed Sparse Column marix)

scipy.sparse.csr_matrix

官方API介绍(省略前几种容易理解的了)
csr_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays.

#  示例解读
>>> 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]])
# 按row行来压缩
# 对于第i行,非0数据列是indices[indptr[i]:indptr[i+1]] 数据是data[indptr[i]:indptr[i+1]]
# 在本例中
# 第0行,有非0的数据列是indices[indptr[0]:indptr[1]] = indices[0:2] = [0,2]
# 数据是data[indptr[0]:indptr[1]] = data[0:2] = [1,2],所以在第0行第0列是1,第2列是2
# 第1行,有非0的数据列是indices[indptr[1]:indptr[2]] = indices[2:3] = [2]
# 数据是data[indptr[1]:indptr[2] = data[2:3] = [3],所以在第1行第2列是3
# 第2行,有非0的数据列是indices[indptr[2]:indptr[3]] = indices[3:6] = [0,1,2]
# 数据是data[indptr[2]:indptr[3]] = data[3:6] = [4,5,6],所以在第2行第0列是4,第1列是5,第2列是6

scipy.sparse.csc_matrix

官方API介绍(省略前几种容易理解的了)
csc_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSC representation where the row indices for column i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays.

#  示例解读
>>> 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])
>>> csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 4],
       [0, 0, 5],
       [2, 3, 6]])
# 按col列来压缩
# 对于第i列,非0数据行是indices[indptr[i]:indptr[i+1]] 数据是data[indptr[i]:indptr[i+1]]
# 在本例中
# 第0列,有非0的数据行是indices[indptr[0]:indptr[1]] = indices[0:2] = [0,2]
# 数据是data[indptr[0]:indptr[1]] = data[0:2] = [1,2],所以在第0列第0行是1,第2行是2
# 第1行,有非0的数据行是indices[indptr[1]:indptr[2]] = indices[2:3] = [2]
# 数据是data[indptr[1]:indptr[2] = data[2:3] = [3],所以在第1列第2行是3
# 第2行,有非0的数据行是indices[indptr[2]:indptr[3]] = indices[3:6] = [0,1,2]
# 数据是data[indptr[2]:indptr[3]] = data[3:6] = [4,5,6],所以在第2列第0行是4,第1行是5,第2行是6
  • 29
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
在PyTorch中,CSC(Compressed Sparse Column)和CSR(Compressed Sparse Row)是用于稀疏矩阵存储和操作的格式。CSCCSR都是将稀疏矩阵压缩为三个数组,分别存储非零元素的值、列索引和行指针。 要使用CSCCSR格式,首先需要安装SciPy库,因为PyTorch不直接支持这两种格式。使用以下命令安装SciPy: ```python pip install scipy ``` 安装完成后,可以使用SciPy的`csr_matrix`和`csc_matrix`函数将稀疏矩阵转换为CSRCSC格式。例如,将一个二维稀疏矩阵转换为CSR格式: ```python import torch from scipy.sparse import csr_matrix sparse_matrix = torch.sparse_coo_tensor(indices, values, size) csr_matrix = csr_matrix(sparse_matrix.to_dense().numpy()) ``` 在上面的代码中,`indices`是一个包含非零元素的索引的张量,`values`是一个包含非零元素值的张量,`size`是稀疏矩阵的形状。首先,使用PyTorch的`sparse_coo_tensor`函数创建一个COO(Coordinate)格式的稀疏张量,然后使用`to_dense`方法将其转换为密集张量。最后,使用SciPy的`csr_matrix`函数将密集矩阵转换为CSR格式。 类似地,可以使用`csc_matrix`函数将稀疏矩阵转换为CSC格式: ```python from scipy.sparse import csc_matrix csc_matrix = csc_matrix(sparse_matrix.to_dense().numpy()) ``` 转换为CSRCSC格式后,可以使用SciPy提供的各种函数对稀疏矩阵进行操作和计算。 请注意,这里的示例代码是将PyTorch的稀疏张量转换为CSRCSC格式。如果你已经有一个稀疏矩阵CSRCSC表示,可以直接使用相关的SciPy函数进行操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值