0
0
SciPydata~10 mins

Curve fitting (curve_fit) in SciPy - Interactive Code Practice

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'
Aminimize
Bcurve_fit
Clinregress
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong function like minimize or root.
Forgetting to import from scipy.optimize.
2fill in blank
medium

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

SciPy
def model(x, [1], b):
    return [1] * x + b
Drag options to blanks, or click blank then click option'
Aa
Bx
Cy
Dm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'x' as a parameter name which conflicts with input.
Using 'y' which is usually output, not parameter.
3fill in blank
hard

Fix the error in the code to fit the model to data using curve_fit.

SciPy
params, covariance = curve_fit(model, xdata, [1])
Drag options to blanks, or click blank then click option'
Aydata
Bparams
Cmodel
Dxdata
Attempts:
3 left
💡 Hint
Common Mistakes
Passing xdata twice.
Passing the model function instead of data.
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] 0, 'intercept': params[2] 1}
Drag options to blanks, or click blank then click option'
A[
B]
C(
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using curly braces which are for dictionaries.
5fill in blank
hard

Fill all three blanks to plot the original data points and the fitted curve.

SciPy
import matplotlib.pyplot as plt

plt.scatter(xdata, ydata, label='Data')
plt.plot(xdata, model(xdata, [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
Dxdata
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up slope and intercept order.
Using wrong variable names for x-axis label.