scipy 1.8.0 Pypi GitHub Homepage
Other Docs
BackRef

To remove in the future –– scipy.sparse

Sparse matrices (:mod:`scipy.sparse`)

.. currentmodule:: scipy.sparse
    

SciPy 2-D sparse array package for numeric data.

note

This package is switching to an array interface, compatible with NumPy arrays, from the older matrix interface. We recommend that you use the array objects (:None:None:`bsr_array`, :None:None:`coo_array`, etc.) for all new work.

When using the array interface, please note that:

The construction utilities (:None:None:`eye`, :None:None:`kron`, :None:None:`random`, :None:None:`diags`, etc.) have not yet been ported, but their results can be wrapped into arrays:

A = csr_array(eye(3))

Contents

Sparse array classes

.. autosummary:: 
    :toctree:generated/
    bsr_array - Block Sparse Row array
    coo_array - A sparse array in COOrdinate format
    csc_array - Compressed Sparse Column array
    csr_array - Compressed Sparse Row array
    dia_array - Sparse array with DIAgonal storage
    dok_array - Dictionary Of Keys based sparse array
    lil_array - Row-based list of lists sparse array

Sparse matrix classes

.. autosummary:: 
    :toctree:generated/
    bsr_matrix - Block Sparse Row matrix
    coo_matrix - A sparse matrix in COOrdinate format
    csc_matrix - Compressed Sparse Column matrix
    csr_matrix - Compressed Sparse Row matrix
    dia_matrix - Sparse matrix with DIAgonal storage
    dok_matrix - Dictionary Of Keys based sparse matrix
    lil_matrix - Row-based list of lists sparse matrix
    spmatrix - Sparse matrix base class

Functions

Building sparse matrices:

.. autosummary:: 
    :toctree:generated/
    eye - Sparse MxN matrix whose k-th diagonal is all ones
    identity - Identity matrix in sparse format
    kron - kronecker product of two sparse matrices
    kronsum - kronecker sum of sparse matrices
    diags - Return a sparse matrix from diagonals
    spdiags - Return a sparse matrix from diagonals
    block_diag - Build a block diagonal sparse matrix
    tril - Lower triangular portion of a matrix in sparse format
    triu - Upper triangular portion of a matrix in sparse format
    bmat - Build a sparse matrix from sparse sub-blocks
    hstack - Stack sparse matrices horizontally (column wise)
    vstack - Stack sparse matrices vertically (row wise)
    rand - Random values in a given shape
    random - Random values in a given shape

Save and load sparse matrices:

.. autosummary:: 
    :toctree:generated/
    save_npz - Save a sparse matrix to a file using ``.npz`` format.
    load_npz - Load a sparse matrix from a file using ``.npz`` format.

Sparse matrix tools:

.. autosummary:: 
    :toctree:generated/
    find

Identifying sparse matrices:

.. autosummary:: 
    :toctree:generated/
    issparse
    isspmatrix
    isspmatrix_csc
    isspmatrix_csr
    isspmatrix_bsr
    isspmatrix_lil
    isspmatrix_dok
    isspmatrix_coo
    isspmatrix_dia

Submodules

.. autosummary:: 
    csgraph - Compressed sparse graph routines
    linalg - sparse linear algebra routines

Exceptions

.. autosummary:: 
    :toctree:generated/
    SparseEfficiencyWarning
    SparseWarning

Usage information

There are seven available sparse matrix types:

  1. csc_matrix: Compressed Sparse Column format

  2. csr_matrix: Compressed Sparse Row format

  3. bsr_matrix: Block Sparse Row format

  4. lil_matrix: List of Lists format

  5. dok_matrix: Dictionary of Keys format

  6. coo_matrix: COOrdinate format (aka IJV, triplet format)

  7. dia_matrix: DIAgonal format

To construct a matrix efficiently, use either dok_matrix or lil_matrix. The lil_matrix class supports basic slicing and fancy indexing with a similar syntax to NumPy arrays. As illustrated below, the COO format may also be used to efficiently construct matrices. Despite their similarity to NumPy arrays, it is strongly discouraged to use NumPy functions directly on these matrices because NumPy may not properly convert them for computations, leading to unexpected (and incorrect) results. If you do want to apply a NumPy function to these matrices, first check if SciPy has its own implementation for the given sparse matrix class, or convert the sparse matrix to a NumPy array (e.g., using the :None:None:`toarray()` method of the class) first before applying the method.

