Numpy.eye() method **“returns a 2D array with 1’s as the diagonal and 0’s**** elsewhere.”** The diagonal can be main, upper, or lower depending on the optional parameter** k**. A positive **k** is for the upper diagonal, a negative **k **is for the lower, and a** 0 k **(default) is for the main diagonal.

**Syntax**

`numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')`

**Parameters**

**N:**It represents several rows in a 2D array.**M:**It represents the number of columns. It is an optional argument; by default, it equals the value N i.e., several rows.**k:**This argument is used for representing the Diagonal. It is entirely optional.

By default, it is equal to 0. A positive value (k>0) refers to an upper diagonal, and a negative value(k<0) to a lower diagonal.

**dtype:**It denotes the data type of the returned array. It is entirely optional, and by default, it is a float.**order:**The order parameter can be either C_contiguous or F_contiguous.

**Return Value**

The numpy.eye() method returns a 2D array of shape, N x M, i.e., a matrix where all elements are equal to zero, except for the kth diagonal, whose values are equal to one.

**Example 1: How to Use np.eye() Method**

```
import numpy as np
# 2x2 matrix with 1's on main diagonal
obj1 = np.eye(2, dtype=float)
print("Matrix : \n", obj1)
# matrix with Row=2 Column=3 and diagonal=1
obj2 = np.eye(2, 3, k=1)
print("\nMatrix : \n", obj2)
# matrix with Row=3 Column=3 and diagonal=-1
obj2 = np.eye(3, 3, k=1)
print("\nMatrix : \n", obj2)
```

**Output**

```
Matrix :
[[1. 0.]
[0. 1.]]
Matrix :
[[0. 1. 0.]
[0. 0. 1.]]
Matrix :
[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
```

**Example 2: Identity Matrix using Numpy.eye() function**

```
import numpy as np
b = np.eye(2, dtype = float)
print("Matrix b : \n", b)
arr = np.eye(4, 5, k = -1)
print("\nMatrix arr : \n", arr)
```

**Output**

```
Matrix b :
[[1. 0.]
[0. 1.]]
Matrix arr :
[[0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]]
```

That’s it.

Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.