SciPy

scipy.sparse.eye

scipy.sparse.eye(m, n=None, k=0, dtype=<type 'float'>, format=None)[source]

Sparse matrix with ones on diagonal

Returns a sparse (m x n) matrix where the k-th diagonal is all ones and everything else is zeros.

Parameters:

m : int

Number of rows in the matrix.

n : int, optional

Number of columns. Default: m.

k : int, optional

Diagonal to place ones on. Default: 0 (main diagonal).

dtype : dtype, optional

Data type of the matrix.

format : str, optional

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

Examples

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