To perform manipulations such as multiplication or inversion, first convert the matrix to either CSC or CSR format. The lil_matrix format is row-based, so conversion to CSR is efficient, whereas conversion to CSC is less so.

All conversions among the CSR, CSC, and COO formats are efficient, linear-time operations.

Matrix vector product

To do a vector product between a sparse matrix and a vector simply use the matrix :None:None:`dot` method, as described in its docstring:

>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
>>> v = np.array([1, 0, -1])
>>> A.dot(v)
array([ 1, -3, -1], dtype=int64)
warning

therefore using it will result on unexpected results or errors. The corresponding dense array should be obtained first instead:

>>> np.dot(A.toarray(), v)
array([ 1, -3, -1], dtype=int64)

but then all the performance advantages would be lost.

The CSR format is specially suitable for fast matrix vector products.

Example 1

Construct a 1000x1000 lil_matrix and add some values to it:

>>> from scipy.sparse import lil_matrix
>>> from scipy.sparse.linalg import spsolve
>>> from numpy.linalg import solve, norm
>>> from numpy.random import rand
>>> A = lil_matrix((1000, 1000))
>>> A[0, :100] = rand(100)
>>> A[1, 100:200] = A[0, :100]
>>> A.setdiag(rand(1000))

Now convert it to CSR format and solve A x = b for x:

>>> A = A.tocsr()
>>> b = rand(1000)
>>> x = spsolve(A, b)

Convert it to a dense matrix and solve, and check that the result is the same:

>>> x_ = solve(A.toarray(), b)

Now we can compute norm of the error with:

>>> err = norm(x-x_)
>>> err < 1e-10
True

It should be small :)

Example 2

Construct a matrix in COO format:

>>> from scipy import sparse
>>> from numpy import array
>>> I = array([0,3,1,0])
>>> J = array([0,3,1,2])
>>> V = array([4,5,7,9])
>>> A = sparse.coo_matrix((V,(I,J)),shape=(4,4))

Notice that the indices do not need to be sorted.

Duplicate (i,j) entries are summed when converting to CSR or CSC.

>>> I = array([0,0,1,3,1,0,0])
>>> J = array([0,2,1,3,1,0,0])
>>> V = array([1,1,1,1,1,1,1])
>>> B = sparse.coo_matrix((V,(I,J)),shape=(4,4)).tocsr()

This is useful for constructing finite-element stiffness and mass matrices.

Further details

CSR column indices are not necessarily sorted. Likewise for CSC row indices. Use the .sorted_indices() and .sort_indices() methods when sorted indices are required (e.g., when passing data to other libraries).

Examples

See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy

