0
0
SciPydata~20 mins

Fitting custom models in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Model Fitting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of custom model fitting with scipy.optimize.curve_fit

What is the output of the following code snippet that fits a custom quadratic model to data?

SciPy
import numpy as np
from scipy.optimize import curve_fit

def model(x, a, b, c):
    return a * x**2 + b * x + c

xdata = np.array([0, 1, 2, 3, 4])
ydata = np.array([1, 3, 7, 13, 21])

params, _ = curve_fit(model, xdata, ydata)
print(np.round(params, 2))
A[1.0, 2.0, 1.0]
B[0.0, 1.0, 1.0]
C[1.0, 1.0, 1.0]
D[1.0, 1.0, 2.0]
Attempts:
2 left
💡 Hint

Recall the quadratic formula: y = a*x² + b*x + c. Check the data points to guess coefficients.

data_output
intermediate
1:30remaining
Number of parameters fitted in a custom exponential model

Given the custom exponential model y = a * exp(b * x) + c, how many parameters does scipy.optimize.curve_fit estimate?

SciPy
import numpy as np
from scipy.optimize import curve_fit

def exp_model(x, a, b, c):
    return a * np.exp(b * x) + c

xdata = np.linspace(0, 4, 5)
ydata = exp_model(xdata, 2, 0.5, 1) + 0.1 * np.random.normal(size=xdata.size)
params, _ = curve_fit(exp_model, xdata, ydata)
print(len(params))
A4
B2
C3
D1
Attempts:
2 left
💡 Hint

Count the number of parameters in the function definition after x.

🔧 Debug
advanced
2:00remaining
Identify the error in custom model fitting code

What error does the following code raise when trying to fit a linear model?

SciPy
import numpy as np
from scipy.optimize import curve_fit

def linear_model(x, m, c):
    return m * x + c

xdata = np.array([0, 1, 2, 3])
ydata = np.array([1, 3, 5, 7])

params, _ = curve_fit(linear_model, xdata, ydata, p0=[1])
AValueError: x and y must have same length
BValueError: 'p0' must be a list of length 2
CRuntimeError: Optimal parameters not found
DTypeError: linear_model() missing 1 required positional argument
Attempts:
2 left
💡 Hint

Check the initial guess p0 length compared to model parameters.

visualization
advanced
2:30remaining
Plot result of fitting a sinusoidal custom model

Which plot correctly shows the fitted sinusoidal model y = a * sin(b * x + c) to noisy data?

SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def sin_model(x, a, b, c):
    return a * np.sin(b * x + c)

xdata = np.linspace(0, 2 * np.pi, 50)
ydata = 3 * np.sin(2 * xdata + 0.5) + 0.3 * np.random.normal(size=xdata.size)
params, _ = curve_fit(sin_model, xdata, ydata, p0=[2, 2, 0])

plt.scatter(xdata, ydata, label='Data')
plt.plot(xdata, sin_model(xdata, *params), color='red', label='Fitted model')
plt.legend()
plt.show()
AScatter points with a red exponential curve
BScatter points with a flat red line
CScatter points with a red linear line
DScatter points with a red smooth sine curve closely following data
Attempts:
2 left
💡 Hint

Think about the shape of a sine wave and how fitting works.

🧠 Conceptual
expert
2:00remaining
Effect of initial parameter guess on fitting convergence

Which statement best describes the effect of the initial parameter guess p0 in scipy.optimize.curve_fit when fitting complex custom models?

AA poor initial guess can cause the algorithm to converge to a local minimum, not the global best fit
BInitial guesses have no effect; curve_fit always finds the global minimum
CInitial guesses only affect the speed but never the final result
Dcurve_fit ignores initial guesses if the model is nonlinear
Attempts:
2 left
💡 Hint

Consider how optimization algorithms work with complex landscapes.