numpy.eye(N, M=None, k=0, dtype=, order='C')
Return ndarray ( N, M ) shape.
N | Int, number of rows |
M | Int (optional ), number of columns ( default is equal to N ) |
k | Int (optional ), default is 0,Position of diagonal, Positive value for upper and negative for lower diagonal |
dtype | data-type( Optional ), Data Type of returned array. |
order | {'C','F'} Optional, how the output is to be stored. C- style or Fortan style |
![]() | ![]() |
| Shape: (3, 4) Dimension 2 | Shape: (4, 3) Dimension 2 |
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.]]
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.]]
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()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.