Challenge - 5 Problems
NumPy with SciPy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Recall the integral of sin(x) from 0 to pi is 2.
✗ Incorrect
The integral of sin(x) from 0 to pi is exactly 2. The quad function returns 2.0, which rounds to 2.0.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
The linkage matrix has n-1 rows and 4 columns for n samples.
✗ Incorrect
For n samples, the linkage matrix has n-1 rows and 4 columns. Here n=4, so shape is (3,4).
❓ visualization
advanced2: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')
Attempts:
2 left
💡 Hint
The dendrogram function is from scipy.cluster.hierarchy, not matplotlib.
✗ Incorrect
The dendrogram function is imported from scipy.cluster.hierarchy and called with the linkage matrix Z. Then plt.show() displays the plot.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
FFT transforms a signal from time domain to frequency domain.
✗ Incorrect
The fft function computes the discrete Fourier transform, returning complex numbers representing amplitude and phase of frequency components.
🔧 Debug
expert2: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)
Attempts:
2 left
💡 Hint
The jac parameter expects a function or False, not True, unless the function returns gradient.
✗ Incorrect
Setting jac=True tells minimize that the function returns both value and gradient, but f returns only value, causing a TypeError.