How to implement Simple linear regression Scikit learn tutorial

Table of Contents

  1. Introduction to linear regression
  2. Problem Statement
  3. Code walkthrough
  4. Video Tutorial

1 Introduction to linear regression

  1. we have an input variable X and one output variable Y and we want to build a linear relationship between these variables 
  2. Here the input variable is called an independent variable and the output variable is called dependent

Problem Statement

  1. variables we have a data set consisting of two columns one has a new franchise fee and one as startup cost here we want to predict the value of start-up cost
  2. X- variable will be animal franchise fee and my Y would be startup cost we would like to get a linear relationship between these two in order to predict startup cost for my testing data set

2 Code walk-through  

Imported the all necessary libraries Matplotlib which will be used for plotting the second one is pandas for reading a dataset

Figure-1

Imported my data set using read CSV function and initialized x and y variables with my annual franchise fee and my startup cost respectively

let’s visualize the dataset 

Figure-2
  1. Here is the data set plotted where the x-axis is the annual franchise fee and the y-axis stands for startup cost these red plots are nothing but our data
  2. split the data set into a training set and test set we will be using a function called train test split which will split a whole dataset into a training set and test dataset.
  3. A fitting straight line through the points the best fitting line is called a regression line.

Training the model

A regression line first we need to import the linear regression class then we need to call the function linear regression which will return an object of its own type.

Figure-3

Then we need to fit the data by calling the fit function

Visualizing the regression line

A list of predicted values can obtain as an output from predict function 

Figure-4
Figure-5

Interpretation of regression line

  1. Predicted values are on the regression line and red dots are true points. If points close to the regression line then our model is good.
  2. Vertical distance from the points to the regression line represents the error of prediction. So the error prediction for any redpoint will be this vertical distance from the regression line
  3. The Error prediction for a point is the value of the point minus the value predicted
  4. As we can see red points which are very near to the regression line its error of prediction is very small by contrast
  5. The point is much farther to the regression line its error of prediction is large

how to choose best line ?

  1. The most commonly used criteria for the best fitting line is the line that minimizes the mean squared error of prediction
  2. To find mean squared error we will import it  from sklearn.metrices and we need to pass our testing data and our prediction data as parameters to it

Video Tutorial

Leave a Reply

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