0
0
SciPydata~20 mins

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

Choose your learning style9 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.