📜  Python中的 numpy.identity()

📅  最后修改于: 2022-05-13 01:55:01.497000             🧑  作者: Mango

Python中的 numpy.identity()

numpy.identity(n, dtype = None) :返回一个单位矩阵,即一个在主对角线上有一个的方阵。

Parameters : 
n     : [int] Dimension n x n of output array  
dtype : [optional, float(by Default)] Data type of returned array.  
Returns : 
identity array of dimension n x n,  with its main diagonal set to one, and all other elements 0.

例子:

Python
# Python Programming illustrating
# numpy.identity method
 
import numpy as geek
 
# 2x2 matrix with 1's on main diagonal
b = geek.identity(2, dtype = float)
print("Matrix b : \n", b)
 
 
a = geek.identity(4)
print("\nMatrix a : \n", a)


输出 :

Matrix b : 
 [[ 1.  0.]
 [ 0.  1.]]

Matrix a : 
 [[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]