Challenge - 5 Problems
Advanced Scipy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of scipy.optimize root finding
What is the output of this code that finds the root of the function f(x) = x^3 - 1 using scipy.optimize.root?
SciPy
import numpy as np from scipy.optimize import root def f(x): return x**3 - 1 sol = root(f, 0.5) print(round(sol.x[0], 3))
Attempts:
2 left
💡 Hint
The root of x^3 - 1 = 0 is the cube root of 1.
✗ Incorrect
The function f(x) = x^3 - 1 has a root at x = 1 because 1^3 - 1 = 0. The scipy.optimize.root method finds this root starting from initial guess 0.5.
❓ data_output
intermediate2:00remaining
Shape of result from scipy.cluster.hierarchy linkage
What is the shape of the array returned by scipy.cluster.hierarchy.linkage when clustering 5 data points?
SciPy
from scipy.cluster.hierarchy import linkage import numpy as np X = np.random.rand(5, 2) Z = linkage(X, method='single') print(Z.shape)
Attempts:
2 left
💡 Hint
The linkage matrix has one less row than the number of original points, and 4 columns.
✗ Incorrect
For n data points, linkage returns an (n-1) by 4 matrix describing the hierarchical clustering steps.
❓ visualization
advanced3:00remaining
Visualizing optimization convergence with scipy.optimize.minimize
Which option shows the correct plot of the function value decreasing over iterations when minimizing f(x) = (x-3)^2 using scipy.optimize.minimize with callback?
SciPy
import matplotlib.pyplot as plt from scipy.optimize import minimize history = [] def callback(x): history.append(x[0]) def f(x): return (x[0] - 3)**2 res = minimize(f, [0], callback=callback, options={'disp': False}) plt.plot(history) plt.xlabel('Iteration') plt.ylabel('x value') plt.title('Optimization progress') plt.show()
Attempts:
2 left
💡 Hint
The optimizer moves x from the start value towards the minimum at 3.
✗ Incorrect
The callback records x values each iteration, showing a smooth approach from 0 to 3 in the plot.
🧠 Conceptual
advanced1:30remaining
Why advanced numerical methods handle complex problems better
Why do advanced numerical methods like those in scipy.optimize solve complex problems better than simple methods?
Attempts:
2 left
💡 Hint
Think about how these methods adjust their steps based on feedback.
✗ Incorrect
Advanced methods use iterative steps that adjust based on the function's behavior, handling complexity and constraints better than fixed simple methods.
🔧 Debug
expert2:30remaining
Identify the error in this scipy integration code
What error does this code produce when trying to integrate f(x) = 1/x from 0 to 1 using scipy.integrate.quad?
SciPy
from scipy.integrate import quad def f(x): return 1/x result, error = quad(f, 0, 1) print(result)
Attempts:
2 left
💡 Hint
Consider the behavior of 1/x near zero and how quad handles singularities.
✗ Incorrect
The function 1/x has a singularity at 0, so quad raises an IntegrationWarning about the singularity but still returns a result.