Get Started

Model Parameters

Now that you've built a general model, let's "optimize" or "fit" it to a new (preloaded) measured data set, xd, yd, by finding the specific values for model parameters a0, a1 for which the model data and the measured data line up on a plot.

This is an iterative visualization strategy, where we start with a guess for model parameters, pass them into the model(), over-plot the resulting modeled data on the measured data, and visually check that the line passes through the points. If it doesn't, we change the model parameters and try again.

This is a part of the course

“Introduction to Linear Modeling in Python”

View Course

Exercise instructions

  • Complete the function plot_data_and_model(xd, yd, ym), passing xd, yd and xd, ym into the internal plotting calls.
  • Compute model predictions using ym = model() by passing in both the data xd and the guessed model parameters a0 and a1.
    • Inspect the data provided above, and use this as a guide when you provide your first two estimates. You can come back and revise these estimates after reviewing how the line fits the data.
  • Use plot_data_and_model() to plot xd, yd, and ym together.
  • Change the values of a0 and a1 and repeat the previous 2 steps until the line passes through all the points.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Complete the plotting function definition
def plot_data_with_model(xd, yd, ym):
    fig = plot_data(____, ____)  # plot measured data
    fig.axes[0].plot(____, ____, color='red')  # over-plot modeled data
    plt.show()
    return fig

# Select new model parameters a0, a1, and generate modeled `ym` from them.
a0 = ____
a1 = ____
ym = model(xd, a0, a1)

# Plot the resulting model to see whether it fits the data
fig = plot_data_with_model(xd, yd, ____)

This exercise is part of the course

Introduction to Linear Modeling in Python

IntermediateSkill Level
4.0+
4 reviews

Explore the concepts and applications of linear models with python and build models to describe, predict, and extract insight from data patterns.

Here we look at the parts that go into building a linear model. Using the concept of a Taylor Series, we focus on the parameters slope and intercept, how they define the model, and how to interpret the them in several applied contexts. We apply a variety of python modules to find the model that best fits the data, by computing the optimal values of slope and intercept, using least-squares, numpy, statsmodels, and scikit-learn.

Exercise 1: What makes a model linearExercise 2: Terms in a ModelExercise 3: Model ComponentsExercise 4: Model Parameters
Exercise 5: Interpreting Slope and InterceptExercise 6: Linear ProportionalityExercise 7: Slope and Rates-of-ChangeExercise 8: Intercept and Starting PointsExercise 9: Model OptimizationExercise 10: Residual Sum of the SquaresExercise 11: Minimizing the ResidualsExercise 12: Visualizing the RSS MinimaExercise 13: Least-Squares OptimizationExercise 14: Least-Squares with `numpy`Exercise 15: Optimization with ScipyExercise 16: Least-Squares with `statsmodels`

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free