0
0
NumPydata~10 mins

np.linalg.eig() for eigenvalues in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compute eigenvalues of matrix A.

NumPy
import numpy as np
A = np.array([[2, 0], [0, 3]])
eigenvalues, _ = np.linalg.[1](A)
print(eigenvalues)
Drag options to blanks, or click blank then click option'
Aeig
Bdet
Csolve
Dinv
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.linalg.inv() which computes inverse, not eigenvalues.
Using np.linalg.det() which computes determinant, not eigenvalues.
2fill in blank
medium

Complete the code to extract only eigenvalues from the result.

NumPy
import numpy as np
B = np.array([[4, 1], [2, 3]])
eigenvalues, [1] = np.linalg.eig(B)
print(eigenvalues)
Drag options to blanks, or click blank then click option'
Avectors
Bvals
Ceigenvectors
Dvecs
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the second variable as eigenvalues again.
Using unrelated variable names like 'vals' which is unclear.
3fill in blank
hard

Fix the error in the code to correctly compute eigenvalues.

NumPy
import numpy as np
C = np.array([[1, 2], [3, 4]])
eigenvalues, _ = np.linalg.[1](C)
print(eigenvalues)
Drag options to blanks, or click blank then click option'
Aeigvals
Beig
Ceigen
Deigenvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using eigvals which returns only eigenvalues and causes an unpacking error.
Using non-existent functions like eigen or eigenvalues.
4fill in blank
hard

Fill both blanks to create a dictionary of eigenvalue to eigenvector mapping.

NumPy
import numpy as np
D = np.array([[5, 4], [1, 2]])
eigenvalues, eigenvectors = np.linalg.eig(D)
result = {eigenvalues[[1]]: eigenvectors[:, [2]] for i in range(len(eigenvalues))}
print(result)
Drag options to blanks, or click blank then click option'
Ai
B0
C1
Dlen(eigenvalues)
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed indices like 0 or 1 instead of the loop variable.
Using length of eigenvalues as index which causes error.
5fill in blank
hard

Fill all three blanks to filter eigenvalues greater than 3 and create a dictionary with their eigenvectors.

NumPy
import numpy as np
E = np.array([[6, 2], [2, 3]])
eigenvalues, eigenvectors = np.linalg.eig(E)
filtered = {eigenvalues[[1]]: eigenvectors[:, [2]] for [3] in range(len(eigenvalues)) if eigenvalues[[1]] > 3}
print(filtered)
Drag options to blanks, or click blank then click option'
Ai
Bj
Ck
Dm
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for indexing causing errors.
Using fixed indices instead of loop variable.