0
0
SciPydata~20 mins

Importing SciPy submodules - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SciPy Submodules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A5.0
B2.0
C0.9999999999999998
D-2.0
Attempts:
2 left
💡 Hint
Recall the formula for determinant of a 2x2 matrix [[a,b],[c,d]] is ad - bc.
data_output
intermediate
2: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)
A(4,)
B(2, 2)
C(3,)
D(1, 4)
Attempts:
2 left
💡 Hint
The FFT output length matches the input length.
🔧 Debug
advanced
2: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)
ATypeError: 'function' object is not callable
BAttributeError: module 'scipy' has no attribute 'optimize'
CNameError: name 'minimize' is not defined
DNo error, returns optimization result
Attempts:
2 left
💡 Hint
Check if submodules are automatically imported with 'import scipy'.
🧠 Conceptual
advanced
2: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?
A
from scipy.integrate import quad
result = quad(lambda x: x, 0, 1)
B
import scipy
result = scipy.integrate.quad(lambda x: x, 0, 1)
C
import integrate
result = integrate.quad(lambda x: x, 0, 1)
D
from scipy import quad
result = quad(lambda x: x, 0, 1)
Attempts:
2 left
💡 Hint
Consider which import statement correctly accesses the 'quad' function.
🚀 Application
expert
3: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))
A(3, 3) <class 'scipy.sparse.csc.csc_matrix'>
B(3, 3) <class 'numpy.ndarray'>
C(3, 3) <class 'scipy.sparse.csr.csr_matrix'>
D(3, 3) <class 'list'>
Attempts:
2 left
💡 Hint
csr_matrix creates a compressed sparse row matrix with the same shape as input.