Trusted answers to developer questions

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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:

: This represents the number of rows we want in the output array.`N`

: This represents the number of columns we want in the output array. This is optional.`M`

: This represents the index of the diagonal.`k`

`0`

is the default value and the main diagonal. This is optional.: This represents the data type of array to be returned. This is optional.`dtype`

: This represents whether the output should be stored in`order`

`C`

or`F`

order in memory. This is optional.: This is the array prototype or`like`

`array_like`

object.

The `numpy.eye()`

function returns a type of array where all the elements are equal to `0`

, except for the $k^{th}$ 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)

Implementing the numpy.eye() function

**Line 1**: We import the`numpy`

module.**Line 4**: We use the`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`

.**Line 6**: We print the variable`myarray`

.

import numpy as np# An array with 2 rows and integer data typemyarray = np.eye(2, dtype=int)print(myarray)

Implementing the numpy.eye() function

**Line 1**: We import the`numpy`

module.**Line 4**: We use the`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`

.**Line 6**: We print the variable`myarray`

.

RELATED TAGS

python

numpy

communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Related Courses