Machine Learning [Python] – Multiple Linear Regression

In this tutorial, we learn how to use scikit-learn library to implement Multiple linear regression. The carbon dioxide emissions dataset will be used again (Machine Learning [Python] – Linear Regression) to build a model, evaluate it and use it to predict an unknown value.

What is Multiple Linear Regression?

Multiple linear regression refers to a statistical technique that is used to predict the outcome of a variable based on the value of two or more variables. It is sometimes known simply as multiple regression, and it is an extension of linear regression. The variable that we want to predict is known as the dependent variable, while the variables we use to predict the value of the dependent variable are known as independent or explanatory variables.

Parts Required

  • Python interpreter (Spyder, Jupyter, etc.).


Following are the steps required to perform this tutorial.

Packages Needed

Import Dataset

As mentioned before, the fuel consumption dataset will be used, which contains model-specific fuel consumption ratings and estimated carbon dioxide emissions for new light-duty vehicles for retail sale in Canada. [Dataset source]

In the post about linear regression, some conclusions have already been presented that can be drawn from the graphical representations and the correlations between the different variables.

Creating Train and Test Dataset

Train/Test split involves splitting the dataset into training and testing sets respectively, which are mutually exclusive. After which, you train with the training set and test with the testing set. This will provide a more accurate evaluation of out-of-sample accuracy because the testing dataset is not part of the dataset that have been used to train the data. It is more realistic for real-world problems. Since this data has not been used to train the model, the model has no knowledge of the outcome of these data points. So, in essence, it is truly out-of-sample testing.

Train Data Distribution

Multiple Regression Model

There are multiple variables that predict the Co2emission. When more than one independent variable is present, the process is called multiple linear regression. For example, predicting co2emission using FUELCONSUMPTION_COMB, ENGINESIZE and CYLINDERS of cars.

Coefficient and Intercept, are the parameters of the fit line. Given that it is a multiple linear regression, with 3 parameters, and knowing that the parameters are the intercept and coefficients of the hyperplane, sklearn can estimate them from our data. Scikit-learn uses plain Ordinary Least Squares method to solve this problem.

Ordinary Least Squares (OLS)

OLS is a method for estimating the unknown parameters in a linear regression model. OLS chooses the parameters of a linear function of a set of explanatory variables by minimizing the sum of the squares of the differences between the target-dependent variable and those predicted by the linear function. In other words, it tries to minimize the sum of squared errors (SSE) or mean squared error (MSE) between the target variable (y) and our predicted output (𝑦̂) over all samples in the dataset.

OLS can find the best parameters using of the following methods:

  • Solving the model parameters analytically using closed-form equations;
  • Using an optimization algorithm (Gradient Descent, Stochastic Gradient Descent, Newton’s Method, etc.).


Variance Regression Score:
If 𝑦̂ is the estimated target output, y the corresponding (correct) target output, and Var is Variance, the square of the standard deviation, then the explained variance is estimated as follow:

The best possible score is 1.0, lower values are worse.

Now let’s use a multiple linear regression with the same dataset but this time use FUEL CONSUMPTION_CITY and FUEL CONSUMPTION_HWY instead of FUELCONSUMPTION_COMB. This means that we will now use four parameters to see if it results in better accuracy.

In this case, although the value of the variance is the same, the residual sum of the squares is less, which means that we have slightly better precision in this case.





[4] IBM – Machine Learning with Python – A Practical Introduction

[5] Udemy – The Data Science Course 2020: Complete Data Science Bootcamp – 365 Careers

[6] Udemy – Machine Learning and Data Science (Python)


Leave a Reply

Your email address will not be published. Required fields are marked *