90 Elements
scipy.sparse._base.spmatrix.nonzero
scipy.sparse.csgraph._shortest_path.bellman_ford
scipy.sparse.linalg._dsolve.linsolve.spsolve
scipy.sparse._dia.isspmatrix_dia
scipy.sparse._coo.coo_matrix.tocsr
scipy.sparse._matrix_io.load_npz
scipy.sparse.linalg._isolve.iterative.gmres
scipy.sparse._csc.csc_matrix
scipy.sparse.csgraph._tools.csgraph_to_masked
scipy.sparse._dia.dia_matrix.diagonal
scipy.sparse._bsr.bsr_matrix
scipy.sparse.linalg._matfuncs.inv
scipy.sparse._arrays.dia_array
scipy.sparse.csgraph._shortest_path.shortest_path
scipy.sparse._construct.bmat
scipy.sparse._construct.spdiags
scipy.sparse._construct.eye
scipy.sparse._coo.coo_matrix.diagonal
scipy.sparse._construct.identity
scipy.sparse._csc.isspmatrix_csc
scipy.sparse.linalg._isolve.minres.minres
scipy.sparse.csgraph._tools.csgraph_from_masked
scipy.sparse.csgraph._tools.csgraph_from_dense
scipy.sparse.linalg._isolve.lsmr.lsmr
scipy.sparse._construct.rand
scipy.sparse.csgraph._laplacian.laplacian
scipy.sparse._coo.isspmatrix_coo
scipy.sparse.csgraph._traversal.depth_first_order
scipy.sparse.linalg._eigen.lobpcg.lobpcg.lobpcg
scipy.sparse._arrays.coo_array
scipy.sparse._construct.hstack
scipy.sparse.csgraph._traversal.connected_components
scipy.sparse.linalg._dsolve.linsolve.spilu
scipy.sparse._csc.csc_matrix.nonzero
scipy.sparse._dok.isspmatrix_dok
scipy.sparse.linalg._expm_multiply.expm_multiply
scipy.sparse._construct.random
scipy.sparse.linalg._dsolve.linsolve.splu
scipy.sparse.csgraph._shortest_path.johnson
scipy.sparse._construct.block_diag
scipy.sparse.linalg._matfuncs.expm
scipy.sparse._dia.dia_matrix
scipy.sparse.linalg._eigen.arpack.arpack.eigs
scipy.sparse._construct.kron
scipy.sparse._arrays.bsr_array
scipy.sparse.linalg._isolve.lgmres.lgmres
scipy.sparse.csgraph._shortest_path.dijkstra
scipy.sparse.csgraph._tools.reconstruct_path
scipy.sparse._construct.vstack
scipy.sparse._compressed._cs_matrix.diagonal
scipy.sparse._dok.dok_matrix
scipy.sparse._base.spmatrix.diagonal
scipy.sparse.csgraph._tools.csgraph_masked_from_dense
scipy.sparse._csr.csr_matrix
scipy.sparse.linalg._eigen.arpack.arpack.eigsh
scipy.sparse._csr.isspmatrix_csr
scipy.sparse.csgraph._reordering.reverse_cuthill_mckee
scipy.sparse.csgraph._reordering.structural_rank
scipy.sparse.linalg._eigen._svds.svds
scipy.sparse._construct.diags
scipy.sparse.linalg._isolve.lsqr.lsqr
scipy.sparse.linalg._isolve.tfqmr.tfqmr
scipy.sparse.linalg._norm.norm
scipy.sparse.linalg._interface.LinearOperator
scipy.sparse.linalg._interface.aslinearoperator
scipy.sparse._extract.tril
scipy.sparse._arrays.csr_array
scipy.sparse.linalg._isolve.iterative.qmr
scipy.sparse._arrays.csc_array
scipy.sparse._bsr.bsr_matrix.diagonal
scipy.sparse.csgraph._flow.maximum_flow
scipy.sparse.csgraph._traversal.breadth_first_order
scipy.sparse._arrays.dok_array
scipy.sparse._extract.triu
scipy.sparse._coo.coo_matrix
scipy.sparse._base.spmatrix.dot
scipy.sparse._extract.find
scipy.sparse.linalg._onenormest.onenormest
scipy.sparse.csgraph._shortest_path.floyd_warshall
scipy.sparse.csgraph._matching.min_weight_full_bipartite_matching
scipy.sparse.csgraph._matching.maximum_bipartite_matching
scipy.sparse.csgraph._tools.csgraph_to_dense
scipy.sparse._coo.coo_matrix.tocsc
scipy.sparse._matrix_io.save_npz
scipy.sparse._lil.isspmatrix_lil
scipy.sparse.linalg._dsolve.linsolve.factorized
scipy.sparse.linalg._dsolve.linsolve.spsolve_triangular
scipy.sparse._bsr.isspmatrix_bsr
scipy.sparse._base.isspmatrix
scipy.sparse.csgraph._tools.construct_dist_matrix

pandas

pandas.core.dtypes.common.is_scipy_sparse
pandas.core.dtypes.common.is_sparse
pandas.core.dtypes.common.is_extension_type

networkx

networkx.convert_matrix.from_scipy_sparse_matrix
networkx.convert_matrix.from_scipy_sparse_array

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/sparse/__init__.py#0
type: <class 'module'>
Commit: