numpy.eye () in Python



numpy.eye (R, C = No, k = 0, dtype = type & lt; & # 39; float & # 39; & gt;): Returns a matrix with 1 on the diagonal and 0 elsewhere relative to k
Parameters :

  R:  Number of rows  C:  [optional] Number of columns; By default M = N  k:  [int, optional, 0 by default] Diagonal we require; k & gt; 0 means diagonal above main diagonal or vice versa.  dtype:  [optional, float (by Default)] Data type of returned array. 

Returns :

 array of shape, R x C, an array where all elements are equal to zero, except for the k-th diagonal, whose values ​​are equal to one. 

# Python Programming Illustrative
# numpy.eye method

 

import numpy as geek

  
Matrix # 2x2 with 1 on the main diagnosis

b = geek.eye ( 2 , dtype = float )

print ( < code class = "string"> "Matrix b:" , b)

  
# matrix with R = 4 C = 5 and 1 for diagnosis
# below the main diagnosis

a = geek.eye ( 4 , 5 , k = - 1 )

print ( "Matrix a:" , a)

Output:

 Matrix b: [[1. 0.] [0. 1.]] Matrix a: [[0. 0. 0. 0. 0 .] [1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.]] 

Notes :
These codes will not work for an online ID. Please run them on your systems to see how they work

This article is courtesy of Mohit Gupta_OMG