📅  最后修改于: 2023-12-03 14:46:01.208000             🧑  作者: Mango
The numpy.matlib.eye()
function in Python is used to create a 2-D identity matrix or a 2-D array with ones on the diagonal and zeros elsewhere. It is a part of the numpy.matlib
module.
numpy.matlib.eye(n, M=None, k=0, dtype=<class 'float'>, order='C')
n
(square matrix).import numpy as np
# Create a 2x2 identity matrix
matrix1 = np.matlib.eye(2)
print(matrix1)
Output:
[[1. 0.]
[0. 1.]]
In the above example, numpy.matlib.eye(2)
creates a 2x2 identity matrix with ones on the main diagonal and zeros elsewhere.
import numpy as np
# Create a 3x3 identity matrix
matrix2 = np.matlib.eye(3)
print(matrix2)
Output:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
import numpy as np
# Create a 4x5 identity matrix
matrix3 = np.matlib.eye(4, 5)
print(matrix3)
Output:
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]]
The numpy.matlib.eye()
function is a useful tool when you need to create identity matrices in Python. It simplifies the task of generating arrays or matrices with ones on the diagonal and zeros elsewhere.