0
0
SciPydata~20 mins

Eigenvalue problems (eigs, eigsh) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Eigenvalue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of scipy.sparse.linalg.eigs on a symmetric matrix
What is the output of the following code snippet? It computes eigenvalues and eigenvectors of a symmetric matrix using eigs.
SciPy
import numpy as np
from scipy.sparse.linalg import eigs

A = np.array([[2, 1], [1, 2]])
vals, vecs = eigs(A, k=1, which='LM')
print(np.round(vals.real, 3))
A[0.0]
B[3.0]
C[2.0]
D[1.0]
Attempts:
2 left
💡 Hint
The matrix is symmetric with eigenvalues 3 and 1. 'which=LM' returns the largest magnitude eigenvalue.
data_output
intermediate
1:30remaining
Number of eigenvalues returned by eigsh
Given a 5x5 sparse symmetric matrix, how many eigenvalues and eigenvectors will eigsh return when called with k=3?
SciPy
import numpy as np
from scipy.sparse.linalg import eigsh

A = np.diag(np.arange(1,6))
vals, vecs = eigsh(A, k=3)
print(len(vals), vecs.shape)
A3 (5, 3)
B5 (5, 5)
C3 (3, 3)
D5 (3, 5)
Attempts:
2 left
💡 Hint
The parameter k controls how many eigenvalues and eigenvectors are returned.
🔧 Debug
advanced
1:30remaining
Error raised by eigs with k too large
What error does the following code raise? It tries to compute 5 eigenvalues of a 4x4 matrix using eigs.
SciPy
import numpy as np
from scipy.sparse.linalg import eigs

A = np.eye(4)
vals, vecs = eigs(A, k=5)
ATypeError: invalid argument type
BNo error, returns 5 eigenvalues
CRuntimeWarning: convergence not achieved
DValueError: k must be smaller than N
Attempts:
2 left
💡 Hint
k must be less than the size of the matrix.
🧠 Conceptual
advanced
2:00remaining
Difference between eigs and eigsh
Which statement correctly describes the difference between eigs and eigsh in scipy?
A<code>eigsh</code> is for symmetric or Hermitian matrices and is more efficient; <code>eigs</code> works for general matrices.
B<code>eigs</code> only works for symmetric matrices; <code>eigsh</code> works for any matrix.
C<code>eigs</code> uses dense matrix methods; <code>eigsh</code> uses sparse matrix methods.
D<code>eigsh</code> returns all eigenvalues; <code>eigs</code> returns only the largest eigenvalue.
Attempts:
2 left
💡 Hint
Think about matrix symmetry and algorithm optimization.
🚀 Application
expert
2:30remaining
Identifying dominant eigenvalue with eigs
You have a large sparse matrix representing a network. You want to find the dominant eigenvalue (largest magnitude) and its eigenvector using eigs. Which code snippet correctly achieves this?
Avals, vecs = eigs(A, k=1, which='LR')
Bvals, vecs = eigs(A, k=1, which='SM')
Cvals, vecs = eigs(A, k=1, which='LM')
Dvals, vecs = eigs(A, k=1, which='SR')
Attempts:
2 left
💡 Hint
'LM' means largest magnitude eigenvalue.