AppDividend
Latest Code Tutorials

np.eye: Create 2D Array with Ones on the Diagonal and Zeros

The np.eye() function returns a 2D array with  1’s as the diagonal and  0’s elsewhere with specified size in Python. It returns an array where all items are equal to zero, except for the k-th diagonal, whose values are equal to one.

The diagonal can be main, upper, or lower depending on the optional parameter k. A positive k is for the upper diagonal, a negative is for the lower, and a  0 k is for the main diagonal. If the value is positive, it refers to the upper diagonal, and the negative value represents the lower diagonal.

Syntax

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

Arguments

N – It is the number of rows needed to be generated from the function. It is a required argument. 

M – It is the number of columns needed to be generated from the function. By default, it is assigned as None. If None is assigned, then it creates an X n matrix. The value assigned to n will be assigned to M.

K – It is the index of the diagonal. By default, it is assigned to 0. Hence the diagonal starting from the 0th index will be assigned with 1. If the value is positive, then it refers to the upper diagonal. If the value is less than zero, it refers to the lower diagonal.

Dtype – It stands for the data type. By default, it is assigned as a float. Therefore, all the values generated in the array will be of this mentioned data type.

Order – It has two values, C and F. If it is C, it is stored in row-major. If it is F, then it is stored in column-major. 

Like – If an array style is passed in the like argument, the array will be generated only when the generated output is like the given argument.

Program for creating eye matrix using np.eye() function

import numpy as np

# Creating a 3 X 3 identical array using eye function
mat = np.eye(3)

# Printing the array
print(mat)

# Printing the shape of the array
print(mat.shape)

Output

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

(3, 3)

We imported a numpy package for working with numerical calculations in this program. Then, we created a 3 X 3 identical array using an np.eye() function. An identical matrix is a kind of matrix in which the middle diagonal of the array is 1 and all other elements are 0.

Hence, we created a 2D array using the numpy eye() method by passing 3 for the N argument. To print the shape of an array in Python, use the np.shape property.

Passing order argument

import numpy as np

# Creating a 3 X 3 identical matrix using eye function
mat = np.eye(4, 5, 2, order='F')

# Printing the array
print(mat)

# Printing the shape of the array
print(mat.shape)

Output

[[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]
[0. 0. 0. 0. 0.]]

(4, 5)

In this program, we passed the value of N as 4, and for M, we have given 5, and for k, we passed the value as 2. Then, in order of argument, we used F.

That’s it for this tutorial.

Related posts

np.diag

np.diag_indices

np.diagflat

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.