Python NumPy matlib.eye() Function

NumPy matlib.eye() Function:

The matlib.eye() function of the NumPy module returns a matrix with 1’s on the diagonal and 0’s elsewhere.

Syntax:

numpy.matlib.eye(n, M=None, k=0, dtype='float', order='C')

Parameters

n:  This is required. The number of rows in the resultant matrix is denoted by n.
M: This is optional. The number of columns in the resultant matrix is denoted by M. The default value is n.
k: This is optional. This denotes the index of the diagonal. The main diagonal is represented by 0, an upper diagonal by a positive value, and a lower diagonal by a negative value.
dtype: This is optional. It denotes the output matrix data type. float is the default value.

order: This is optional. It Specifies whether or not the result should be stored. C (C-style) and F (Fortran-style) are two possible values. ‘C’ is the default value. That means it is the matrix’s insertion order.

Return Value:

A matrix with ones on the diagonal and zeros in the rest of the matrix is returned.

NumPy matlib.eye() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Import matlib function of numpy module using the import keyword.
  • Pass the number of rows(n) value as an argument to the matlib.eye() function of numpy module to create a matrix of given rows.
  • Store it in a variable.
  • Print the matrix with the given number of rows.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the number of rows(n) value as an argument to the matlib.eye() function
# of numpy module to create a matrix of given rows.
# Store it in a variable.
gvn_matrx = np.matlib.eye(n=3)
# Print the matrix with given number of rows
print("The matrix with given number of rows is:\n", gvn_matrx)

Output:

The matrix with given number of rows is:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Example2: Given n, M, k values

Approach:

  • Import numpy module using the import keyword.
  • Import matlib function of numpy module using the import keyword.
  • Pass the number of rows(n), columns(M), index of the diagonal values as the argument to the matlib.eye() function of numpy module to create a matrix of given rows, columns.
  • Store it in a variable.
  • Print the matrix with the given number of rows and columns.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the number of rows(n), columns, index of the diagonal values as the argument to 
# the matlib.eye() function of numpy module to create a matrix of given rows, columns.
# Store it in a variable.
gvn_matrx = np.matlib.eye(n=3, M=5, k=1)
# Print the matrix with given number of rows & columns
print("The matrix with given number of rows & columns is:\n", gvn_matrx)

Output:

The matrix with given number of rows & columns is:
[[0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]]

Example3

Given n, M, k, dtype values

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the number of rows(n), columns, index of the diagonal, dtype values as the argument to 
# the matlib.eye() function of numpy module to create a matrix of given rows, columns
# with integer values.
# Store it in a variable.
gvn_matrx = np.matlib.eye(n=3, M=5, k=0, dtype=int)
# Print the matrix with given number of rows & columns with integer values
print("The matrix with given number of rows & columns with integer values:\n", gvn_matrx)

Output:

The matrix with given number of rows & columns with integer values:
[[1 0 0 0 0]
 [0 1 0 0 0]
 [0 0 1 0 0]]