Bird
Raised Fist0
SciPydata~20 mins

Eigenvalues and eigenvectors (eig) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Eigen Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of eigenvalues and eigenvectors calculation
What is the output of the following code that calculates eigenvalues and eigenvectors of a matrix using scipy.linalg.eig?
SciPy
import numpy as np
from scipy.linalg import eig

A = np.array([[2, 1], [1, 2]])
eigenvalues, eigenvectors = eig(A)
print(np.round(eigenvalues, 2))
print(np.round(eigenvectors, 2))
A
[1.+0.j 3.+0.j]
[[ 0.71 -0.71]
 [ 0.71  0.71]]
B
[3.+0.j 1.+0.j]
[[ 0.71  0.71]
 [-0.71  0.71]]
C
]]17.0  17.0-[ 
]17.0  17.0 [[
]j.0+.1 j.0+.3[
D
[1.+0.j 3.+0.j]
[[ 0.71  0.71]
 [-0.71  0.71]]
Attempts:
2 left
💡 Hint
Remember eigenvalues are sorted in the order returned by eig, and eigenvectors correspond column-wise.
❓ data_output
intermediate
2:00remaining
Number of eigenvalues for a non-square matrix
What happens when you try to compute eigenvalues of a non-square matrix using scipy.linalg.eig? How many eigenvalues are returned?
SciPy
import numpy as np
from scipy.linalg import eig

B = np.array([[1, 2, 3], [4, 5, 6]])
eigenvalues, eigenvectors = eig(B)
ARaises a ValueError because the matrix is not square
BReturns an empty array with zero eigenvalues
CReturns 3 eigenvalues corresponding to the number of columns
DReturns 2 eigenvalues corresponding to the number of rows
Attempts:
2 left
💡 Hint
Eigenvalues are defined only for square matrices.
🧠 Conceptual
advanced
2:00remaining
Interpretation of complex eigenvalues
A real 2x2 matrix has eigenvalues 1+2j and 1-2j. What does this imply about the matrix's behavior?
AThe matrix is diagonal with real entries
BThe matrix is symmetric and positive definite
CThe matrix has only real eigenvectors
DThe matrix represents a rotation combined with scaling
Attempts:
2 left
💡 Hint
Complex conjugate eigenvalues often indicate rotation in 2D space.
🔧 Debug
advanced
2:00remaining
Identify the error in eigenvalue calculation code
What error does the following code produce and why? import numpy as np from scipy.linalg import eig C = np.array([[1, 2], [3, 4], [5, 6]]) eigenvalues, eigenvectors = eig(C)
AIndexError: index out of bounds
BTypeError: too many values to unpack
CValueError: expected square matrix
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the shape of the matrix passed to eig.
🚀 Application
expert
3:00remaining
Using eigenvectors to diagonalize a matrix
Given a symmetric matrix M, which code snippet correctly diagonalizes M using its eigenvectors and eigenvalues?
SciPy
import numpy as np
from scipy.linalg import eig

M = np.array([[4, 1], [1, 3]])
A
vals, vecs = eig(M)
D = np.diag(vals)
M_reconstructed = vecs @ D @ np.linalg.inv(vecs)
B
vals, vecs = eig(M)
D = np.diag(vals)
M_reconstructed = vecs @ D @ vecs.T
C
vals, vecs = eig(M)
D = np.diag(vals)
M_reconstructed = vecs.T @ D @ vecs
D
vals, vecs = eig(M)
D = np.diag(vals)
M_reconstructed = np.linalg.inv(vecs) @ D @ vecs
Attempts:
2 left
💡 Hint
Diagonalization formula: M = V D V^{-1} where V columns are eigenvectors.

Practice

(1/5)
1. What do eigenvalues represent in the context of a matrix transformation?
easy
A. The sum of all matrix elements
B. The directions that change completely under the matrix
C. How much eigenvectors stretch or shrink when the matrix is applied
D. The inverse of the matrix

Solution

  1. Step 1: Understand eigenvalues meaning

    Eigenvalues measure the factor by which eigenvectors are stretched or shrunk when multiplied by the matrix.
  2. Step 2: Compare options with definition

    How much eigenvectors stretch or shrink when the matrix is applied correctly describes this stretching/shrinking effect. Other options describe unrelated concepts.
  3. Final Answer:

    How much eigenvectors stretch or shrink when the matrix is applied -> Option C
  4. Quick Check:

    Eigenvalues = stretch/shrink factor [OK]
Hint: Eigenvalues tell how vectors scale under the matrix [OK]
Common Mistakes:
  • Confusing eigenvalues with eigenvectors
  • Thinking eigenvalues are directions
  • Mixing eigenvalues with matrix sum
2. Which of the following is the correct way to import the function to compute eigenvalues and eigenvectors using scipy?
easy
A. from scipy.linalg import eig
B. import scipy.eigen as eig
C. from scipy.linalg import eigenvalues
D. import scipy.linalg.eig as eigen

Solution

  1. Step 1: Recall scipy function for eigenvalues

    The function to compute eigenvalues and eigenvectors is named 'eig' and is in scipy.linalg.
  2. Step 2: Check import syntax

    from scipy.linalg import eig correctly imports 'eig' from 'scipy.linalg'. Other options use incorrect module names or syntax.
  3. Final Answer:

    from scipy.linalg import eig -> Option A
  4. Quick Check:

    Correct import = from scipy.linalg import eig [OK]
Hint: Use 'from scipy.linalg import eig' to get eigenvalues [OK]
Common Mistakes:
  • Using wrong module names like scipy.eigen
  • Trying to import non-existent functions
  • Incorrect import syntax
3. What is the output of the following code?
import numpy as np
from scipy.linalg import eig
A = np.array([[2, 0], [0, 3]])
values, vectors = eig(A)
print(values)
medium
A. [2.+0.j 3.+0.j]
B. [0.+2.j 0.+3.j]
C. [5.+0.j 0.+0.j]
D. [2 3]

Solution

  1. Step 1: Understand matrix and eig function

    Matrix A is diagonal with values 2 and 3 on the diagonal. Eigenvalues of a diagonal matrix are the diagonal elements.
  2. Step 2: Predict output of eig

    scipy.linalg.eig returns eigenvalues as complex numbers, so output will be [2.+0.j 3.+0.j].
  3. Final Answer:

    [2.+0.j 3.+0.j] -> Option A
  4. Quick Check:

    Diagonal matrix eigenvalues = diagonal elements [OK]
Hint: Diagonal matrix eigenvalues = diagonal entries as complex numbers [OK]
Common Mistakes:
  • Expecting real numbers instead of complex
  • Adding eigenvalues instead of listing
  • Confusing eigenvalues with eigenvectors
4. The following code throws an error. What is the main issue?
import numpy as np
from scipy.linalg import eig
A = np.array([[1, 2, 3], [4, 5, 6]])
values, vectors = eig(A)
medium
A. eig function is misspelled
B. Matrix A is not square
C. Missing import for numpy
D. Vectors variable name is invalid

Solution

  1. Step 1: Check matrix shape

    Matrix A has shape (2, 3), which is not square (rows != columns).
  2. Step 2: Understand eig requirements

    scipy.linalg.eig requires a square matrix to compute eigenvalues and eigenvectors.
  3. Final Answer:

    Matrix A is not square -> Option B
  4. Quick Check:

    eig needs square matrix [OK]
Hint: Check matrix shape; eig needs square matrix [OK]
Common Mistakes:
  • Ignoring matrix shape requirement
  • Assuming eig works on any matrix
  • Blaming variable names or imports
5. Given the matrix B = [[4, 1], [2, 3]], which of the following is a correct eigenvalue computed using scipy.linalg.eig?
hard
A. 7.0 + 0.j
B. 3.0 + 0.j
C. 1.0 + 0.j
D. 5.0 + 0.j

Solution

  1. Step 1: Calculate eigenvalues of B

    Eigenvalues satisfy det(B - λI) = 0. For B=[[4,1],[2,3]], characteristic polynomial is (4-λ)(3-λ)-2=0 -> λ² -7λ +10=0.
  2. Step 2: Solve quadratic equation

    Roots are λ = (7 ± sqrt(49 - 40))/2 = (7 ± 3)/2 -> λ=5 or λ=2.
  3. Step 3: Match options

    5.0 + 0.j is 5.0 + 0.j, which matches one eigenvalue. Others do not match.
  4. Final Answer:

    5.0 + 0.j -> Option D
  5. Quick Check:

    Eigenvalues = 5 and 2 [OK]
Hint: Solve characteristic polynomial for eigenvalues [OK]
Common Mistakes:
  • Miscomputing characteristic polynomial
  • Ignoring complex number format
  • Choosing wrong root from quadratic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard