csc_matrix 与csr_matrix 中的indptr与indices 什么意思

csr:csr官方文档
csc:csc官方文档

首先以csc_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]])

cs“r”_matirx 是row的意思 即以row为标准

当row = 0时,即在矩阵的第0列时
首先计算:indptr[0]与indptrp[1] 即为0 与 2
非零的位置为 indices[indptr[0]:indptrp[1]] = indices[0:2] = 0与2
即第0行非零的位置为第0列与第2列 那第0列与第2列的数据是多少呢?
data[indptr[0]:indptrp[1]] = data[0:2] = 1与2
所以生成 [1, 0, 2]

同理在row= 1时 即在矩阵的第1列时
首先计算:indptr[1]与indptrp[2] 即为2 与 3
非零的位置为 indices[indptr[1]:indptrp[2]] = indices[2:3] = 只有2
即第一行非零的位置只有第2列 那第2列的数据是多少呢?
data[indptr[1]:indptrp[2]] = data[2:3] = 3
所以生成 [0, 0, 3]

在row =2 时 即在矩阵的第2列时
首先计算:indptr[2]与indptrp[3] 即为3与 6
非零的位置为 indices[indptr[2]:indptrp[3]] = indices[3:6] = 0、1、2
即第2行非零的位置有第0、1、2列 那第0、1、2列的数据是多少呢?
data[indptr[2]:indptrp[3]] = data[3:6] = 4、5、6
所以生成 [4, 5, 6]

而csc_matrix 即以column 列为标准 情况与上述相同

文字中黄色字体可以帮助你找到规律

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值