close
The Wayback Machine - https://web.archive.org/web/20240806130704/https://www.geeksforgeeks.org/numpy-eye-python/
Open In App

numpy.eye() in Python

Last Updated : 04 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

numpy.eye(R, C = None, k = 0, dtype = type <‘float’>) : –The eye tool returns a 2-D 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.

Parameters : 

R : Number of rows
C : [optional] Number of columns; By default M = N
k : [int, optional, 0 by default]
          Diagonal we require; k>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.

Example:

Python




# Python Programming illustrating
# numpy.eye method
 
import numpy as geek
 
# 2x2 matrix with 1's on main diagonal
b = geek.eye(2, dtype = float)
print("Matrix b : \n", b)
 
# matrix with R=4 C=5 and 1 on diagonal
# below main diagonal
a = geek.eye(4, 5, k = -1)
print("\nMatrix a : \n", 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.]]

Note : 
These codes won’t run on online-ID. Please run them on your systems to explore the working.

 


Previous Article
Next Article

Similar Reads

numpy matrix operations | eye() function
numpy.matlib.eye() is another function for doing matrix operations in numpy. It returns a matrix with ones on the diagonal and zeros elsewhere. Syntax : numpy.matlib.eye(n, M=None, k=0, dtype='float', order='C') Parameters : n : [int] Number of rows in the output matrix. M : [int, optional] Number of columns in the output matrix, defaults is n. k :
2 min read
Python | sympy.eye() method
With the help of sympy.eye() method, we can find the identity matrix by using sympy.eye() method. Syntax : sympy.eye() Return : Return an identity matrix. Example #1 : In this example, we can see that by using sympy.eye() method, we are able to find identity matrix having dimension nxn, where n will be pass as a parameter. # import sympy from sympy
1 min read
Python Pytorch eye() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.eye() returns a returns a 2-D tensor of size n*m with ones on the diagonal and zeros elsewhere. Syntax: torch.eye(n, m, out=None) Parameters: n: the number of rows m: the number of
1 min read
Python - tensorflow.eye()
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. tensorflow.eye() is used to generate identity matrix. Syntax: tensorflow.eye( num_rows, num_columns, batch_shape, dtype, name) Parameters: num_rows: It is int32 scalar Tensor which defines the number of rows to be presen
2 min read
Eye blink detection with OpenCV, Python, and dlib
In this article, we are going to see how to detect eye blink using OpenCV, Python, and dlib. This is a fairly simple task and it requires you to have a basic understanding of OpenCV and how to implement face landmark detection programs using OpenCV and dlib, since we'll be using that as the base for today's project. Stepwise ImplementationStep 1: I
6 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self&lt;value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self&gt;value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self&lt;=value Example #1 : In this example we
1 min read
Practice Tags :
three90RightbarBannerImg