Bird
Raised Fist0
NumPydata~10 mins

np.linalg.eig() for eigenvalues in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - np.linalg.eig() for eigenvalues
Start with square matrix A
↓
Call np.linalg.eig(A)
↓
Compute eigenvalues and eigenvectors
↓
Return eigenvalues array and eigenvectors matrix
↓
Use eigenvalues and eigenvectors for analysis
The function takes a square matrix, calculates its eigenvalues and eigenvectors, and returns them for further use.
Execution Sample
NumPy
import numpy as np
A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
print(eigenvectors)
This code calculates eigenvalues and eigenvectors of matrix A and prints them.
Execution Table
StepActionMatrix AEigenvaluesEigenvectors
1Input matrix A[[4 2] [1 3]]--
2Call np.linalg.eig(A)[[4 2] [1 3]]ComputedComputed
3Calculate eigenvalues[[4 2] [1 3]][5.0, 2.0]-
4Calculate eigenvectors[[4 2] [1 3]][5.0, 2.0][[0.89442719 -0.70710678] [0.4472136 0.70710678]]
5Return results[[4 2] [1 3]][5.0, 2.0][[0.89442719 -0.70710678] [0.4472136 0.70710678]]
💡 Eigenvalues and eigenvectors computed and returned for matrix A
Variable Tracker
VariableStartAfter np.linalg.eig() callFinal
A[[4 2] [1 3]][[4 2] [1 3]][[4 2] [1 3]]
eigenvalues-[5.0, 2.0][5.0, 2.0]
eigenvectors-[[0.89442719 -0.70710678] [0.4472136 0.70710678]][[0.89442719 -0.70710678] [0.4472136 0.70710678]]
Key Moments - 3 Insights
Why are eigenvalues returned as an array of numbers?
Eigenvalues are the special numbers that tell how the matrix stretches space. The execution_table row 3 shows these values computed as [5.0, 2.0].
What does the eigenvectors matrix represent?
Each column in the eigenvectors matrix (see execution_table row 4) is a vector that points in a direction that the matrix scales by the corresponding eigenvalue.
Why must the input matrix be square?
Eigenvalues and eigenvectors are defined only for square matrices. The input matrix A in execution_table row 1 is 2x2, which is square.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 3, what are the eigenvalues of matrix A?
A[5.0, 2.0]
B[4, 3]
C[1, 2]
D[0.894, 0.447]
💡 Hint
Check the 'Eigenvalues' column in execution_table row 3.
At which step in the execution_table are eigenvectors first shown?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the first row where 'Eigenvectors' column has values.
If matrix A was not square, what would happen when calling np.linalg.eig()?
AIt would return eigenvalues and eigenvectors anyway
BIt would raise an error
CIt would return zeros
DIt would return the input matrix unchanged
💡 Hint
Recall key_moments about matrix shape requirements.
Concept Snapshot
np.linalg.eig(matrix)
- Input: square matrix
- Output: eigenvalues array, eigenvectors matrix
- Eigenvalues: special scalars
- Eigenvectors: directions scaled by eigenvalues
- Used in data science for PCA, stability analysis
Full Transcript
This visual trace shows how np.linalg.eig() works step-by-step. We start with a square matrix A. Calling np.linalg.eig(A) computes eigenvalues and eigenvectors. Eigenvalues are numbers that describe how the matrix stretches space, and eigenvectors are directions that stretch by those numbers. The execution table shows each step: input matrix, computation, and output. Variables track the matrix and results. Key moments clarify common confusions like why the matrix must be square and what eigenvectors mean. The quiz tests understanding by asking about eigenvalues, when eigenvectors appear, and error handling for non-square matrices.

Practice

(1/5)
1. What does the function np.linalg.eig() return when applied to a square matrix?
easy
A. The determinant and inverse of the matrix
B. An array of eigenvalues and a matrix of eigenvectors
C. The transpose and trace of the matrix
D. The sum and product of matrix elements

Solution

  1. Step 1: Understand the purpose of np.linalg.eig()

    This function is designed to find eigenvalues and eigenvectors of a square matrix.
  2. Step 2: Recall the output format

    It returns two objects: one array with eigenvalues and one matrix with eigenvectors as columns.
  3. Final Answer:

    An array of eigenvalues and a matrix of eigenvectors -> Option B
  4. Quick Check:

    Eigenvalues and eigenvectors [OK]
Hint: Remember: eig() returns eigenvalues and eigenvectors [OK]
Common Mistakes:
  • Confusing eigenvalues with determinant
  • Expecting only one output instead of two
  • Mixing eigenvectors with matrix transpose
