Incremental assembly
Build a sparse matrix by assigning entries progressively, especially when the final nonzero pattern is not known up front.
SciPy · Class
scipy.sparse.lil_matrix is a row-based sparse matrix class suited to incremental construction. It accepts dense or sparse input, or a shape for creating an empty matrix.
lil_matrix stores a two-dimensional sparse matrix in row-oriented lists, making incremental construction convenient. It can be initialized from dense or sparse data, or from a shape; after construction, convert it to another sparse format when arithmetic or computation requires a different representation.
scipy.sparse.lil_matrix(arg1, shape=None, dtype=None, copy=False, *, maxprint=None)| Name | Type | Description |
|---|---|---|
arg1 | array_like, sparse array/matrix, or tuple | Input data, another sparse object, or a shape specification such as (M, N). |
shape | 2-tuple of int, optional | Matrix shape when supplied separately. |
dtype | dtype, optional | Data type of stored values; if omitted, the documented default is dtype='d'. |
copy | bool, optional | Whether to copy input data when constructing from an existing object. |
maxprint | int, optional | Maximum number of items printed in a representation. |
A scipy.sparse.lil_matrix instance representing the supplied data or requested shape.
from scipy.sparse import lil_matrix
A = lil_matrix((3, 4), dtype=float)
A[0, 1] = 2.5
A[2, 3] = 7
print(A.toarray())
print(A.nnz)Creates an empty 3-by-4 LIL matrix, assigns two entries, and converts it to a dense array for display.
Build a sparse matrix by assigning entries progressively, especially when the final nonzero pattern is not known up front.
Convert a dense two-dimensional array or another sparse representation into row-oriented LIL storage.
Create a correctly shaped sparse matrix before populating selected row and column positions.
lil_matrix is a row-based list-of-lists sparse matrix intended for incremental construction. It can be initialized from a two-dimensional dense array, another sparse matrix, or a shape tuple.
Follow-up: Which insertion strategy improves construction speed? Pre-sort inserted indices by row and column so additions avoid unnecessary list-search work.
Use lil_matrix((M, N), dtype=...) to create an empty matrix with the requested dimensions and data type. Assign entries by row and column, then use a suitable sparse conversion for later computation.
Follow-up: Why not use LIL for every sparse operation? Its row-list representation is construction-oriented; another sparse format may be more appropriate for computation.
The nnz attribute reports the number of stored values, and the documentation explicitly includes stored zeros in that count. Therefore nnz is not necessarily the count of mathematically nonzero values.
Follow-up: How would you inspect the actual dense values? Convert the matrix with toarray(), while remembering that dense conversion can require substantially more memory.
scipy.sparse.csr_matrixscipy.sparse.csc_matrixscipy.sparse.coo_matrixscipy.sparse.dok_matrixscipy.sparse.issparse