In this article, we will try to understand the identity function of NumPy in Python.
The Python package NumPy (Numerical Python) is used to manipulate arrays. Numerous mathematical operations can be carried out on an array with NumPy.
It provides a vast library of high-level mathematical functions that work on these arrays and matrices and strong data structures that ensure efficient calculations with arrays and matrices.
In the year 2005, Travis Oliphant developed NumPy. You can use it for free because it is an open-source project.
Also read: Numpy Gradient: Returning the Gradient of N-dimensional Array
What is numpy.identity()?
This function is used to return a square array (an array with equal numbers of rows and columns) with ones on its main diagonal, this kind of array is known as an identity array.
Syntax of numpy.identity()
numpy.identity(n, dtype=None, like=None)
- n: int
- Number of rows or columns of the required output array
- dtype: data-type
- The data type of the values in the output array,
- By default set to float.
- like: array_like
- Arrays that are not NumPy arrays can be created using a reference object. The outcome will be determined by an array-like provided in as like if it complies with the array function protocol. In this instance, it makes sure that an array object is created that is compatible with the one that was provided as an argument.
Returns a n x n symmetric array, with its main diagonal set to ones and all remaining elements set to zeros.
Implementation of numpy.identity()
Make sure to import the NumPy package in your IDE before using this function. To import the NumPy package run the following line of code.
import numpy as np
Example 1: passing only the ‘n’ parameter
Example 2: passing other parameters
np.identity(3, dtype=int) np.identity(3, dtype=complex)
Working on arrays becomes easy by using the NumPy package in Python. The identity() function is a simple way to create a n x n identity matrix/array.