×

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization.

Linear Regression is the widely used Machine Learning algorithm. It comes under Supervised Learning. It is an arithmetical technique employed for the predictive study. It estimates real, continuous, or numeric variables such as sales, income, price of any item, etc.

The Linear regression algorithm displays a linear association between dependent variable y and one or more sovereign (x) variables; hence it is called linear regression.  The linear association is depicted in a Linear regression which illustrates the way in which the value of one dependent variable alters with the other independent one.

Graphically, the straight line is employed to indicate the relationship between the variables.

The straight line in the graph shows the mark of regression.

Mathematically, the linear relationship is denoted as:

y= a0 + a1x + c

Here,

Y= Dependent Variable that acts as our target Variable.
X= Independent Variable that acts as our forecaster Variable.
a0= Intercept of the line which provides an extra degree of freedom.
a1 = Coefficient of Linear regression.
c = chance of fault.

Here, X and Y values are considered for the training dataset.

Line of Regression

Now, let us deal with the line of regression.

A line of regression is the one that displays the relationship between the independent and dependent variables.

1. Positive Linear relationship:

Positive Linear relationships are those in which the value of the dependent variable increases on the Y-axis and the value of an independent variable increases on the X-axis 

Linear regression

Here, the line equation is: a0 + a1x + c

2. Negative Linear relationship:

Negative Linear relationships are those in which the value of the dependent variable decreases on the Y-axis and the value of an independent variable increase on the X-axis.

Linear regression

Here, the line equation is: - a0 + a1x + c

Types of Linear Regression:

There exist two types of linear regression.

1. Simple Linear Regression

The value of a mathematical dependent variable is predicted with the aid of a single independent variable in a simple Linear Regression.

2. Multiple Linear Regression

The value of a mathematical dependent variable is predicted with the aid of multiple sovereign variables in multiple linear regression.

The best fit line:

The chief area is to find the finest suitable line while dealing with linear regression. It means that there should exist minimum fault between predicted values and actual values. Hence, the best fit line will have the smallest amount of error.

Cost function:

  1. The dissimilar values of the coefficient of lines (a0, a1) give a dissimilar line of regression. Thus, we need to compute the best values for a0 and a1 to find the best fit line, Hence, the cost function is employed for the calculation of the best fit line.
  2. The Cost function calculates the performance of a linear regression model and thus improves the regression coefficients or weights.
  3. To calculate the exactness of the mapping function, the cost function can be employed which plots the input variable to the resultant variable. The hypothesis function is the other name of the mapping function.

Mean Squared Error (MSE) cost function

The Mean Squared Error (MSE) cost function is employed to calculate the cost function of linear regression. It is the midpoint of squared error arising between the predicted values and actual values.

It can be written

Linear regression

Here,

N = Overall quantity of observations
Yi = Authentic cost
(a1xi+a0) = Forecast cost.

Optimization

Optimization is the process of discovering the best model out of several models and the process of determining the way by which a line of regression fits the group of observations is known as the Goodness of Fit.

R-squared method:

It is a mathematical way employed for the computation of the goodness of fit.

Here, the value lies between 0 and 1 which in turn denotes the percentage.

The good value depends on the domain we are working in. But it should neither be too high nor too low.  0.8 is considered a great value in many areas.

The coefficient of determination is considered for simple linear regression and the coefficient of multiple determination is considered for multiple linear regression.

The calculation formula is:

R-squared = Totality of squares of residuals/ Sum of squares


Related Topics

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Python Struct

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.