Challenge - 5 Problems
SciPy Submodules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of importing and using scipy.linalg
What is the output of this code snippet?
SciPy
from scipy import linalg import numpy as np matrix = np.array([[1, 2], [3, 4]]) det = linalg.det(matrix) print(round(det, 2))
Attempts:
2 left
💡 Hint
Recall the formula for determinant of a 2x2 matrix [[a,b],[c,d]] is ad - bc.
✗ Incorrect
The determinant of the matrix [[1,2],[3,4]] is (1*4) - (2*3) = 4 - 6 = -2.0. The scipy.linalg.det function computes this value.
❓ data_output
intermediate2:00remaining
Shape of array returned by scipy.fftpack.fft
What is the shape of the array returned by this code?
SciPy
from scipy.fftpack import fft import numpy as np arr = np.array([1, 2, 3, 4]) result = fft(arr) print(result.shape)
Attempts:
2 left
💡 Hint
The FFT output length matches the input length.
✗ Incorrect
The fft function returns a complex array of the same length as the input array. Here, input length is 4, so output shape is (4,).
🔧 Debug
advanced2:00remaining
Identify the error when importing scipy.optimize
What error will this code raise?
SciPy
import scipy result = scipy.optimize.minimize(lambda x: x**2, 0)
Attempts:
2 left
💡 Hint
Check if submodules are automatically imported with 'import scipy'.
✗ Incorrect
Importing only 'scipy' does not automatically import submodules like 'optimize'. Accessing scipy.optimize without importing it causes AttributeError.
🧠 Conceptual
advanced2:00remaining
Best practice for importing SciPy submodules
Which option is the best practice to import and use the 'integrate' submodule of SciPy for numerical integration?
Attempts:
2 left
💡 Hint
Consider which import statement correctly accesses the 'quad' function.
✗ Incorrect
Option A correctly imports 'quad' from 'scipy.integrate' submodule. Option A fails because 'scipy.integrate' is not imported by default. Option A fails because 'integrate' is not a standalone module. Option A fails because 'quad' is not directly in 'scipy'.
🚀 Application
expert3:00remaining
Predict output shape after importing and using scipy.sparse
What is the shape and type of the output after running this code?
SciPy
from scipy.sparse import csr_matrix import numpy as np arr = np.array([[0, 0, 1], [1, 0, 0], [0, 2, 0]]) sparse_mat = csr_matrix(arr) print(sparse_mat.shape, type(sparse_mat))
Attempts:
2 left
💡 Hint
csr_matrix creates a compressed sparse row matrix with the same shape as input.
✗ Incorrect
The csr_matrix constructor converts the numpy array into a sparse matrix of type 'csr_matrix' with the same shape (3,3).