Linear Regression

Today we will see what linear regression is all about, linear regression is used to find a relationship between two variables that one independent and one dependent variable these that using linear regression we we find out the intercept and slope that that helps us to frame the future of the graph so we can say that linear regression is a predictive model that make predictions on the outcome of the future.

Steps to perform Linear Regression:

  1. First we import matplotlib.pyplot (for graph) and scipy ( to perform linear regression).

  2. Then we specify the variables that we want to perfrom linear regression on.

  3. In step-3 we define the slope,intercept,r,p and std_err using function stats.linregress().

  4. Then we define a function lets say myfun() that outputs slope*x + intercept that helps us to form the line of LR.

This is the most basic example of Linear regression.

import matplotlib.pyplot as plt
from scipy import stats

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

slope, intercept, r, p, std_err = stats.linregress(x, y)

def myfunc(x):
  return slope * x + intercept

mymodel = list(map(myfunc, x))

plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()

What is polynomial regression?

It is used when we are unable to create a line between the points so now we have to create a ploynomial curve.

Almost whole process of the polynomial regression is similar to that of linear but there are some new commands used like,

linspace()- It is used to create an array, polyfit()- it is used to fit data within the polynomial function and poly1d()- creates the function.

mymodel = np.poly1d(np.polyfit(x,y,3)) this command is very important what this command actually does is it holds the equation for a fitted curve that represents the relation between your x and y

Basic Example:

import matplotlib.pyplot as plt
from scipy import stats
import numpy as np
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel = np.poly1d(np.polyfit(x,y,3)) # polyfit is used to fit data within the polynomial function, and poly1d creates a polynomial.

myline = np.linspace(1,22,100) #linspace command is used to create an array and for this particular code it will create an array between umber 1 and 22 with 100 divisions.

plt.scatter(x,y)
plt.plot(myline,mymodel(myline))
plt.show()