0
0
SciPydata~10 mins

Cholesky decomposition in SciPy - Interactive Code Practice

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

Complete the code to import the Cholesky decomposition function from scipy.linalg.

SciPy
from scipy.linalg import [1]

# Now you can use the function for decomposition
Drag options to blanks, or click blank then click option'
Aeig
Binv
Ccholesky
Dsvd
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong function like 'inv' or 'eig'.
Using 'svd' which is for singular value decomposition, not Cholesky.
2fill in blank
medium

Complete the code to perform Cholesky decomposition on matrix 'A'.

SciPy
import numpy as np
from scipy.linalg import cholesky

A = np.array([[4, 2], [2, 3]])
L = cholesky([1], lower=True)
print(L)
Drag options to blanks, or click blank then click option'
AA
BL
Cnp
Dcholesky
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name instead of the matrix.
Passing the result variable 'L' before it is assigned.
3fill in blank
hard

Fix the error in the code to correctly compute the Cholesky decomposition.

SciPy
import numpy as np
from scipy.linalg import cholesky

B = np.array([[1, 2], [2, 1]])
L = cholesky([1], lower=True)
print(L)
Drag options to blanks, or click blank then click option'
Anp
BB
Ccholesky
DL
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name or other variables instead of the matrix.
Using a matrix that is not positive definite (this matrix is not positive definite, so decomposition will fail).
4fill in blank
hard

Fill both blanks to create a dictionary of Cholesky decompositions for matrices with keys 'X' and 'Y'.

SciPy
matrices = {'X': np.array([[9, 3], [3, 5]]), 'Y': np.array([[16, 4], [4, 10]])}
decompositions = {key: [1](mat, lower=True) for key, mat in matrices.items() if mat.shape[0] == [2]
print(decompositions)
Drag options to blanks, or click blank then click option'
Acholesky
B2
C1
Dnp.linalg.cholesky
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong function name like 'np.linalg.cholesky' when importing from scipy.linalg.
Checking for wrong matrix size.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores the upper triangular Cholesky decomposition for each matrix in 'data'.

SciPy
data = {'A': np.array([[25, 15], [15, 18]]), 'B': np.array([[36, 12], [12, 20]])}
results = { [1]: cholesky(mat, lower=[2]) for [3], mat in data.items() }
print(results)
Drag options to blanks, or click blank then click option'
A'A'
BFalse
Ckey
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable for keys.
Setting lower=True instead of False.
Not unpacking the dictionary items correctly.