0
0
SciPydata~10 mins

Fitting custom models 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 function used for curve fitting from scipy.

SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Aminimize
Bcurve_fit
Clinprog
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a different optimization function like minimize or root.
Forgetting to import from scipy.optimize.
2fill in blank
medium

Complete the code to define a custom linear model function with parameters a and b.

SciPy
def linear_model(x, a, b):
    return [1]
Drag options to blanks, or click blank then click option'
Aa + b * x
Ba / x + b
Ca * b + x
Da * x + b
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of multiplication and addition.
Using division instead of multiplication.
3fill in blank
hard

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

SciPy
params, covariance = curve_fit([1], x_data, y_data)
Drag options to blanks, or click blank then click option'
Alinear_model
Bnp.array
Cx_data
Dy_data
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data arrays instead of the model function.
Using numpy arrays as the model argument.
4fill in blank
hard

Fill both blanks to extract the fitted parameters and print the slope.

SciPy
a, b = [1]
print('Slope:', [2])
Drag options to blanks, or click blank then click option'
Aparams
Ba
Cb
Dcovariance
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to unpack covariance instead of params.
Printing the wrong variable for slope.
5fill in blank
hard

Fill all three blanks to create a dictionary of parameters with keys 'slope' and 'intercept', and print it.

SciPy
fit_params = [1]({'slope': [2], 'intercept': [3])
print(fit_params)
Drag options to blanks, or click blank then click option'
Adict
Ba
Cb
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using params instead of individual variables a and b.
Not using dict to create the dictionary.