- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Return a 2-D array with ones on the diagonal and zeros elsewhere and also set the number of columns in Numpy

The **numpy.eye()** returns a 2-D array with 1’s as the diagonal and 0’s elsewhere. Here, the 1st parameter means the "Number of rows in the output" i.e. 4 means 4x4 array. The 2nd parameter is the number of columns in the output.

The function eye() returns an array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one. The dtype is the data-type of the returned array. The order suggests whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.

The like parameter is a reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

## Steps

At first, import the required library −

import numpy as np

Create a 2d array. The numpy.eye() returns a 2-D array with 1’s as the diagonal and 0’s elsewhere. Here, the 1st parameter means the "Number of rows in the output" i.e. 4 means 4x4 array. The 2nd parameter is the number of columns in the output. We have set 3 here −

arr = np.eye(4, 3)

Display the array −

print("Array...

", arr)

Get the type of the array −

print("

Array type...

", arr.dtype)

Get the shape of the array −

print("

Array shape...

", arr.shape)

Get the dimensions of the Array −

print("

Array Dimensions...

",arr.ndim)

Get the number of elements in the Array −

print("

Array (count of elements)...

",arr.size)

## Example

import numpy as np # Create a 2d array # The numpy.eye() returns a 2-D array with 1’s as the diagonal and 0’s elsewhere. # Here, the 1st parameter means the "Number of rows in the output" i.e. 4 means 4x4 array # The 2nd parameter is the number of columns in the output. We have set 3 here arr = np.eye(4, 3) # Display the array print("Array...

", arr) # Get the type of the array print("

Array type...

", arr.dtype) # Get the shape of the array print("

Array shape...

", arr.shape) # Get the dimensions of the Array print("

Array Dimensions...

",arr.ndim) # Get the number of elements of the Array print("

Array (count of elements)...

",arr.size)

## Output

Array... [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.] [0. 0. 0.]] Array type... float64 Array shape... (4, 3) Array Dimensions... 2 Array (count of elements)... 12

- Related Articles
- Return a 2-D array with ones on the diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the lower diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere but set a different datatype in Numpy
- Create an array with ones above the main diagonal and zeros elsewhere in Numpy
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy
- Return a new array of given shape filled with zeros and also set the desired datatype in Numpy
- Return a new array of given shape filled with zeros and also set the desired output in Numpy
- Return a new array of given shape filled with ones and also set the desired data-type in Numpy
- Return an array of ones with the same shape and type as a given array in Numpy
- Return specified diagonals and set the offset of the diagonal from the main diagonal in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Return the Upper triangle of an array and set the diagonal above to zero elements in Numpy