Get Started

Residual Sum of the Squares

In a previous exercise, we saw that the altitude along a hiking trail was roughly fit by a linear model, and we introduced the concept of differences between the model and the data as a measure of model goodness.

In this exercise, you'll work with the same measured data, and quantifying how well a model fits it by computing the sum of the square of the "differences", also called "residuals".

This is a part of the course

“Introduction to Linear Modeling in Python”

View Course

Exercise instructions

  • Load the x_data, y_data with the pre-defined load_data() function.
  • Call the pre-defined model(), passing in x_dataand specific values a0, a1.
  • Compute the residuals as y_data - y_model and then find rss by using np.square() and np.sum().
  • Print the resulting value of rss.

Hands-on interactive exercise

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

# Load the data
x_data, y_data = load_data()

# Model the data with specified values for parameters a0, a1
y_model = model(____, a0=150, a1=25)

# Compute the RSS value for this parameterization of the model
rss = np.sum(np.square(____ - ____))
print("RSS = {}".format(____))

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 ParametersExercise 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 Squares
Exercise 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