scipy常见数据结构:coo_matrix、csc_matrix与csr_matrix

scipy.sparse.coo_matrix

coo_matrix全称是A sparse matrix in COOrdinate format,一种基于坐标格式的稀疏矩阵,每一个矩阵项是一个三元组(行,列,值)。
该矩阵的常见构造方法有如下几种:

  • coo_matrix(D)
    举例如下:
import numpy as np
from scipy.sparse import coo_matrix
coo = coo_matrix(np.array([1, 2, 3, 4, 5, 6]).reshape((2,3)))
print(coo)

输出为:


image.png

使用稠密二维数组构造

  • coo_matrix(S)
    使用另外一个稀疏矩阵S构造。
  • coo_matrix((M, N), [dtype])
    举例如下:
from scipy.sparse import coo_matrix
coo_matrix((3, 4), dtype=np.int8).toarray()

输出为:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)

  • coo_matrix((data, (i, j)), [shape=(M, N)])
    data即矩阵存储的数据,i为行下标,j为列下标,
    data,i,j的关系为:A[i[k], j[k]] = data[k]
    举例如下:
from scipy.sparse import coo_matrix
row  = np.array([0, 3, 1, 0])
col  = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
pr

输出为:
array([[4, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 5]])
如果行列坐标有重复,对应的值直接累加,举例如下:

row  = np.array([0, 0, 1, 3, 1, 0, 0])
col  = np.array([0, 2, 1, 3, 1, 0, 0])
data = np.array([1, 1, 1, 1, 1, 1, 1])
coo = coo_matrix((data, (row, col)), shape=(4, 4))
np.max(coo.data)
coo.toarray()

输出为:
array([[3, 0, 1, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]])

scipy.sparse.csr_matrix

csr是Compressed Sparse Row matrix的缩写即压缩稀疏基于行存储的矩阵,好绕口,该矩阵有如下几种构造方法:

  • csr_matrix(D)
    D是一个稠密矩阵或2维的ndarray
    举例如下:
import numpy as np
from scipy.sparse import csr_matrix
csr = csr_matrix(np.array([1, 2, 3, 4, 5, 6]).reshape((2,3)))
print(csr)

输出为:


image.png
  • csr_matrix(S)
    使用另外一个csr即S构造
  • csr_matrix((M, N), [dtype])
    构造一个shape为(M,N)的dtype类型空矩阵
    举例如下:
import numpy as np
from scipy.sparse import csr_matrix
import numpy as np
from scipy.sparse import csr_matrix
csr_matrix((3, 4), dtype=np.int8).toarray()

输出为:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)

  • csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
    data,row_ind,col_ind的关系为:a[row_ind[k], col_ind[k]] = data[k]
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
csr_matrix((data, (row, col)), shape=(3, 3)).toarray()

输出为:
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
按行存储,即先存储第0行,然后第1行,依次到最后一行,即先扫描row数组的数据,第一个数据是0即第0行,然后扫描col的第一个数据是0即第0列,那么第0行第0列存储的值就是data的第一个数据即1,然后继续扫描row的第二个数据还是0即还是第0行,col对应的第二个数据是2即第2列,data的第二个数据是2,即第0行第2列的数据是2,依次扫描row,找对应的col和data构造稀疏矩阵。

  • csr_matrix((data, indices, indptr), [shape=(M, N)])
    这是标准的CSR表示方法,其中第i行的列下标存储在indices[indptr[i]:indptr[i+1]],根据该公式可以得到行数即为indptr的长度减1,对应的列列值存储在data[indptr[i]:indptr[i+1]]。
    举例如下:
import numpy as np
from scipy.sparse import csr_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]])

scipy.sparse.csc_matrix

csc是Compressed Sparse Column matrix的缩写即基于列存储的压缩稀疏矩阵,该矩阵有如下几种构造方法:

  • csc_matrix(D)
    使用一个二维数组构造,举例如下:
import numpy as np
from scipy.sparse import csr_matrix
csc = csc_matrix(np.array([1, 2, 3, 4, 5, 6]).reshape((2,3)))
print(csc)

输出如下:


image.png

和前面的csr的输出对比可以看出该矩阵是按列逐个存储。

  • csc_matrix(S)
    使用另外一个csc构造。
  • csc_matrix((M, N), [dtype])
import numpy as np
from scipy.sparse import csc_matrix
csc_matrix((3, 4), dtype=np.int8).toarray()

输出如下:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)

  • csc_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
    举例如下:
import numpy as np
from scipy.sparse import csc_matrix
row = np.array([0, 2, 2, 0, 1, 2])
col = np.array([0, 0, 1, 2, 2, 2])
data = np.array([1, 2, 3, 4, 5, 6])
csc_matrix((data, (row, col)), shape=(3, 3)).toarray()

输出如下:
array([[1, 0, 4],
[0, 0, 5],
[2, 3, 6]])

  • csc_matrix((data, indices, indptr), [shape=(M, N)])
    这是标准的csc矩阵表示方法,其中第i列的行下标存储在indices[indptr[i]:indptr[i+1]],对应的行值存储在data[indptr[i]:indptr[i+1]]。
    举例如下:
import numpy as np
from scipy.sparse import 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])
csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()

输出如下:
array([[1, 0, 4],
[0, 0, 5],
[2, 3, 6]])

coo_matrix、csc_matrix与csr_matrix的关系与用法

coo_matrix由于构造方便容易理解,所以通常都是先构造该矩阵然后调用tocsr和tocsc函数来获取另外两种矩阵的存储。
csr_matrix支持快速的按行切片,而csc_matrix则支持快速按列切片操作。

你可能感兴趣的:(scipy常见数据结构:coo_matrix、csc_matrix与csr_matrix)