sparse matrix稀疏矩陣不同的存儲形式在sparse模塊中對應如下:
scipy不同稀疏矩陣的介紹和優缺點
scipy.sparse庫中提供了多種表示稀疏矩陣的格式,每種格式都有不同的用處。同時稀疏矩陣可以支持加、減、乘、除和冪等算術操作。Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division,and matrix power.分塊壓縮稀疏行格式(BSR)bsr_matrix(arg1, shape=None, dtype=None, copy=False, blocksize=None)Block Sparse Row matrix:
和壓縮稀疏行格式(CSR)很相似,但是BSR更適合於有密集子矩陣的稀疏矩陣,分塊矩陣通常出現在向量值有限的離散元中,在這種情景下,比CSR和CSC算術操作更有效。The Block Compressed Row (BSR) format is very similar to the Compressed Sparse Row (CSR) format. BSR is appropriate for sparse matrices with dense sub matrices. Block matrices often arise in vector-valued finite element discretizations. In such cases, BSR is considerably more efficient than CSR and CSC for many sparse arithmetic operations.
csc_matrix(arg1,shape=None, dtype=None, copy=False)壓縮的列稀疏矩陣CSC :
高效的CSC +CSC, CSC * CSC算術運算;高效的列切片操作。但是矩陣內積操作沒有CSR, BSR快;行切片操作慢(相比CSR);稀疏結構的變化代價高(相比LIL 或者 DOK)。
Advantages of the CSC format
•efficient arithmetic operations CSC + CSC, CSC * CSC, etc.
•efficient column slicing
•fast matrix vector products (CSR, BSR may be faster!)
Disadvantages of the CSC format
•slow row slicing operations (consider CSR)
•changes to the sparsity structure are expensive (consider LIL or DOK)
csr_matrix(arg1, shape=None, dtype=None, copy=False)Compressed Sparse Row matrix壓縮稀疏行格式(CSR):
高效的CSR + CSR, CSR *CSR算術運算;高效的行切片操作;高效的矩陣內積內積操作。但是列切片操作慢(相比CSC);稀疏結構的變化代價高(相比LIL 或者 DOK)。CSR格式在存儲稀疏矩陣時非零元素平均使用的字節數(Bytes per Nonzero Entry)最為穩定(float類型約為8.5,double類型約為12.5)。CSR格式常用於讀入數據后進行稀疏矩陣計算。
Advantages of the CSR format
•efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
•efficient row slicing
•fast matrix vector products
Disadvantages of the CSR format
•slow column slicing operations (consider CSC)
•changes to the sparsity structure are expensive (consider LIL or DOK)
coo_matrix(arg1,shape=None,dtype=None,copy=False)坐標格式(COO):
坐標形式的一種稀疏矩陣。采用三個數組row、col和data保存非零元素的信息。這三個數組的長度相同,row保存元素的行,col保存元素的列,data保存元素的值。
coo_matrix不支持元素的存取和增刪,一旦創建之后,除了將之轉換成其它格式的矩陣,幾乎無法對其做任何操作和矩陣運算。
Advantages of the COO format
•facilitates fast conversion among sparse formats
•permits duplicate entries (see example)
•very fast conversion to and from CSR/CSC formats
•does not directly support:
–arithmetic operations
–slicing
COO格式常用於從文件中進行稀疏矩陣的讀寫,如matrix market即采用COO格式。
最常用的函數:
tocsc() |
Return a copy of this matrix in Compressed Sparse Column format |
tocsr() |
Return a copy of this matrix in Compressed Sparse Row format |
todense([order, out]) |
Return a dense matrix representation of this matrix |
許多稀疏矩陣的數據都是采用這種格式保存在文件中的,例如某個CSV文件中可能有這樣三列:“用戶ID,商品ID,評價值”。采用numpy.loadtxt或pandas.read_csv將數據讀入之后,可以通過coo_matrix快速將其轉換成稀疏矩陣:矩陣的每行對應一位用戶,每列對應一件商品,而元素值為用戶對商品的評價。
dia_matrix(arg1, shape=None, dtype=None, copy=False)Sparse matrix with DIAgonal storage
對角存儲格式(DIA)和ELL格式在進行稀疏矩陣-矢量乘積(sparse matrix-vector products)時效率最高,所以它們是應用迭代法(如共軛梯度法)解稀疏線性系統最快的格式;DIA格式存儲數據的非零元素平均使用的字節數與矩陣類型有較大關系,適合於StructuredMesh結構的稀疏矩陣(float類型約為4.05,double類型約為8.10)。對於Unstructured Mesh以及Random Matrix,DIA格式使用的字節數是CSR格式的十幾倍。dok_matrix(arg1, shape=None, dtype=None, copy=False)Dictionary Of Keys based sparse matrix.
dok_matrix從dict繼承,它采用字典保存矩陣中不為0的元素:字典的鍵是一個保存元素(行,列)信息的元組,其對應的值為矩陣中位於(行,列)中的元素值。顯然字典格式的稀疏矩陣很適合單個元素的添加、刪除和存取操作。通常用來逐漸添加非零元素,然后轉換成其它支持快速運算的格式。
基於字典存儲的稀疏矩陣。This is an efficient structure for constructing sparse matrices incrementally.Allows for efficient O(1) access of individual elements. Duplicates are not allowed. Can be efficiently converted to a coo_matrix once constructed.
lil_matrix(arg1, shape=None, dtype=None, copy=False)Row-based linked list sparse matrix
This is an efficient structure for constructing sparse matrices incrementally.
基於行連接存儲的稀疏矩陣。lil_matrix使用兩個列表保存非零元素。data保存每行中的非零元素,rows保存非零元素所在的列。這種格式也很適合逐個添加元素,並且能快速獲取行相關的數據。
Advantages of the LIL format•supports flexible slicing
•changes to the matrix sparsity structure are efficient
Disadvantages of the LIL format
•arithmetic operations LIL + LIL are slow (consider CSR or CSC)
•slow column slicing (consider CSC)
•slow matrix vector products (consider CSR or CSC)
Intended Usage
•LIL is a convenient format for constructing sparse matrices
•once a matrix has been constructed, convert to CSR or CSC format for fast arithmetic and matrix vector operations
•consider using the COO format when constructing large matrices
Note:{dok_matrix和lil_matrix適合逐漸添加元素}
綜合
2. COO和CSR格式比起DIA和ELL來,更加靈活,易於操作;
3. ELL的優點是快速,而COO優點是靈活,二者結合后的HYB格式是一種不錯的稀疏矩陣表示格式;
4. 根據Nathan Bell的工作:
CSR格式在存儲稀疏矩陣時非零元素平均使用的字節數(Bytes per Nonzero Entry)最為穩定(float類型約為8.5,double類型約為12.5)
而DIA格式存儲數據的非零元素平均使用的字節數與矩陣類型有較大關系,適合於StructuredMesh結構的稀疏矩陣(float類型約為4.05,double類型約為8.10)
對於Unstructured Mesh以及Random Matrix,DIA格式使用的字節數是CSR格式的十幾倍;
5. 一些線性代數計算庫:COO格式常用於從文件中進行稀疏矩陣的讀寫,如matrix market即采用COO格式,而CSR格式常用於讀入數據后進行稀疏矩陣計算