BetterDocs
Home
Docs

Creation | np.identity()

Method:

np.identity(n, dtype=np.float64, *, like=None)

Returns a 2D identity matrix of size n x n.

Returns:

np.ndarray

Parameters:

n: int-

Number of rows (and columns) in n x n output.

import numpy as np

identity_matrix = np.identity(n=3)
print(identity_matrix)
'''
Output:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
'''

dtype: np.float64, Optional-

The desired data-type for the NumPy array.

import numpy as np

identity_matrix = np.identity(n=3, dtype=np.int32)
print(identity_matrix)
'''
Output:
[[1 0 0]
 [0 1 0]
 [0 0 1]]
'''

Values: +

By passing the dtype, you are implicitly converting the values to the passed dtype.

like: None, Optional-

The like parameter in np.identity() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy