# scikit-learn: Predict Sales Revenue with Multiple Linear Regression

In this project, you will build and evaluate multiple linear regression models using Python. You will use scikit-learn to calculate the regression, while using pandas for data management and seaborn for plotting. The data for this project consists of the very popular Advertising dataset to predict sales revenue based on advertising spending through media such as TV, radio, and newspaper.

By the end of this project, you will be able to:

• Build univariate and multivariate linear regression models using scikit-learn
• Perform Exploratory Data Analysis (EDA) and data visualization with seaborn
• Evaluate model fit and accuracy using numerical measures such as R² and RMSE
• Model interaction effects in regression using basic feature engineering techniques

Duration (mins)

Learners

#### 4.7 / 5

Rating

We will cover the following tasks in 52 minutes:

### Introduction and Overview

You will be introduced to the Rhyme interface and the 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 Advertising dataset for this project.

In this task, we will load the very popular Advertising dataset about various costs incurred on advertising by different media 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.

### Relationship between Features and Target

It is good practice to first visualize the data before proceeding with analysis and model building. In this task, we will apply seaborn to create scatter plots of each of the three features and the target. This will allow to make a qualitative observations about the linear or non-linear relationships between the features and the target.

### Multiple Linear Regression Model

We will extend the simple linear regression model to include multiple features. Our approach will give each predictor a separate slope coefficient in a single model. This way, we can avoid the drawbacks of fitting a separate simple linear model to each predictor.

In this task, we use scikit-learn’s LinearRegression( ) estimator to calculate the multiple regression coefficient estimates when TV, radio, and newspaper advertising budgets are used to predict sales revenue. Lastly, we will compare and contrast the coefficient estimates from multiple regression to those from simple linear regression.

### Feature Selection

Do all the predictors help to explain the target, or is only a subset of the predictors useful? We will address exactly this question in this task. We will use feature selection to determine which predictors are associated with the response, so as to fit a single model involving only those features.

We will use R², the most common numerical measure of model fit and understand its limitations.

### Model Evaluation Using Train/Test Split and Model Metrics

Assessing model accuracy is very similar to that of simple linear regression. Our first step will be to split the data into a training set and a testing set using the train_test_split( ) helper function from sklearn.metrics. Next, we will create two separate models, one of which uses all predictors, while the other excludes newspaper. We fit the training set to the estimator and make predictions on the testing set. Model fit and the accuracy of the predictions will be evaluated using R² and RMSE.

Visual assessment of our models will involve comparing the residual behaviors and the prediction errors using Yellowbrick. Yellowbrick is an open source, pure Python project that extends the scikit-learn API with visual analysis and diagnostic tools. It is commonly used inside of a Jupyter Notebook alongside pandas data frames.

### Interaction Effect (Synergy) in Regression Analysis

From our previous analysis of the residuals, we concluded that we need to incorporate interaction terms due to the non-additive relationship between the features and target. A simple method to extend our model to allow for interaction effects is to include a third feature by taking the product of the other two features in our model. This feature will have its separate slope coefficient which can be interpreted as the increase in the effectiveness of radio advertising for a one unit increase in TV advertising or vice versa.

## Watch Preview

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

## Reviews

The fact that the different functions and methods of the various libraries were shown in real time was essential. Otherwise it would be barely possible to figure out where one has to use a DOT, PARENTHESES, COLON, SQUARE BRACKETS, and what not, even if one understands the theoretical concepts such as linear regression or R2 score. Thus I couldn't follow the other machine learning and deep learning courses on Coursera (Michigan, Johns Hopkins, Andrew's courses) but this was doable and enabled me to learn more about the coding aspect.

Roland Nikolaus Löchli

My keys in the cloud desktop don't work in chrome. Had to switch to firefox. The cloud desktop is also too small to view.

Yi

1. Copy & past from/to cloud and local doesn't work so none of the key code snippets can be kept 2. With shared screen, video & notebook are both smaller

