📜  numpy 矩阵运算 |身份()函数

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

numpy 矩阵运算 |身份()函数

numpy.matlib.identity()是另一个在 numpy 中进行矩阵运算的函数。它返回给定输入大小的平方单位矩阵。

代码#1:

# Python program explaining
# numpy.matlib.identity() function
  
# importing matrix library from numpy
import numpy as geek
import numpy.matlib
  
# desired 3 x 3 output square identity matrix 
out_mat = geek.matlib.identity(3) 
print ("Output matrix : ", out_mat) 
输出 :
Output matrix :  
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

代码#2:

# Python program explaining
# numpy.matlib.identity() function
  
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
  
# desired 5 x 5 output square identity matrix 
out_mat = geek.matlib.identity(n = 5, dtype = int) 
print ("Output matrix : ", out_mat) 
输出 :
Output matrix :  
[[1 0 0 0 0]
 [0 1 0 0 0]
 [0 0 1 0 0]
 [0 0 0 1 0]
 [0 0 0 0 1]]