numpy.eye() in Python: Creating Identity Matrix

The numpy.eye() function in the NumPy library is used to create a 2-D identity matrix with ones on the main diagonal (from the top-left to the bottom-right) and zeros elsewhere.

In this article, we’ll understand look at five examples of creating an identity matrix by using numpy.eye() function. Let’s first look at this function’s syntax, parameters and return value.

Also Read: numpy.cbrt() in Python

Syntax of numpy.eye() Function

Below is the syntax for using the numpy.eye() function in Python.

Syntax:

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

Parameters:

  • N: The number of rows (and columns) in the output matrix.
  • M (optional): The number of columns in the output matrix. If not provided, it defaults to N.
  • k (optional): The index of the diagonal. The main diagonal is denoted by k = 0, the diagonal above it by k > 0, and the diagonal below it by k < 0. The default value is k = 0.
  • dtype (optional): The data type of the output array. The default is a floating-point type.
  • order (optional): Specifies the memory layout of the output array. It can be ‘C’ (row-major, the default) or ‘F’ (column-major).

Return: An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

Examples of numpy.eye() Function

Let us now look at examples to demonstrate the use of numpy.eye() function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios. Let’s see each of them one by one.

Example 1: By Specifying N in numpy.eye() Function

In this example, we will only provide N (number of the rows) in the numpy.eye() function.

Example:

import numpy as np
np.eye(5)

In the above code, to use the eye() function we first imported the Numpy library as np then we provided parameter N which is 5 in the np.eye() function means we provided the number of rows for the output array.

Output:

Example By specifying N in numpy.eye() function

In the output above, we got a 5×5 matrix with ones on the main diagonal and zeros elsewhere.

Example 2: By Specifying N and M in numpy.eye() Function

In this example, we will provide N (number of rows) and M (number of columns) in the numpy.eye() function.

Example:

np.eye(2,3) 

In the above code, we provided parameters N as 2 and M as 3 in the np.eye() function means we provided the number of rows and the number of columns for the output array respectively.

Output:

Example By specifying N and M in numpy.eye() function

In the output above, we got a 2×3 matrix with ones on the main diagonal and zeros elsewhere.

Example 3: By Specifying N and k in numpy.eye() Function

In this example, we will provide N (number of rows) and k (index of the diagonal) in the numpy.eye() function.

Example:

np.eye(4,k=-1)

In the above code, within the np.eye() function we provided parameter N as 4 which is the number of rows in the output array and parameter k as -1 which means the diagonal has been one step downward from the default diagonal.

Output:

Example By specifying N and k in numpy.eye() function

In the output above, we got a 4×4 matrix where the diagonal has been one step downward from the default diagonal.

Example 4: By Specifying N and dtype in numpy.eye() Function

In this example, we will provide N (number of rows) and dtype (data type) in the numpy.eye() function.

Example:

np.eye(5,dtype=int)

In the above code, within the np.eye() function we provided parameter N as 5 which is the number of rows in the output array or matrix and parameter dtype as int which means we changed the datatype of values to an integer which was by default float datatype.

Output:

Example By specifying N and dtype in numpy.eye() function

In the output above, we got a 5×5 matrix where the datatype of the values changed to integer.

Example 5: By Specifying N, M, k, and dtype in numpy.eye() Function

In this example, we will provide all the parameters N, M, k, and dtype together in the numpy.eye() function.

Example:

np.eye(6,9,k=3,dtype=int)

In the above code, within the np.eye() function we provided N as 6 and M as 9 which are the number of the rows and the number of columns. Also, we provided K as 3 which means diagonal three steps upward to the default diagonal and we mentioned dtype as int which changed the datatype of values in the output matrix from float to integer.

Output:

Example By specifying N, M, k, and dtype in numpy.eye() function

In the output above, we got a 6×9 matrix with a diagonal of three steps upward to the default diagonal and the datatype of the values is integers.

Summary

In this tutorial, we have discussed numpy.eye() function provided by Python’s NumPy library, its syntax, parameters, and return value and also explored five examples to create an identity matrix by passing different parameters. After reading this tutorial, we hope you can easily create an identity matrix by using numpy.eye() function in Python.

Reference

https://stackoverflow.com/questions/46126914/what-does-np-eyennparray-mean

Priyanshu Singh
Priyanshu Singh
Articles: 44