Guillaume Triclot

## About the Host (Snehan Kekre)

Snehan Kekre is a Machine Learning and Data Science Instructor at Coursera. He studied Computer Science and Artificial Intelligence at Minerva Schools at KGI, based in San Francisco. His interests include AI safety, EdTech, and instructional design. He recognizes that building a deep, technical understanding of machine learning and AI among students and engineers is necessary in order to grow the AI safety community. This passion drives him to design hands-on, project-based machine learning courses on Rhyme.

##### How is this different from YouTube, PluralSight, Udemy, etc.?
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.
##### Is this session really free?
Absolutely! Your host (Snehan Kekre) has provided this session completely free of cost!
##### Can I buy Rhyme sessions for my company or learning institution?
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 projects 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 help@rhyme.com
##### What kind of accessibility options does Rhyme provide?
Rhyme strives to ensure that visual instructions are helpful for reading impairments. The Rhyme interface has features like resolution and zoom that will be helpful for visual impairments. And, we are currently developing a close-caption functionality to help with hearing impairments. Most of the accessibility options of the cloud desktop's operating system or the specific application can also be used in Rhyme. If you have questions related to accessibility, please email us at accessibility@rhyme.com
##### Why don't you just use containers or virtual browsers?
We started with windows and linux cloud desktops because they have the most flexibility in teaching any software (desktop or web). However, web applications like Salesforce can run directly through a virtual browser. And, others like Jupyter and RStudio can run on containers and be accessed by virtual browsers. We are currently working on such features where such web applications won't need to run through cloud desktops. But, the rest of the Rhyme learning, authoring, and monitoring interfaces will remain the same.
##### I have a different question
Please email us at help@rhyme.com and we'll respond to you within one business day.

## More Projects by Snehan Kekre

43 minutes
###### Classify Toxic Comments using Convolutions with Keras
1 hour and 11 minutes
42 minutes
57 minutes
52 minutes
41 minutes
52 minutes
###### Project: Facial Expression Recognition in Keras
1 hour and 24 minutes
###### Project: Real-Time Object Detection with YOLOv3
1 hour and 5 minutes
###### Project: Image Super Resolution using Autoencoders in Keras
1 hour and 4 minutes
53 minutes
###### scikit-learn: Logistic Regression for Sentiment Analysis
1 hour and 26 minutes
45 minutes
58 minutes
57 minutes
###### Predictive Analytics for Business with H2O and R
1 hour and 6 minutes
47 minutes
43 minutes
###### Create Interactive Dashboards with Streamlit and Python
1 hour and 33 minutes
50 minutes
53 minutes
###### Project: Regression Analysis with Yellowbrick
1 hour and 10 minutes
55 minutes
###### Project: Named Entity Recognition using LSTMs with Keras
1 hour and 10 minutes
47 minutes
###### Build a Machine Learning Web App with Streamlit and Python
1 hour and 21 minutes
###### Logistic Regression with NumPy and Python
1 hour and 3 minutes
47 minutes
58 minutes
###### Explainable Machine Learning with LIME and H2O in R
1 hour and 24 minutes
###### Automatic Machine Learning with H2O AutoML
1 hour and 1 minute
58 minutes
###### Build a Data Science Web App with Streamlit and Python
1 hour and 21 minutes
###### Project: Support Vector Machines with scikit-learn
1 hour and 40 minutes
48 minutes
52 minutes
52 minutes
###### Project: Predict Employee Turnover with scikit-learn
1 hour and 4 minutes
###### Project: Predictive Modelling with Azure Machine Learning Studio
1 hour and 6 minutes
###### Project: Anomaly Detection in Time Series Data with Keras
1 hour and 3 minutes
59 minutes
###### Project: Generate Synthetic Images with DCGANs in Keras
1 hour and 7 minutes
###### Project: Perform Feature Analysis with Yellowbrick
1 hour and 15 minutes