🚀 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.linalg.inv()

AI & DATA SCIENCE // np-linalg-inv

np.linalg.inv() computes the multiplicative inverse of a square matrix — the matrix that, multiplied by the original, produces the identity matrix.

Syntax

np.linalg.inv(A)

Deep Dive Course

For a square matrix A, its inverse satisfies A multiplied by its inverse equaling the identity matrix, the same way a number multiplied by its reciprocal equals 1. Not every square matrix has an inverse: a singular matrix, one whose determinant is 0, has no inverse, and np.linalg.inv() raises a LinAlgError if you try to invert one. Matrix inversion is also numerically unstable for matrices that are close to singular, a small determinant, or ill-conditioned, which can amplify floating-point rounding errors significantly in the result.

1Understanding np.linalg.inv()

For a square matrix A, its inverse satisfies A multiplied by its inverse equaling the identity matrix, the same way a number multiplied by its reciprocal equals 1. Not every square matrix has an inverse: a singular matrix, one whose determinant is 0, has no inverse, and np.linalg.inv() raises a LinAlgError if you try to invert one. Matrix inversion is also numerically unstable for matrices that are close to singular, a small determinant, or ill-conditioned, which can amplify floating-point rounding errors significantly in the result.

💡

Avoid computing the inverse of A and multiplying it by b to solve a linear system — use np.linalg.solve(A, b) instead, which is both faster and numerically more stable than explicitly forming the matrix inverse and multiplying by it.

editor.html
import numpy as np

A = np.array([[4, 7], [2, 6]])
A_inv = np.linalg.inv(A)
print(A_inv)
localhost:3000

2Practical Example

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

editor.html
import numpy as np

A = np.array([[4, 7], [2, 6]])
A_inv = np.linalg.inv(A)
identity = A @ A_inv
print(np.round(identity, 10))
localhost:3000

3Best Practices

Follow these guidelines when working with np.linalg.inv():

1. Use np.linalg.solve(A, b) instead of computing the inverse of A and multiplying by b when solving a linear system — it's faster and avoids extra numerical error

2. Catch LinAlgError when a matrix might be singular or ill-conditioned, rather than assuming inversion will always succeed

3. Check np.linalg.det(A) is meaningfully nonzero before relying on an inverse for numerically sensitive calculations

⚠️

Tip: Avoid computing the inverse of A and multiplying it by b to solve a linear system — use np.linalg.solve(A, b) instead, which is both faster and numerically more stable than explicitly forming the matrix inverse and multiplying by it.

editor.html
import numpy as np

A = np.array([[4, 7], [2, 6]])
A_inv = np.linalg.inv(A)
print(A_inv)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

A = np.array([[4, 7], [2, 6]])
A_inv = np.linalg.inv(A)
print(A_inv)
Example 02Advanced Example
import numpy as np

A = np.array([[4, 7], [2, 6]])
A_inv = np.linalg.inv(A)
identity = A @ A_inv
print(np.round(identity, 10))

Best Practices

  • Use np.linalg.solve(A, b) instead of computing the inverse of A and multiplying by b when solving a linear system — it's faster and avoids extra numerical error
  • Catch LinAlgError when a matrix might be singular or ill-conditioned, rather than assuming inversion will always succeed
  • Check np.linalg.det(A) is meaningfully nonzero before relying on an inverse for numerically sensitive calculations

Interview Question

Why does np.linalg.inv() raise an error for some matrices, and what property determines whether a matrix is invertible?

Hint: Think about the determinant.

A square matrix is invertible only if its determinant is nonzero; a matrix with a determinant of exactly 0 is called singular, and it has no valid inverse, the same way the number 0 has no reciprocal in ordinary arithmetic. np.linalg.inv() raises a LinAlgError specifically when it detects that the matrix is singular, or numerically indistinguishable from singular, since there's no mathematically valid inverse matrix to return in that case.

Exercises

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

A = np.array([[4, 7], [2, 6]])
A_inv = np.linalg.inv(A)
print(A_inv)

Frequently Asked Questions

Why does np.linalg.inv() raise an error for some matrices, and what property determines whether a matrix is invertible?

A square matrix is invertible only if its determinant is nonzero; a matrix with a determinant of exactly 0 is called singular, and it has no valid inverse, the same way the number 0 has no reciprocal in ordinary arithmetic. np.linalg.inv() raises a LinAlgError specifically when it detects that the matrix is singular, or numerically indistinguishable from singular, since there's no mathematically valid inverse matrix to return in that case.

Related Functions

np-linalg-detnp-linalg-solvenp-eye