0
0
SciPydata~10 mins

Why fitting models to data reveals relationships in SciPy - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the curve fitting function from scipy.

SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Acurve_fit
Bfit_curve
Coptimize_curve
Dcurvefit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'curvefit' without underscore causes import error.
Trying 'fit_curve' which does not exist.
2fill in blank
medium

Complete the code to define a linear model function for fitting.

SciPy
def linear_model(x, [1], b):
    return [1] * x + b
Drag options to blanks, or click blank then click option'
Am
Ba
Cy
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'x' as a parameter name conflicts with input variable.
Using 'y' as a parameter name is confusing.
3fill in blank
hard

Fix the error in the code to fit the linear model to data arrays x_data and y_data.

SciPy
params, covariance = curve_fit([1], x_data, y_data)
Drag options to blanks, or click blank then click option'
Afit_linear
Bcurve_fit
Clinear_model
Dmodel_linear
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'curve_fit' itself causes recursion error.
Using undefined function names causes NameError.
4fill in blank
hard

Fill both blanks to create a dictionary of fitted parameters with keys 'slope' and 'intercept'.

SciPy
fitted_params = {'slope': params[1], 'intercept': params[2]
Drag options to blanks, or click blank then click option'
A[0]
B[1]
C(0)
D(1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets causes TypeError.
Mixing up indices swaps slope and intercept.
5fill in blank
hard

Fill all three blanks to create a plot of data points and the fitted line using matplotlib.

SciPy
import matplotlib.pyplot as plt
plt.scatter(x_data, y_data, label='Data')
plt.plot(x_data, linear_model(x_data, [1], [2]), color='red', label='Fit')
plt.xlabel('[3]')
plt.ylabel('y')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Aparams[0]
Bparams[1]
Cx
DX
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'params(0)' instead of 'params[0]' causes error.
Labeling x-axis with uppercase 'X' is less common.