scipy.sparse.
identity#
- scipy.sparse.identity(n, dtype='d', format=None)[來源]#
稀疏格式的單位矩陣
返回一個形狀為 (n,n) 的單位矩陣,使用給定的稀疏格式和 dtype。這與
eye_array
的不同之處在於它具有正方形形狀,且只有主對角線上為 1。因此它是乘法單位矩陣。eye_array
允許矩形形狀,並且對角線可以從主對角線偏移。警告
此函數返回一個稀疏矩陣 – 而不是稀疏陣列。 建議您使用
eye_array
以利用稀疏陣列功能。- 參數:
- nint
單位矩陣的形狀。
- dtypedtype, optional
矩陣的資料類型
- formatstr, optional
結果的稀疏格式,例如 format="csr" 等。
範例
>>> 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)>