numpy.identity() in Python (original) (raw)
Last Updated : 22 Apr, 2025
numpy.identity()
function is used to create an **identity matrix which is used to make identity matrix. This is commonly used in linear algebra and numerical computations. It has the following properties:
- Diagonal elements are all 1s.
- Non-diagonal elements are all 0s.
**Syntax: numpy.identity(n, dtype=None)
where:
- **n : It takes int value and is Dimension n x n of output array
- **dtype : It returns Data type of returned array. It is optional and by default it is float.
In the below example we use numpy.identity()
to create identity matrices of size 2×2 and 4×4 with 1
s on the diagonal and 0
s elsewhere. The dtype=float
specifies that the matrix elements should be float type.
Python `
import numpy as geek
b = geek.identity(2, dtype = float) print("Matrix b : \n", b)
a = geek.identity(4) print("\nMatrix a : \n", a)
`
**Output :
Identity Matrix
It is u
seful for linear algebra operations like matrix multiplication, transformations and solving equations.
Similar Reads
- numpy.eye() in Python numpy.eye() is a function in the NumPy library that creates a 2D array with ones on the diagonal and zeros elsewhere. This function is often used to generate identity matrices with ones along the diagonal and zeros in all other positions. Let's understand with the help of an example: [GFGTABS] Pytho 2 min read
- numpy.alen() in Python numpy.alen() function is used to return the length of the first dimension of the input array. Syntax : numpy.alen(arr) Parameters : arr : [array_like] Input array. Return : [int]Length of the first dimension of arr. Code #1 : # Python program explaining # alen() function import numpy as geek # input 1 min read
- numpy.isnan() in Python The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax : numpy.isnan(array [, out]) Parameters : array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array placed with 2 min read
- numpy.index() in Python numpy.core.defchararray.index(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range But if substring is not found, it raises ValueError. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, option 1 min read
- numpy.add() in Python NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays. numpy.add() SyntaxSyntax : numpy.add(arr1, arr2, /, out= 4 min read
- NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read
- numpy.i0() function | Python numpy.i0() function is the modified Bessel function of the first kind, order 0. it's usually denoted by I0. Syntax : numpy.i0(x) Parameters : x : [array_like, dtype float or complex] Argument of the Bessel function. Return : [ndarray, shape = x.shape, dtype = x.dtype] The modified Bessel function ev 1 min read
- numpy.iinfo() function – Python numpy.iinfo() function shows machine limits for integer types. Syntax : numpy.iinfo(dtype) Parameters : dtype : [integer type, dtype, or instance] The kind of integer data type to get information about. Return : Machine limits for integer types. Code #1 : # Python program explaining # numpy.iinfo() 1 min read
- numpy.multiply() in Python The numpy.multiply() is a numpy function in Python which is used to find element-wise multiplication of two arrays or scalar (single value). It returns the product of two input array element by element. Syntax: numpy.multiply(arr1, arr2, out=None, where=True, casting='same_kind', order='K', dtype=No 3 min read
- numpy.nonzero() in Python numpy.nonzero()function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(ar 2 min read