We can create identity like array using np.eye() which gives more flexibility.
Eye
Function
•
We can create identity like array using np.eye()
which gives more flexibility. The syntax is np.eye(N, M=None, k=0, dtype=float)
Where
N: Number of rows
M: Number of
columns
k:
This parameter in np.eye() determines where the diagonal of 1s will be placed
in the array. The k = 0 means Main diagonal (default), the k > 0 means Moves
the diagonal up (above the main diagonal), the k < 0 means Moves the
diagonal down (below the main diagonal).
•
Following Python code fragments illustrate the use of eye function
In [10]:
import numpy
as np
np.eye(4, 4, k=0)
Out[10]:
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
In [11]:
import
numpy as np
np.eye(4, 4, k=1)
Out[11]:
array([[0., 1., 0., 0.],
[0., 0., 1., 0.1],
[0., 0., 0., 1.],
[0., 0., 0., 0.]])
In [12]:
import numpy as np
np.eye(4, 4, k= ‒1)
Out[12]:
array([[0., 0., 0., 0.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - NumPy: Eye Function
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation