scipy.sparse.identity

scipy.sparse.identity(n, dtype='d', format=None)[source]

Identity matrix in sparse format

Returns an identity matrix with shape (n,n) using a given sparse format and dtype.

Parameters
nint

Shape of the identity matrix.

dtypedtype, optional

Data type of the matrix

formatstr, optional

Sparse format of the result, e.g., format=”csr”, etc.

Examples

>>> from scipy.sparse import identity
>>> identity(3).toarray()
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> identity(3, dtype='int8', format='dia')
<3x3 sparse matrix of type '<class 'numpy.int8'>'
        with 3 stored elements (1 diagonals) in DIAgonal format>