scipy笔记:scipy.sparse
1 稀疏矩陣介紹
????????在networkx包中,很多運算返回的是sparse matrix(如nx.laplacian_matrix),這是稀疏矩陣格式。隸屬于scipy.sparse
import networkx as nx G = nx.Graph() G.add_node(1) G.add_nodes_from([2, 3]) G.add_edge(1, 2) G.add_edges_from([(1, 3)])print(G.edges([3,2])) #[(3, 1), (2, 1)nx.laplacian_matrix(G) ''' <3x3 sparse matrix of type '<class 'numpy.intc'>'with 7 stored elements in Compressed Sparse Row format> '''????????在矩陣中,若數值為0的元素數目遠遠多于非0元素的數目,并且非0元素分布沒有規律時,則稱該矩陣為稀疏矩陣。
2 稀疏矩陣舉例:
2.1?bsr矩陣
block sparse row matrix
bsr_matrix(arg1[, shape, dtype, copy, blocksize])BSR矩陣有三個參數:
- inptr列表的第i個元素與i+1個元素是儲存第i行的數據的列索引以及數據的區間索引
- 即indices[indptr[i]:indptr[i+1]]【左閉右開】為第i行元素的列索引
- data[indptr[i]: indptr[i+1]]【左閉右開】為第i行元素的data。
我們逐行分析
對于第0行,indptr[0]:indptr[1] -> 0:2,即[0,2)。因此第0行的列為indice[0:2]=[0,2],
data為:,對應的就是最后結果的第0,1行。
對于第1行,indptr[1]:indptr[2] -> 2:3,即[2,3)。因此第0行的列為indice[2:3]=[2],
data為:,對應的就是最后結果的第2,3行。
對于第2行,indptr[2]:indptr[3]?-> 3:6,即[3,6)。因此第2行的列為indice[3:6]=[0,1,2],
data為:,對應的就是最后結果的第4,5行。
?2.2 coo矩陣
coo_matrix(arg1[, shape, dtype, copy])?coo_matrix(arg1[, shape, dtype, copy])
?coo_matrix是可以根據行和列索引進行data值的累加。
from scipy.sparse import * 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, 2]) coo_matrix((data, (row, col)), shape=(4, 4)).toarray() ''' array([[4, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]]) '''?2.3 csc矩陣 & csr矩陣
Compressed Sparse Column & Row matrix
并沒有看出來和前面的coo有什么區別。。。希望評論區批評指針~
from scipy.sparse import * 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, 2]) csc_matrix((data, (row, col)), shape=(4, 4)).toarray() ''' array([[4, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]], dtype=int32) ''' from scipy.sparse import * 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, 2]) csr_matrix((data, (row, col)), shape=(4, 4)).toarray() ''' array([[4, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]], dtype=int32) '''2.4 dia矩陣
dia_matrix(arg1[, shape, dtype, copy]) from scipy.sparse import *data = np.array([[1, 2, 3, 4],[1,3,5,7],[2,4,5,7]])offsets = np.array([0, -1, 2])dia_matrix((data, offsets), shape=(4, 4)).toarray() ''' array([[1, 0, 5, 0],[1, 2, 0, 7],[0, 3, 3, 0],[0, 0, 5, 4]]) '''data定義對角線元素
offsets定義對角線的偏移量,0代表正對角線,正數代表往上偏移,負數代表往下偏移
一上圖為例:最終的結果是:data[0]就是正對角線、data[1]向下偏移一格,data[2]向上偏移兩格
2.5 dok矩陣
Dictionary Of Keys based sparse matrix
dok_matrix可以高效地逐漸構造稀疏矩陣。
這個一目了然了
3 基本初始化方法
3.1?sparse_matrix((data, (row, col)), shape=(4, 4)).toarray()
除了dok和dia,其他的都適用
row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) print(bsr_matrix((data, (row, col)), shape=(4, 4)).toarray()) print(coo_matrix((data, (row, col)), shape=(4, 4)).toarray()) print(csc_matrix((data, (row, col)), shape=(4, 4)).toarray()) print(csr_matrix((data, (row, col)), shape=(4, 4)).toarray()) #print(dia_matrix((data, (row, col)), shape=(4, 4)).toarray()) #print(dok_matrix((data, (row, col)), shape=(4, 4)).toarray()) ''' [[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]] '''3.2?sparse_matrix(array).toarray()
array可以是list,也可以是np.array
適用于所有類型的矩陣
row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) print(bsr_matrix(array).toarray()) print(coo_matrix(array).toarray()) print(csc_matrix(array).toarray()) print(csr_matrix(array).toarray()) print(dia_matrix(array).toarray()) print(dok_matrix(array).toarray()) ''' [[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]][[4 0 9 0][0 7 0 0][0 0 0 0][0 0 0 5]] '''4 判別函數
| issparse(x) isspmatrix(x) | x是否為sparse類型 |
| isspmatrix_csc(x) | x是否為csc_matrix類型 |
| isspmatrix_csr(x) | x是否為csr_matrix類型 |
| isspmatrix_bsr(x) | x是否為bsr_matrix類型 |
| isspmatrix_lil(x) | x是否為lil_matrix類型 |
| isspmatrix_dok(x) | x是否為dok_matrix類型 |
| isspmatrix_coo(x) | x是否為coo_matrix類型 |
| isspmatrix_dia(x) | x是否為dia_matrix類型 |
5 文件操作
| save_npz(file, matrix[, compressed]) | 以.npz格式保存稀疏矩陣 |
| load_npz(file) | 導入.npz格式的稀疏矩陣 |
6 轉化函數
| todense([order, out]) | 返回稀疏矩陣的np.matrix形式 |
| toarray([order, out]) | 返回稀疏矩陣的np.array形式 |
| tobsr([blocksize, copy]) | 返回稀疏矩陣的bsr_matrix形式 |
| tocoo([copy]) | 返回稀疏矩陣的coo_matrix形式 |
| tocsc([copy]) | 返回稀疏矩陣的csc_matrix形式 |
| tocsr([copy]) | 返回稀疏矩陣的csr_matrix形式 |
| todia([copy]) | 返回稀疏矩陣的dia_matrix形式 |
| todok([copy]) | 返回稀疏矩陣的dok_matrix形式 |
7 其他函數(待補充)
| find(A) | 返回稀疏矩陣A中的非零元的位置以及數值 ? |
總結
以上是生活随笔為你收集整理的scipy笔记:scipy.sparse的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 包介绍:osmnx
- 下一篇: numpy 笔记:setdiff1d