🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.identity()

AI & DATA SCIENCE // np-identity

np.identity(n) creates a square n by n identity matrix — ones on the main diagonal, zeros everywhere else.

Syntax

np.identity(n, dtype=float)

Deep Dive Course

np.identity(n) is a simpler, more restricted version of np.eye(n): it always produces a square matrix with the ones strictly on the main diagonal, with no equivalent of np.eye()'s M, non-square shape, or k, diagonal offset, parameters. For the common case of just needing a plain identity matrix, np.identity() communicates that specific intent slightly more directly than calling np.eye() with its extra unused parameters.

1Understanding np.identity()

np.identity(n) is a simpler, more restricted version of np.eye(n): it always produces a square matrix with the ones strictly on the main diagonal, with no equivalent of np.eye()'s M, non-square shape, or k, diagonal offset, parameters. For the common case of just needing a plain identity matrix, np.identity() communicates that specific intent slightly more directly than calling np.eye() with its extra unused parameters.

💡

Use np.identity(n) instead of np.eye(n) specifically when you want a plain square identity matrix and nothing more — it's functionally identical to np.eye(n) with no extra arguments, but reads more directly as expressing exactly that intent.

editor.html
import numpy as np

I = np.identity(4)
print(I)
localhost:3000

2Practical Example

Here is a real-world application of np.identity() showing how it is used in production NumPy code.

editor.html
import numpy as np

A = np.array([[2, 0], [0, 3]])
I = np.identity(2)
print(A @ I)
localhost:3000

3Best Practices

Follow these guidelines when working with np.identity():

1. Use np.identity(n) for a plain identity matrix; switch to np.eye() only when you need its extra shape or diagonal-offset options

2. Cast to an integer dtype explicitly (dtype=int) if you need an identity matrix without floating-point representation

3. Use np.identity(n) as the starting point when iteratively building up a matrix via a sequence of linear transformations

⚠️

Tip: Use np.identity(n) instead of np.eye(n) specifically when you want a plain square identity matrix and nothing more — it's functionally identical to np.eye(n) with no extra arguments, but reads more directly as expressing exactly that intent.

editor.html
import numpy as np

I = np.identity(4)
print(I)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

I = np.identity(4)
print(I)
Example 02Advanced Example
import numpy as np

A = np.array([[2, 0], [0, 3]])
I = np.identity(2)
print(A @ I)

Best Practices

  • Use np.identity(n) for a plain identity matrix; switch to np.eye() only when you need its extra shape or diagonal-offset options
  • Cast to an integer dtype explicitly (dtype=int) if you need an identity matrix without floating-point representation
  • Use np.identity(n) as the starting point when iteratively building up a matrix via a sequence of linear transformations

Interview Question

What's the practical difference between np.identity(n) and calling np.eye(n) with no extra arguments?

Hint: Think about what each function is actually capable of producing beyond the simple case.

Called with just n, np.identity(n) and np.eye(n) produce the exact same result — a square identity matrix. The difference is in what each function is designed for: np.eye() additionally supports a non-square shape via its M parameter and a shifted diagonal via its k parameter, while np.identity() is deliberately restricted to only the plain square identity case. Choosing np.identity() when that's genuinely all you need communicates that intent more precisely to a reader than np.eye() with its unused extra capability.

Exercises

MediumPractice using np.identity() in a real scenario.
View Solution
import numpy as np

I = np.identity(4)
print(I)

Frequently Asked Questions

What's the practical difference between np.identity(n) and calling np.eye(n) with no extra arguments?

Called with just n, np.identity(n) and np.eye(n) produce the exact same result — a square identity matrix. The difference is in what each function is designed for: np.eye() additionally supports a non-square shape via its M parameter and a shifted diagonal via its k parameter, while np.identity() is deliberately restricted to only the plain square identity case. Choosing np.identity() when that's genuinely all you need communicates that intent more precisely to a reader than np.eye() with its unused extra capability.

Related Functions

np-eyenp-arraynp-linalg-inv