- 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 the identity array and change the datatype of the output in Numpy
To return the identity array, use the numpy.identity() method in Python Numpy. The identity array is a square array with ones on the main diagonal. The 1s parameter is the number of rows (and columns) in n x n output. The "dtype" parameter is used to return the data-type of the output.
The function returns n x n array with its main diagonal set to one, and all other elements 0. 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. To return the identity array, use the numpy.identity() method in Python Numpy. The "dtype" parameter is used to return the data-type of the output −
arr = np.identity(4, dtype = int)
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 # To return the identity array, use the numpy.identity() method in Python Numpy # The identity array is a square array with ones on the main diagonal. # The 1s parameter is the number of rows (and columns) in n x n output # The "dtype" parameter is used to return the ata-type of the output. arr = np.identity(4, dtype = int) # 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] [0 1 0 0] [0 0 1 0] [0 0 0 1]] Array type... int64 Array shape... (4, 4) Array Dimensions... 2 Array (count of elements)... 16
- Related Articles
- Return the identity array in Numpy
- Get the datatype of a masked array in NumPy
- Return the default fill value for a masked array with float datatype in Numpy
- Return the default fill value for a masked array with complex datatype 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 the transpose of the masked array in NumPy
- Return the floor of the array elements in Numpy
- Return the length of the masked array in Numpy
- Return a new array with the same shape and type as a given array and change the order in Numpy
- Return a new Three-Dimensional array without initializing entries and change the order in Numpy
- Return a new array of given shape without initializing entries and change the default type in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere but set a different datatype in Numpy
- Return the truncated value of the array elements in Numpy
- Return the average of the masked array elements in Numpy
