Linear Regression

Linear Regression

Linear regression is perhaps one of the most well known and well understood algorithms in statistics and machine learning.

For implementation of linear regression on data set refer to my git hub link:-

What is Linear Regression?

Linear Regression is a machine learning algorithm based on supervised learning. It performs a regression task. Regression models a target prediction value based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. Different regression models differ based on – the kind of relationship between dependent and independent variables, they are considering and the number of independent variables being used.

No alt text provided for this image

Linear regression performs the task to predict a dependent variable value (y) based on a given independent variable (x). So, this regression technique finds out a linear relationship between x (input) and y(output). Hence, the name is Linear Regression.

In the figure above, X (input) is the work experience and Y (output) is the salary of a person. The regression line is the best fit line for our model.

Hypothesis function for Linear Regression :

No alt text provided for this image

Linear Regression Model Representation

While training the model we are given :

x: input training data (uni-variate – one input variable(parameter))

y: labels to data (supervised learning)

When training the model – it fits the best line to predict the value of y for a given value of x. The model gets the best regression fit line by finding the best θ1 and θ2 values.

θ1: intercept

θ2: coefficient of x

Once we find the best θ1 and θ2 values, we get the best fit line. So when we are finally using our model for prediction, it will predict the value of y for the input value of x.

When a coefficient becomes zero, it effectively removes the influence of the input variable on the model and therefore from the prediction made from the model (0 * x = 0). This becomes relevant if you look at regularization methods that change the learning algorithm to reduce the complexity of regression models by putting pressure on the absolute size of the coefficients, driving some to zero.

Cost Function

  • The dependent variable that is to be predicted is denoted by Y.
  • A line that touches the y-axis is denoted by the intercept b0.
  • b1 is the slope of the line, x represents the independent variables that determine the prediction of Y.
  • The error in the resultant prediction is denoted by e.
No alt text provided for this image

The cost function provides the best possible values for b0 and b1 to make the best fit line for the data points. We do it by converting this problem into a minimization problem to get the best values for b0 and b1. The error is minimized in this problem between the actual value and the predicted value.

No alt text provided for this image

We choose the function above to minimize the error. We square the error difference and sum the error over all data points, the division between the total number of data points. Then, the produced value provides the averaged square error over all data points.

It is also known as MSE(Mean Squared Error), and we change the values of b0 and b1 so that the MSE value is settled at the minimum.

Gradient Descent

The next important terminology to understand linear regression is gradient descent. It is a method of updating b0 and b1 values to reduce the MSE. The idea behind this is to keep iterating the b0 and b1 values until we reduce the MSE to the minimum.

To update b0 and b1, we take gradients from the cost function. To find these gradients, we take partial derivatives with respect to b0 and b1. These partial derivatives are the gradients and are used to update the values of b0 and b1.

No alt text provided for this image

A smaller learning rate takes closer to the minimum, but it takes more time and in case of a larger learning rate. The time taken is sooner but there is a chance to overshoot the minimum value. Now that we are through with the terminologies in linear regression, let us take a look at a few advantages and disadvantages of linear regression for machine learning.

Use Case – Implementing Linear Regression

The process takes place in the following steps:

  1. Loading the Data
  2. Exploring the Data
  3. Slicing The Data
  4. Train and Split Data
  5. Generate The Model
  6. Evaluate The accuracy

For implementation refer to the link given at top of the article.

Summary

In this post you discovered the linear regression algorithm for machine learning.

We covered a lot of ground including:

  • The common names used when describing linear regression models.
  • The representation used by the model.
  • Learning algorithms used to estimate the coefficients in the model.
  • Rules of thumb to consider when preparing data for use with linear regression.

Do you have any questions about linear regression or about this post?

Leave a comment and ask, I will do my best to answer.

Thank You




To view or add a comment, sign in

More articles by Gauransh Singh

  • Decision Tree

    Decision tree learning is one of the predictive modelling approaches used in statistics, data mining and machine…

    2 Comments
  • k-Nearest Neighbors

    In pattern recognition, the k-nearest neighbors algorithm (k-NN) is a non-parametric method used for classification and…

    2 Comments
  • Logistic Regression

    Logistic regression is similar to linear regression because both of these involve estimating the values of parameters…

  • Support Vector Regression

    Support Vector regression is a type of Support vector machine that supports linear and non-linear regression. As it…

  • Decision Tree Regression

    For reference how to implement decision tree regression refer to my git hub link:- Decision tree builds regression or…

  • Data Visualization in Python using Matplotlib and Seaborn

    Data Visualization in Python using Matplotlib What is matplotlib? Matplotlib is a Python 2D plotting library which…

    2 Comments
  • Implementation of Data Preprocessing

    Why do we need to do Preprocessing ? For machine learning algorithms to work, it is necessary to convert the raw data…

  • How to preprocess data to make Machine Learning ready using Numpy, Pandas and other libraries.

    Data Preprocessing refers to the steps applied to make data more suitable for data mining. The steps used for Data…

  • Types of Machine Learning Algorithms

    There some variations of how to define the types of Machine Learning Algorithms but commonly they can be divided into…

    2 Comments
  • Machine Learning

    What is Machine Learning? Machine Learning is a sub-area of artificial intelligence, whereby the term refers to the…

    2 Comments

Explore content categories