scipy常见数据结构:coo_matrix、csc_matri

2020-09-02  本文已影响0人  编程回忆录

scipy.sparse.coo_matrix

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

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

使用稠密二维数组构造

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)

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的缩写即压缩稀疏基于行存储的矩阵,好绕口,该矩阵有如下几种构造方法:

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
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)

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构造稀疏矩阵。

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的缩写即基于列存储的压缩稀疏矩阵,该矩阵有如下几种构造方法:

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的输出对比可以看出该矩阵是按列逐个存储。

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)

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]])

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则支持快速按列切片操作。

上一篇 下一篇

猜你喜欢

热点阅读