scikit-learn: Predicting Sales Revenue with Simple Linear Regression

This project will cover how to implement simple linear regression using Python. We will alternatively use the statsmodels and sklearn modules for calculating the linear regression, while using pandas for data management, and seaborn for plotting. We will be working with the very popular advertising dataset to predict sales revenue based on advertising spending on different mediums.

The following topics will be covered in this project: Exploratory Analysis and Data Visualization, Data Preparation, Building a Simple Linear Regression Model, Understanding Ordinary Least Squares (OLS) to Estimate Model Parameters, Interpreting Model Coefficients, Making Predictions Using the Model, Model Evaluation Using RMSE and R-Square Statistics.

Join for $4.99
scikit-learn: Predicting Sales Revenue with Simple Linear Regression

Duration (mins)


5.0 / 5


Task List

We will cover the following tasks in 1 hour and 4 minutes:

Introduction and Overview

We will understand the Rhyme interface and our learning environment. You will be provided with a cloud desktop with Jupyter Notebooks and all the software you will need to complete the project. Jupyter Notebooks are very popular with Data Science and Machine Learning Engineers as one can write code in cells and use other cells for documentation.

We will also introduce the model we will be building as well the dataset for this Project.

Loading the Data

In this task, we will load the very popular advertising dataset about various costs incurred on advertising by different mediums such as through TV, radio, newspaper, and the sales for a particular product. Next, we will briefly explore the data to get some basic information about what we are going to be working with.

Data Preparation

Cleaning and preprocessing data is a vital process in data analysis and machine learning. In this task, we will take a look at our imported data and ascertain what to remove and what to keep for further analysis.

Exploratory Analysis

It’s good practice to first visualize the data before doing any analysis and model building. If the data is high dimensional, at least examine few slices using simple techniques like box plots. In this task, we will look at the distributions of our response variable , Sales, and also looked at the distributions of our predictors, TV, Radio, and Newspaper.

Exploring Relationships between Predictors and Response

Here we visualize pairwise correlations between our predictors and response. We also create a heatmap of the corresponding correlation matrix. This is done so as to ascertain whether there exists any relationship between the sales revenue and the expenditure on the various advertisement channels.

Creating the Simple Linear Regression Model

Linear regression is an approach for modeling the relationship between a scalar dependent response variable y and one or more predictors (or independent variables) denoted X. The case where we have just one predictor is known as simple linear regression.

In this task, we are going to use the statsmodels.api to create our ordinary least squares (OLS) linear regression model. We will use sklearn to split our data into training and test sets.

Evaluation and Model Parameters

In this task we will use the params and summary() methods to produce human-readable output of our model coefficients and the OLS regression results.

Making Predictions with our Model

Now that we have trained our OLS regression model on the training data, we will use it to make predictions on the test data. Note that the predictions are made on data the model has never seen during train time. Next, will will use the rmse helper function from to calculate the root mean squared error (RMSE), which is the difference between the actual value and predicted value of the response variable.

Plotting the Residuals

One of a few core assumptions of OLS is the normality of errors. If error terms are not normal, then the standard errors of OLS estimates won’t be reliable, meaning that linearity assumption made during model selection does not hold. In this task, we will get the residuals and plot them to check whether they are normally distributed. We will use the jointplot method from seaborn to do this. Upon looking at this result, we can come to a conclusion about the efficacy of our simple linear regression model and whether it could be improved by other methods

Watch Preview

Preview the instructions that you will follow along in a hands-on session in your browser.

Snehan Kekre

About the Host (Snehan Kekre)

Snehan hosts Machine Learning courses at Rhyme. He is in his senior year of university at the Minerva Schools at KGI, pursuing a double major in the Natural Sciences and Computational Sciences, with a focus on physics and machine learning. When not applying computational and quantitative methods to identify the structures shaping the world around him, he can sometimes be seen trekking in the mountains of Nepal.

Frequently Asked Questions

In Rhyme, all projects are completely hands-on. You don't just passively watch someone else. You use the software directly while following the host's (Snehan Kekre) instructions. Using the software is the only way to achieve mastery. With the "Live Guide" option, you can ask for help and get immediate response.
Nothing! Just join through your web browser. Your host (Snehan Kekre) has already installed all required software and configured all data.
You can go to, sign up for free, and follow this visual guide How to use Rhyme to create your own sessions. If you have custom needs or company-specific environment, please email us at
Absolutely. We offer Rhyme for workgroups as well larger departments and companies. Universities, academies, and bootcamps can also buy Rhyme for their settings. You can select sessions and trainings that are mission critical for you and, as well, author your own that reflect your own needs and tech environments. Please email us at
Please email us at and we'll respond to you within one business day.

Ready to join this 1 hour and 4 minutes session?