The numpy.eye()
function in Python is used to return a two-dimensional array with ones (1) on the diagonal and zeros (0) elsewhere.
numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, like=None)
The numpy.eye()
function takes the following parameter values:
N
: This represents the number of rows we want in the output array.M
: This represents the number of columns we want in the output array. This is optional.k
: This represents the index of the diagonal. 0
is the default value and the main diagonal. This is optional.dtype
: This represents the data type of array to be returned. This is optional.order
: This represents whether the output should be stored in C
or F
order in memory. This is optional.like
: This is the array prototype or array_like
object.The numpy.eye()
function returns a type of array where all the elements are equal to 0
, except for the diagonal, whose values are equal to 1
.
import numpy as np# An array with 3 rows with with the ones starting at the index i.e fron the second columnmyarray = np.eye(3, k=1)print(myarray)
numpy
module.numpy.eye()
function to create an array with 3
rows and the ones should start on index 1
of the array. The output is stored in a variable myarray
.myarray
.import numpy as np# An array with 2 rows and integer data typemyarray = np.eye(2, dtype=int)print(myarray)
numpy
module.numpy.eye()
function to create an array with 2
rows. The data type of the array should be integer. The output is stored in a variable myarray
.myarray
.