sparse matrix稀疏矩阵不同的存储形式在sparse模块中对应如下
bsr_matrix(arg1[, shape, dtype,copy, blocksize]) Block Sparse Row matrix
coo_matrix(arg1[, shape, dtype,copy]) A sparse matrix in COOrdinate format.
csc_matrix(arg1[, shape, dtype,copy]) Compressed Sparse Column matrix
csr_matrix(arg1[, shape, dtype,copy]) Compressed Sparse Row matrix
dia_matrix(arg1[, shape, dtype,copy]) Sparse matrix with DIAgonal storage
dok_matrix(arg1[, shape, dtype,copy]) Dictionary Of Keys based sparse matrix.
lil_matrix(arg1[, shape, dtype,copy]) Row-based linked list sparse matrix
Sparse Matrix Storage Formats稀疏矩阵的存储格式
不同稀疏矩阵的介绍和优缺点 scipy.sparse库中提供了多种表示稀疏矩阵的格式,每种格式都有不同的用处。 Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division,and matrix power. bsr_matrix(arg1, shape=None, dtype=None, copy=False, blocksize=None)Block Sparse Row matrix 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)压缩的列稀疏矩阵
Advantages of the CSC format csr_matrix(arg1, shape=None, dtype=None, copy=False)Compressed Sparse Row matrix Advantages of the CSR format coo_matrix(arg1,shape=None,dtype=None,copy=False):坐标形式的一种稀疏矩阵。采用三个数组row、col和data保存非零元素的信息。这三个数组的长度相同,row保存元素的行,col保存元素的列,data保存元素的值。 coo_matrix不支持元素的存取和增删,一旦创建之后,除了将之转换成其它格式的矩阵,几乎无法对其做任何操作和矩阵运算。 Advantages of the COO format
优点:快速的和CSR/CSC formats转换、允许重复录入.
coo_matrix支持重复元素。
Disadvantages of the COO format
·does not directly support: –arithmetic operations –slicing
缺点:不能直接进行科学计算和切片操作
最常用的函数:
ps:许多稀疏矩阵的数据都是采用这种格式保存在文件中的,例如某个CSV文件中可能有这样三列:“用户ID,商品ID,评价值”。采用numpy.loadtxt或pandas.read_csv将数据读入之后,可以通过coo_matrix快速将其转换成稀疏矩阵:矩阵的每行对应一位用户,每列对应一件商品,而元素值为用户对商品的评价。
dok_matrix(arg1, shape=None, dtype=None, copy=False)Dictionary Of Keys based sparse matrix. 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. dok_matrix从dict继承,它采用字典保存矩阵中不为0的元素:字典的键是一个保存元素(行,列)信息的元组,其对应的值为矩阵中位于(行,列)中的元素值。显然字典格式的稀疏矩阵很适合单个元素的添加、删除和存取操作。通常用来逐渐添加非零元素,然后转换成其它支持快速运算的格式。 lil_matrix(arg1, shape=None, dtype=None, copy=False)Row-based linked list sparse matrix 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适合逐渐添加元素} [scipy-ref-0.14.0 - Sparse matrices (scipy.sparse)] [用Python做科学计算-第二版SciPy-数值计算库-稀疏矩阵-sparse] 皮皮blog对于很多元素为零的稀疏矩阵,仅存储非零元素可使矩阵操作效率更高。 现有许多种稀疏矩阵的存储方式,但是多数采用相同的基本技术,即存储矩阵所有的非零元素到一个线性数组中,并提供辅助数组来描述原数组中非零元素的位置。 1. Coordinate Format (COO) 这种存储方式的主要优点是灵活、简单。仅存储非零元素以及每个非零元素的坐标。 使用3个数组进行存储:values, rows, andcolumn
2. Diagonal Storage Format (DIA) If the sparse matrix has diagonals containing only zero elements, then the diagonal storage format can be used to reduce the amount of information needed to locate the non-zero elements. This storage format is particularly useful in many applications where the matrix arises from a finite element or finite difference discretization. The Intel MKL diagonal storage format is specified by two arrays:values anddistance, and two parameters:ndiag, which is the number of non-empty diagonals, andlval, which is the declared leading dimension in the calling (sub)programs.
3. Compressed Sparse Row Format (CSR) The Intel MKL compressed sparse row (CSR) format is specified by four arrays: thevalues,columns,pointerB, andpointerE. The following table describes the arrays in terms of the values, row, and column positions of the non-zero elements in a sparse matrixA.
The compressed sparse column format (CSC) is similar to the CSR format, but the columns are used instead the rows. In other words, the CSC format is identical to the CSR format for the transposed matrix. The CSR format is specified by four arrays: values, columns, pointerB, and pointerE. The following table describes the arrays in terms of the values, row, and column positions of the non-zero elements in a sparse matrixA.
5. Skyline Storage Format The skyline storage format is important for the direct sparse solvers, and it is well suited for Cholesky or LU decomposition when no pivoting is required. The skyline storage format accepted in Intel MKL can store only triangular matrix or triangular part of a matrix. This format is specified by two arrays:values andpointers. The following table describes these arrays:
6. Block Compressed Sparse Row Format (BSR) The Intel MKL block compressed sparse row (BSR) format for sparse matrices is specified by four arrays:values,columns,pointerB, andpointerE. The following table describes these arrays.
7. ELLPACK (ELL) 8. Hybrid (HYB) 由ELL+COO两种格式结合而成。 皮皮blog
选择稀疏矩阵存储格式的一些经验: 1. DIA和ELL格式在进行稀疏矩阵-矢量乘积(sparse matrix-vector products)时效率最高,所以它们是应用迭代法(如共轭梯度法)解稀疏线性系统最快的格式; 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格式常用于读入数据后进行稀疏矩阵计算。 [Sparse Matrix Representations & Iterative Solvers, Lesson 1 by Nathan Bell] [稀疏线性系统 Sparse Linear Systems] [Intel MKL 库中使用的稀疏矩阵格式] |
|
来自: OneDayDayUp > 《Python》