0
0
NumPydata~20 mins

NumPy with SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy with SciPy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of SciPy's integration with NumPy arrays
What is the output of the following code that uses SciPy to integrate a NumPy function over a range?
NumPy
import numpy as np
from scipy.integrate import quad

f = lambda x: np.sin(x)
result, error = quad(f, 0, np.pi)
print(round(result, 5))
A2.0
B3.14159
C0.0
D1.99999
Attempts:
2 left
💡 Hint
Recall the integral of sin(x) from 0 to pi is 2.
data_output
intermediate
2:00remaining
Shape of output from SciPy's hierarchical clustering
Given the following code using SciPy's hierarchical clustering on a NumPy array, what is the shape of the linkage matrix returned?
NumPy
import numpy as np
from scipy.cluster.hierarchy import linkage

X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
Z = linkage(X, method='single')
print(Z.shape)
A(4, 3)
B(3, 3)
C(4, 4)
D(3, 4)
Attempts:
2 left
💡 Hint
The linkage matrix has n-1 rows and 4 columns for n samples.
visualization
advanced
2:00remaining
Correct dendrogram plot from hierarchical clustering
Which option shows the correct code to plot a dendrogram from a linkage matrix Z using SciPy and Matplotlib?
NumPy
import numpy as np
from scipy.cluster.hierarchy import linkage, dendrogram
import matplotlib.pyplot as plt

X = np.random.rand(5, 2)
Z = linkage(X, 'ward')
A
plt.dendrogram(Z)
plt.show()
B
plt.plot(dendrogram(Z))
plt.show()
C
dendrogram(Z)
plt.show()
D
dendrogram(X)
plt.show()
Attempts:
2 left
💡 Hint
The dendrogram function is from scipy.cluster.hierarchy, not matplotlib.
🧠 Conceptual
advanced
2:00remaining
Understanding output of SciPy's fftpack with NumPy arrays
What does the output array represent when applying SciPy's fftpack.fft function to a NumPy array of real numbers?
AThe sorted input array in ascending order
BThe frequency components of the input signal as complex numbers
CThe cumulative sum of the input array elements
DThe inverse Fourier transform of the input array
Attempts:
2 left
💡 Hint
FFT transforms a signal from time domain to frequency domain.
🔧 Debug
expert
2:00remaining
Identify the error in SciPy optimization with NumPy function
What error will the following code raise when trying to minimize a function using SciPy's optimize.minimize with a NumPy array input?
NumPy
import numpy as np
from scipy.optimize import minimize

def f(x):
    return np.sum(x**2)

x0 = np.array([1, 2, 3])
res = minimize(f, x0, method='BFGS', jac=True)
print(res.fun)
ATypeError: The 'jac' parameter is True but the function does not return a gradient
BValueError: jacobian function must return array_like
CTypeError: 'bool' object is not callable
DTypeError: f() missing 1 required positional argument
Attempts:
2 left
💡 Hint
The jac parameter expects a function or False, not True, unless the function returns gradient.