前言
csr_matrix函数主要是用来压缩稀疏矩阵。
一、csr_matrix函数
from scipy.sparse import csr_matrix import numpy as np # data:代表的是稀疏矩阵中存储的所有元素 data = np.array([1,2,3,4,5,6]) # indices: 代表的是这6个元素所在的列的位置 indices = np.array([0,2,2,0,1,2]) # indptr: 游标,每一行起始元素的下标 # 1 2|3|4 5 6的下标为: # 0 1|2|3 4 5 # 最后记录元素的总个数6 indptr = np.array([0,2,3,6]) # 我们使用以上三个变量来记录压缩矩阵。 csr_matrix_0 = csr_matrix((data, indices, indptr), shape = (3,3)) print(csr_matrix_0.toarray()) # 输出: # [[1 0 2] # [0 0 3] # [4 5 6]]
参考文章:
看的懂的scipy.sparse.csr_matrix和scipy.sparse.csc_matrix.
总结
我累了,毁灭吧。