2. Which of the following is the correct syntax to compute eigenvalues and eigenvectors of matrix A using NumPy?
easy
A. eigenvalues, eigenvectors = np.linalg.eig(A)
B. eigenvalues = np.linalg.eigvals(A)
C. eigenvectors, eigenvalues = np.linalg.eig(A)
D. eigenvalues, eigenvectors = np.eig.linalg(A)

Solution

  1. Step 1: Recall the correct function and output order

    The function np.linalg.eig() returns eigenvalues first, then eigenvectors.
  2. Step 2: Check syntax correctness

    eigenvalues, eigenvectors = np.linalg.eig(A) correctly assigns eigenvalues and eigenvectors in order from np.linalg.eig(A).
  3. Final Answer:

    eigenvalues, eigenvectors = np.linalg.eig(A) -> Option A
  4. Quick Check:

    Correct function and order [OK]
Hint: eig() returns (values, vectors) in that order [OK]
Common Mistakes:
  • Swapping eigenvalues and eigenvectors in assignment
  • Using wrong function like np.linalg.eigvals() for both outputs
  • Incorrect module or function name
3. Given the matrix A = np.array([[2, 0], [0, 3]]), what will be the output of np.linalg.eig(A)[0]?
medium
A. [3. 2.]
B. [0. 0.]
C. [2. 3.]
D. [5. 0.]

Solution

  1. Step 1: Identify eigenvalues of a diagonal matrix

    For a diagonal matrix, eigenvalues are the diagonal elements: 2 and 3.
  2. Step 2: Check the output of np.linalg.eig(A)[0]

    This returns the eigenvalues array, which will be [2. 3.].
  3. Final Answer:

    [2. 3.] -> Option C
  4. Quick Check:

    Diagonal elements = eigenvalues [OK]
Hint: Diagonal matrix eigenvalues = diagonal elements [OK]
Common Mistakes:
  • Confusing eigenvalues order
  • Expecting eigenvectors instead of eigenvalues
  • Misreading matrix elements
4. What is wrong with this code snippet?
import numpy as np
A = np.array([[1, 2], [3, 4]])
eigenvalues, eigenvectors = np.linalg.eigvals(A)
medium
A. np.linalg.eigvals() returns only eigenvalues, not eigenvectors
B. Matrix A is not square
C. np.linalg.eigvals() requires two arguments
D. The import statement is incorrect

Solution

  1. Step 1: Understand the function np.linalg.eigvals()

    This function returns only eigenvalues, not eigenvectors.
  2. Step 2: Check the assignment in the code

    The code tries to unpack two values, but eigvals() returns only one, causing an error.
  3. Final Answer:

    np.linalg.eigvals() returns only eigenvalues, not eigenvectors -> Option A
  4. Quick Check:

    eigvals() returns one output [OK]
Hint: eigvals() returns only eigenvalues, not vectors [OK]
Common Mistakes:
  • Expecting two outputs from eigvals()
  • Thinking matrix must be non-square
  • Misunderstanding import syntax
5. You have a matrix B = np.array([[1, 0], [0, -3]]). You want to find the eigenvalue with the largest magnitude. Which code snippet correctly finds it?
hard
A. vals, vecs = np.linalg.eig(B) largest = max(vecs)
B. vals = np.linalg.eigvals(B) largest = max(vals)
C. vals = np.linalg.eigvals(B) largest = vals[np.argmax(vals)]
D. vals, vecs = np.linalg.eig(B) largest = vals[np.argmax(np.abs(vals))]

Solution

  1. Step 1: Compute eigenvalues and eigenvectors

    Use np.linalg.eig(B) to get both eigenvalues and eigenvectors.
  2. Step 2: Find eigenvalue with largest magnitude

    Use np.abs(vals) to get absolute values, then np.argmax() to find index of largest magnitude eigenvalue.
  3. Step 3: Select eigenvalue at that index

    Indexing vals with that index gives the eigenvalue with largest magnitude.
  4. Final Answer:

    vals, vecs = np.linalg.eig(B) largest = vals[np.argmax(np.abs(vals))] -> Option D
  5. Quick Check:

    Use abs and argmax on eigenvalues [OK]
Hint: Use np.abs() and np.argmax() on eigenvalues to find largest [OK]
Common Mistakes:
  • Using max() directly without abs()
  • Trying to find max of eigenvectors
  • Using wrong function for eigenvalues