We use np.linalg.eig() to find special numbers called eigenvalues and vectors from a square matrix. These help us understand important properties of the matrix.
np.linalg.eig() for eigenvalues in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
eigenvalues, eigenvectors = np.linalg.eig(matrix)
matrix must be a square 2D numpy array.
The function returns two arrays: eigenvalues (1D) and eigenvectors (2D).
Examples
NumPy
import numpy as np A = np.array([[2, 0], [0, 3]]) vals, vecs = np.linalg.eig(A)
NumPy
B = np.array([[4, 1], [2, 3]]) vals, vecs = np.linalg.eig(B)
NumPy
C = np.array([[1, 2], [2, 1]]) vals, vecs = np.linalg.eig(C)
Sample Program
This program finds eigenvalues and eigenvectors of a 2x2 matrix. It prints both so you can see the special numbers and directions.
NumPy
import numpy as np # Define a 2x2 matrix matrix = np.array([[5, 4], [1, 2]]) # Calculate eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(matrix) print("Eigenvalues:") print(eigenvalues) print("\nEigenvectors:") print(eigenvectors)
Important Notes
Eigenvalues can be complex numbers if the matrix is not symmetric.
Eigenvectors are normalized to length 1 by default.
Order of eigenvalues matches the columns of eigenvectors.
Summary
np.linalg.eig() finds eigenvalues and eigenvectors of square matrices.
Eigenvalues tell you about scaling factors; eigenvectors show directions.
This is useful in many fields like data science, physics, and engineering.
Practice
1. What does the function
np.linalg.eig() return when applied to a square matrix?easy
Solution
Step 1: Understand the purpose of
This function is designed to find eigenvalues and eigenvectors of a square matrix.np.linalg.eig()Step 2: Recall the output format
It returns two objects: one array with eigenvalues and one matrix with eigenvectors as columns.Final Answer:
An array of eigenvalues and a matrix of eigenvectors -> Option BQuick 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
Solution
Step 1: Recall the correct function and output order
The functionnp.linalg.eig()returns eigenvalues first, then eigenvectors.Step 2: Check syntax correctness
eigenvalues, eigenvectors = np.linalg.eig(A) correctly assigns eigenvalues and eigenvectors in order fromnp.linalg.eig(A).Final Answer:
eigenvalues, eigenvectors = np.linalg.eig(A) -> Option AQuick 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
Solution
Step 1: Identify eigenvalues of a diagonal matrix
For a diagonal matrix, eigenvalues are the diagonal elements: 2 and 3.Step 2: Check the output of
This returns the eigenvalues array, which will be [2. 3.].np.linalg.eig(A)[0]Final Answer:
[2. 3.] -> Option CQuick 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
Solution
Step 1: Understand the function
This function returns only eigenvalues, not eigenvectors.np.linalg.eigvals()Step 2: Check the assignment in the code
The code tries to unpack two values, buteigvals()returns only one, causing an error.Final Answer:
np.linalg.eigvals() returns only eigenvalues, not eigenvectors -> Option AQuick 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
Solution
Step 1: Compute eigenvalues and eigenvectors
Usenp.linalg.eig(B)to get both eigenvalues and eigenvectors.Step 2: Find eigenvalue with largest magnitude
Usenp.abs(vals)to get absolute values, thennp.argmax()to find index of largest magnitude eigenvalue.Step 3: Select eigenvalue at that index
Indexingvalswith that index gives the eigenvalue with largest magnitude.Final Answer:
vals, vecs = np.linalg.eig(B) largest = vals[np.argmax(np.abs(vals))] -> Option DQuick 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
