scipy.sparse.

眼睛#

scipy.sparse.eye(m, n=None, k=0, dtype=<class 'float'>, format=None)[源代码][源代码]#

对角线上为1的稀疏矩阵

返回一个稀疏矩阵(m x n),其中第 k 条对角线全为 1,其余部分全为 0。

参数:
m整数

矩阵中的行数。

nint, 可选

列数。默认值:m

kint, 可选

对角线放置位置。默认值:0(主对角线)。

dtypedtype, 可选

矩阵的数据类型。

格式str, 可选

结果的稀疏格式,例如,format=”csr” 等。

.. warning::

此函数返回一个稀疏矩阵 – 而不是稀疏数组。建议使用 eye_array 以利用稀疏数组功能。

示例

>>> import numpy as np
>>> import scipy as sp
>>> sp.sparse.eye(3).toarray()
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> sp.sparse.eye(3, dtype=np.int8)
<DIAgonal sparse matrix of dtype 'int8'
    with 3 stored elements (1 diagonals) and shape (3, 3)>