Linear Regression

Linear regression is a linear model. It is used to make a prediction(denoted by y) based on the given input value x. When working with Linear regression we assume the relationship between the input and output is linear.

The Model :
h(x) = m * x + c

The Data :
x,Y

Overview of Calculation

Model Error:
|h(x) - y| (Always positive)

Make Model More Accurate:
Update weight and bias based on error. (High Error -> High Correction)
m = m + learn_rate*error*x
c = c + learn_rate*error*x

Variable description
x input data
Y expected output
h(x) prediction of model on input x
m slope of line (weight)
c intercept of line (bias)
lr Pace of Learning. It usually lies between 0.1 - 0.0001

The calculation is performed in loop for a number of iteration(we will do it for 100). Higher iteration yields better results, but it takes longer and increases the risk of overfitting.

keeping number of itteration low can also be detrimental beacuse it can lead to underfitting.

Code :
Open notebook in Colab

Linear regression from Scratch


Importing Library
import matplotlib.pyplot as plt       #Creating Plots
import numpy as np                    #Working with numeric array
import random
import time
        
Misc
%matplotlib notebook                  #Makes Notebook zoom-able & resize-able 
random.seed(61)                       #Generate same random numbers on multiple executions
        
Generating Dataset
x_data = np.array([-7.5,-2.5,4,9])    #Input data
y_data = np.array([-1.5,2.5,4.5,7])   #Output data
        
Defining model
def run_model(x):
  global m,c                          #Making Variable Global for Simplicity
  y = m*x + c                         #Predicting value(Output)
  return y                            #Returning prediction
        
Define intial Value of parametrs
m = random.uniform(-1,1)              #Random slope, Dosen't really matter
c = random.uniform(-10,-5)            #Random intercept
        
Makeing intial prediction
y_init = run_model(x_data)            #Prediction with randomly Generated m and c

        
Plotting Intial Model and Data
fig = plt.figure()                    #Create a new figure
plt.axis([-10, 10, -10, 10])          #Size of plot
plt.scatter(x_data,y_data)            #Plotting data
plt.plot(x_data,y_init)               #Plotting model
fig.canvas.draw()                     #Rendering plot
        
Unable to Load Image
Defining Learn Rate and iterations/epochs
learn_rate = 0.01                     #Rate of learning
iterations = 100                      #Number of Epochs
        
Training Model
for i in range(iterations):           #Loop to iterate
  for x,t in zip(x_data,y_data):      #Loop through data
      y = run_model(x)                #Making prediction
      error = np.abs(t - y)           #Error in prediction, Absolute difference of prediction and true value
      m = m + learn_rate*error*x      #Updating slope
      c = c + learn_rate*error        #Updating intercept
      time.sleep(0.1)                 #Slowing down training, You can comment this out 
  
  y_next = run_model(x_data)          #Prediction on Updated Model for Plotting            

  line, = plt.plot(x_data,y_next)     #Plotting new model
  fig.canvas.draw()                   #Rendering plot
  line.remove()                       #Removing Model
        
Unable to Load GIF
Plotting Final Model
plt.plot(x_data,y_next)               #Plotting final model
fig.canvas.draw()                     #Rendering plot
print(error,m,c)                      #Printing model parameters(slope and intercept) 
        
Unable to Load Image


Using scikit-learn library


Importing Library
from sklearn import linear_model
        
Generating and reshaping Dataset
x_data = np.array([-7.5,-2.5,4,9]).reshape([-1, 1])
y_data = np.array([-1.5,2.5,4.5,7]).reshape([-1, 1])
        
Loading Model
regr = linear_model.LinearRegression()
        
Training
regr.fit(x_data,y_data)
        
Making prediction
y_pred = regr.predict(x_data)
        
Plotting
fig = plt.figure()
plt.axis([-10, 10, -10, 10])
plt.scatter(x_data,y_data)
plt.plot(x_data,y_pred)
fig.canvas.draw()
        
Unable to Load Image