Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.identity

Creates an n-by-n identity array: ones on the main diagonal and zeros elsewhere. The optional dtype selects the output type, while like can enable compatible non-NumPy array creation.

How it works

Call numpy.identity with the desired dimension and optionally a data type. It constructs a square array whose diagonal entries are one and whose remaining entries are zero; unless specified, the output uses floating-point values.

Syntax

numpy.identity(n, dtype=None, *, like=None)

Parameters

NameTypeDescription
nintNumber of rows and columns in the n × n output.
dtypedata-type, optionalData type of the output; defaults to float.
likearray_like, optionalReference object that can allow creation of a non-NumPy array.

Returns

out: ndarray — an n × n array with its main diagonal set to one and every other element set to zero.

Key points

  • The function constructs and returns an identity matrix.
  • Its output is square, and every main-diagonal position contains one.
  • The single dimension argument determines both dimensions of the result.
  • Use dtype to choose the type stored by the returned array.
  • All non-diagonal positions are initialized to zero.

Common mistakes

  • If dtype is omitted, the array elements are floating-point values rather than integers.
  • The like argument can delegate array creation to a compatible non-NumPy array implementation.

Example: Create an integer identity array

import numpy as np

identity = np.identity(3, dtype=np.int64)
print(identity)

Creates a 3-by-3 integer identity array with ones on the main diagonal and zeros elsewhere.

When to use it

Initialize an identity matrix for matrix operations.

Linear algebra

Build predictable square test fixtures with diagonal ones.

Testing

Request integer, Boolean, or another supported output data type.

Typed numerical data

Interview questions

QWhat does numpy.identity return?

It creates an n-by-n square array with ones on the main diagonal and zeros elsewhere. The default output data type is float unless dtype is provided.

Follow-up: How would you obtain integer entries? Pass an integer data type, such as dtype=np.int64.

QWhat do the n and dtype parameters control?

n specifies both the number of rows and the number of columns, so the result is always square. dtype controls the output element type and is optional.

Follow-up: What happens when n is 3 and dtype is omitted? A 3-by-3 array of floating-point zeros and ones is returned.

QWhat is the purpose of like?

like is an optional reference object that can enable creation of an array that is not a NumPy array. If that object supports __array_function__, it determines the result and can make it compatible with the reference.

Follow-up: Why use like instead of relying on NumPy's default? Use it when an alternative array implementation needs to participate in array creation.

  • numpy.eye
  • numpy.diag
  • numpy.ones

Source: numpy.identity. Checked 2026-10-15.