scipy.sparse.

identity#

scipy.sparse.identity(n, dtype='d', format=None)[源代码]#

稀疏格式的单位矩阵

返回一个使用给定稀疏格式和数据类型,形状为 (n, n) 的单位矩阵。这与 eye_array 不同之处在于它是一个仅主对角线上为1的方阵。因此它是乘法单位元。eye_array 允许矩形形状,且对角线可以偏离主对角线。

警告

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

参数
nint

单位矩阵的形状。

dtypedtype, 可选

矩阵的数据类型

formatstr, 可选

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

返回
new_matrix稀疏矩阵

一个主对角线上为1,其余位置为0的方稀疏矩阵。

另请参阅

eye_array

具有指定对角线上为1的选定形状的稀疏数组。

eye

具有指定对角线上为1的选定形状的稀疏矩阵。

示例

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