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

AI & DATA SCIENCE // np-linalg-eig

np.linalg.eig() computes the eigenvalues and corresponding right eigenvectors of a square matrix.

Syntax

np.linalg.eig(A)

Deep Dive Course

An eigenvector v of matrix A is a special vector whose direction is unchanged by the transformation A represents — A applied to v is exactly a scalar multiple of v, and that scalar is the corresponding eigenvalue. np.linalg.eig(A) returns a tuple of eigenvalues and eigenvectors, where eigenvalues is a 1D array and eigenvectors is a 2D array whose columns, not rows, are the corresponding eigenvectors — a common source of indexing mistakes. Eigenvalues can be complex even for a real-valued input matrix, so the returned arrays may have a complex dtype.

1Understanding np.linalg.eig()

An eigenvector v of matrix A is a special vector whose direction is unchanged by the transformation A represents — A applied to v is exactly a scalar multiple of v, and that scalar is the corresponding eigenvalue. np.linalg.eig(A) returns a tuple of eigenvalues and eigenvectors, where eigenvalues is a 1D array and eigenvectors is a 2D array whose columns, not rows, are the corresponding eigenvectors — a common source of indexing mistakes. Eigenvalues can be complex even for a real-valued input matrix, so the returned arrays may have a complex dtype.

💡

Remember eigenvectors are returned as the columns of the eigenvectors matrix, not the rows — indexing a specific column gives the eigenvector for the eigenvalue at that same position, not indexing a row.

editor.html
import numpy as np

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
localhost:3000

2Practical Example

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

editor.html
import numpy as np

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
v = eigenvectors[:, 0]
print(np.allclose(A @ v, eigenvalues[0] * v))
localhost:3000

3Best Practices

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

1. Extract eigenvectors by column, not by row, since that's how np.linalg.eig() actually returns them

2. Check whether the result's dtype is complex, since even a real input matrix can have complex eigenvalues

3. Use np.linalg.eigvals() instead of eig() when you only need the eigenvalues and not the eigenvectors, since it's slightly cheaper to compute

⚠️

Tip: Remember eigenvectors are returned as the columns of the eigenvectors matrix, not the rows — indexing a specific column gives the eigenvector for the eigenvalue at that same position, not indexing a row.

editor.html
import numpy as np

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
Example 02Advanced Example
import numpy as np

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
v = eigenvectors[:, 0]
print(np.allclose(A @ v, eigenvalues[0] * v))

Best Practices

  • Extract eigenvectors by column, not by row, since that's how np.linalg.eig() actually returns them
  • Check whether the result's dtype is complex, since even a real input matrix can have complex eigenvalues
  • Use np.linalg.eigvals() instead of eig() when you only need the eigenvalues and not the eigenvectors, since it's slightly cheaper to compute

Interview Question

For a matrix A with eigenvalue lambda and eigenvector v, what does the equation A times v equals lambda times v actually mean geometrically?

Hint: Think about direction versus magnitude.

It means that applying the linear transformation A to the vector v produces a result that points in exactly the same, or exactly opposite, direction as v — it might get longer, shorter, or flipped, but its direction along that particular line is preserved. Lambda, the eigenvalue, is exactly the scaling factor describing how much v is stretched, shrunk, or reversed by the transformation. Most vectors change direction when transformed by A; eigenvectors are the special exceptions whose direction A leaves invariant.

Exercises

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

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)

Frequently Asked Questions

For a matrix A with eigenvalue lambda and eigenvector v, what does the equation A times v equals lambda times v actually mean geometrically?

It means that applying the linear transformation A to the vector v produces a result that points in exactly the same, or exactly opposite, direction as v — it might get longer, shorter, or flipped, but its direction along that particular line is preserved. Lambda, the eigenvalue, is exactly the scaling factor describing how much v is stretched, shrunk, or reversed by the transformation. Most vectors change direction when transformed by A; eigenvectors are the special exceptions whose direction A leaves invariant.

Related Functions

np-linalg-detnp-linalg-svdnp-trace