Python SciPy Sparse Module Learning notes

Source: Internet
Author: User

1. The official document of the sparse module address : http://docs.scipy.org/doc/scipy/reference/sparse.html 2. sparse matrix has many types of storage, see this post http://blog.csdn.net/anshan1984/article/details/8580952The different storage forms in the sparse module correspond to the following:Bsr_matrix (arg1[, Shape, dtype, copy, blocksize]) Block Sparse Row MatrixCoo_matrix (arg1[, Shape, Dtype, copy]) A sparse matrix in coordinate format.Csc_matrix (arg1[, Shape, Dtype, copy]) compressed Sparse Column matrixCsr_matrix (arg1[, Shape, Dtype, copy]) compressed Sparse Row matrixDia_matrix (arg1[, Shape, Dtype, copy]) Sparse matrix with DIAgonal storageDok_matrix (arg1[, Shape, Dtype, copy]) Dictionary of Keys based sparse matrix.Lil_matrix (arg1[, Shape, Dtype, copy]) row-based linked list sparse matrix 3. to transform a normal non-sparse matrix into a sparse matrix of the corresponding storage form as follows: (Take Coo_matrix as an example)A = Coo_matrix ([[1,2],[3,4]])or according to the requirements of the corresponding storage form, feed the parameters, construct the matrix, take the COO as an example:>>> 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_matrix (data, (Row,col)), shape= (bis)). Todense ()Matrix ([[3, 0, 1, 0],[0, 2, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1]])4. The Hstack and Vstack functions can merge the sparse matrix horizontally or vertically , such as:>>> from Scipy.sparse import Coo_matrix, Vstack>>> A = Coo_matrix ([[1,2],[3,4]])>>> B = Coo_matrix ([[5,6]])>>> Vstack ([A, b]). Todense ()Matrix ([[1, 2],[3, 4],[5, 6]])However, after testing, if the data form A and B are different, they cannot be merged. For example, a stores a string, B is a number, then it cannot be merged. This means that the data format in a matrix must be the same.
5. diags function to establish sparse diagonal matrices  6. For most of the storage formats (which appear to be outside the COO) of sparse matrices, slice operations can be performed, such as for CSC,CSR. can also be arithmeticoperations, the matrix of subtraction, fast. Take the specified number of columns of the matrix, such as the 1,3,8 column of the Matrix: matrix[:,[0,2,7]]  reading of the 7.sparce matrix. Can be read as a regular matrix by subscript . You can also read a specific column or a specific row by GetRow (i), Gecol (i), and nonzero () to read the position of a non-0 element.

Python SciPy Sparse Module Learning notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.