Numpy eye()

numpy.eye(N, M=None, k=0, dtype=, order='C')
Return ndarray ( N, M ) shape.
NInt, number of rows
MInt (optional ), number of columns ( default is equal to N )
kInt (optional ), default is 0,Position of diagonal,
Positive value for upper and negative for lower diagonal
dtypedata-type( Optional ), Data Type of returned array.
order{'C','F'} Optional, how the output is to be stored. C- style or Fortan style
Eye array of shape(3,4)Eye array of  shape(4,3)
Shape: (3, 4)
Dimension 2
Shape: (4, 3)
Dimension 2

Using N and M

import numpy as np
my_data=np.eye(3)
print(my_data)
Output, we used N=3 here, so default value of M is also 3.
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Let us use different value for M
my_data=np.eye(4,M=3)
Output
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]
 [0. 0. 0.]]

Using k

We can use k to change the postion of diagonal. Try with positive value of key
my_data=np.eye(4,k=2)
Output
[[0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
Let us try with negative value of k
my_data=np.eye(4,k=-1)
Output
[[0. 0. 0. 0.]
 [1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]

dtype

We will try to return string dtype
my_data=np.eye(4,dtype=str)
Output
[['1' '' '' '']
 ['' '1' '' '']
 ['' '' '1' '']
 ['' '' '' '1']]
dtype=float
my_data=np.eye(4,dtype=float)
Output
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
Numpy ones() bincount() arange() linspace()
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer