稀疏矩阵指在矩阵中值为0的元素的数量远远多于非0值的矩阵
(非0元素总数/所有元素总数<=0.05)
稀疏矩阵的实现对象:
csc_matrix(): 压缩面向列的稀疏矩阵
csr_matrix(): 压缩面向行的稀疏矩阵
bsr_matrix(): 基于块的行稀疏矩阵
lil_matrix(): 基于行的链表格稀疏矩阵
dok_matrix(): 基于字典键的稀疏矩阵
coo_matrix(): 基于坐标格式的稀疏矩阵(即IJV, 三维格式)
dia_matrix(): 基于对角线存储的稀疏矩阵
import scipy.sparse as spr
import numpy as np
参数介绍:
shape: 矩形的形状
dtype: 矩形值的类型
copy: 指定建立的稀疏矩阵是否为复制模式
D1 = np.arange(9).reshape(3, 3)
c1 = spr.csc_matrix(arg1=D1)
print(c1)
(1, 0) 3
(2, 0) 6
(0, 1) 1
(1, 1) 4
(2, 1) 7
(0, 2) 2
(1, 2) 5
(2, 2) 8
D2 = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])
c2 = spr.csc_matrix(arg1=D2)
print(c2)
(2, 2) 1
c3 = spr.csc_matrix(arg1=(5, 6))
print(c3)
print(c3.todense()) # 输出原矩阵
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
参数说明:
data:矩阵中非0值
row_ind和col_ind:data对应的行列坐标
shape:矩阵形状大小
row = np.array([1, 3, 4])
col = np.array([2, 5, 6])
data = np.array([3, 4, 5])
c4 = spr.csc_matrix((data, (row, col)), shape=(7, 7))
print(c4.todense())
[[0 0 0 0 0 0 0]
[0 0 3 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 4 0]
[0 0 0 0 0 0 5]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
参数说明:
data:矩阵中非0值
indices:data对应的行索引
indptr:为data、indices值范围提供下标,第i列对应的行坐标范围为indices[indptr[i]:indptr[i+1]],第i列的非0值范围是data[indptr[i]:indptr[i+1]], 长度为N+1
data = np.array([1, 10, 20, 5, 50, 60])
indices = np.array([0, 2, 2, 0, 1, 2])
indptr = np.array([0, 2, 3, 6])
c5 = spr.csc_matrix((data, indices, indptr), shape=(3, 3))
原理介绍:
当列坐标i=0时,
indptr[0]=0, indptr[1]=2;
indices[0:2]=[0,2];行坐标值
data[0:2]=[1,10];矩阵对应元素
即: matrix[0][0]=1, matrix[2][0]=10
当列坐标i=1时,
indptr[1]=2, indptr[2]=3;
indices[2:3]=[2];行坐标值
data[2:3]=[20];矩阵对应元素
即:matrix[2][1]=20
当列坐标i=2时,
indptr[2]=3, indptr[3]=6;
indices[3:6]=[0,1,2];行坐标值
data[3:6]=[5,50,60];矩阵对应元素
即: matrix[0][2]=5, matrix[1][2]=50, matrix[2][2]=60
print(c5.todense())
[[ 1 0 5]
[ 0 0 50]
[10 20 60]]
参数说明:
shape: 矩形的形状
dtype: 矩形值的类型
copy: 指定建立的稀疏矩阵是否为复制模式
D1 = np.arange(9).reshape(3, 3)
c1 = spr.coo_matrix(arg1=D1)
print(c1)
print(c1.todense())
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
[[0 1 2]
[3 4 5]
[6 7 8]]
D2 = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])
c2 = spr.coo_matrix(arg1=D2)
print(c2)
print(c2.todense())
(2, 2) 1
[[0 0 0]
[0 0 0]
[0 0 1]]
c3 = spr.coo_matrix(arg1=(5, 6))
print(c3)
print(c3.todense()) # 输出原矩阵
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
参数说明:
data:矩阵中非0值
row_ind和col_ind:data对应的行列坐标
shape:矩阵形状大小
row = np.array([1, 3, 4])
col = np.array([2, 5, 6])
data = np.array([3, 4, 5])
c4 = spr.coo_matrix((data, (row, col)), shape=(7, 7))
print(c4)
print(c4.todense())
(1, 2) 3
(3, 5) 4
(4, 6) 5
[[0 0 0 0 0 0 0]
[0 0 3 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 4 0]
[0 0 0 0 0 0 5]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]