Programming a Driverless Car Chapter 5: Linear Regression


  • Working with a regression Problem
  • Defining regression Equation
  • Cost Function
  • Parameters of cost function
  • Objective of Linear Regression
  • Different Regression Methods

Let’s understand this with an example. We have 2 types of data : Input data and output data.


So, we have a table of data with us. We can portray the same data on a graph. So,graph can be made between price and area. Both the axis are:
Y- Dependent Variable, criterion variable or regressand
X- Independent Variable, predictor variables or regressors.

There is no connection between the data, by which we can predict values. The data is all scattered, so to predict a connection between any 2 values, we draw a straight line between the points. The straight line is a model used to create a value for an independent input so we can get a dependent output.

We have chosen points that are very far away from the current model but it passes it from few points. You can call this prior knowledge. Now, how accurate the predicted values will be depends on how optimized the model is.

So, if we provide more data, it can be more optimized and give better predictions. The current model is a straight line and the equation for it will be:

y=mx+c —– Linear regression in one variable
As input we have 2 types of value, X(independent variable) and Y (Dependent Variable)

If we had 2 variables then it would have been multiple variable linear regression.

Solving equations for the above model. The straight line equation is:

We can change x and c to w(0) and w(1):

The main goal for linear regression is minimizing the difference between y and y. If we predict new values for each data set, so it make our current equation more optimized. But how do we minimize the difference?

When we look at various examples, we tend to see that how various intercept value changes based on the value of m and c.

The Cost Function for this will be:

Value of i is taken from 1 to m, to train the model for different models. y-yvalue has to be decreased to generate a more optimum solution. Therefore, we have squared the value of its difference.

Objective of Linear Regression:

  • Establish if there is a relationship between two variables. Example- relationship between housing process and area of house, no of hours of study and the marks obtained, income and spending etc.
  • Prediction of new possible values. Based on the area of house predicting the house prices in a particular month, based on number of hour studied predicting the possible marks. Sales in next 3 months etc.

Regression Algorithms:

  • Simple Linear Regression
    Where we take 1 dependent variable (interval or ratio), 1 independent variable (interval or ratio or dichotomous)
  • Multiple Linear Regression
    Where we have 1 dependent variable (interval or ratio), 2+ independent variable (interval or ratio or dichotomous)
  • Logistic regression
    1 dependent variable (binary), 2+ independent variable(s) (interval or ratio or dichotomous)


  • What is GDA?
  • Learning rate
  • Overview

The goal is to optimize Y, so it can achieve the value of the dependent variable Y.


Where X is the independent variable and Y is the value predicted by the current model.

Repeat this until convergence:

While updating w=0, w=1

:= is an assignment operator.

is learning rate.
Learning rate controls how big step we take while updating our parameter w.
If is too small, gradient descent can be slow.
If is too big, gradient descent can overshoot the minimum, it may fail to converge.

LAB: Linear Regression Example using sklearn

>>>import numpy
>>>import matplotlib

>>>import sklearn 

import matplotlib.pyplot as plt
import numpy as np

from sklearn import datasets, linear_model


#for testing and training



#for training[:-30][-30:]

#creating model using linear model


plt.scatter(diabetes_x_testing,diabetes_y_testing,color= ‘red’) ----this is for testing in a graph form

plt.plot(diabetes_x_testing,reg.predict(diabetes_x_testing),color= ‘green’,linewidth=3)


                                                                       Fig:Output of the program

Here we see, the testing data is scattered and the training model is predicted using linear regression model. This is the most optimized output.

LAB2: Using Linear regression using Gradient descent Algorithm

Working with X=independent Variable , Y=Dependent Variable

We have these 2 data sets, where the first one is for X and the other one is Y.

This is our data: 



import theano

import numpy

import matplotlib.pyplot as plt

#getting both independent and dependent data values



#we will feed these values in regression model



ms=theano.shared(m,name= ‘ms’)

cs=theano.shared(c,name= ‘cs’)

x=theano.tensor.vector( ‘x’)

y=theano.tensor.vector( ‘y’),ms)+cs



gradient= theano.tensor.grad(cost,mss)








For i in range(steps):


print costm

print ‘intercept’

print cs.get_value()

print ‘slope’

print ms.get_value()

#for testing the data




Output: This is the output if we have to train it.

Output: This is the data for testing data values.

Best mean Fitting-

In this we will see how do we use Linear regression to predict better values.

  • Best Fitting Line
  • Calculating Slope
  • Calculating Intercept

Best fitting line-

A line that fits the data “best” will be one for which the n prediction errors-one for each observed data point- are as small as possible in some overall sense.

In linear regression we establish a relationship between x and y. Like this, what we have already seen

For calculating slope:

Instead of using gradient descent algorithm we will use best fitting line

import numpy

import matplotlib.pyplot as plt




This is the data set:

import numpy

import matplotlib.pyplot as plt



def bestfit(x,y):



return m,b



For x in xd:


print m

print b



Recommended Reading:  Introduction to Supervised learning

Next: Chapter 6: Clustering

You can go back to the Table of Contents here

This course evolves with your active feedback. Do let us know your feedback in the comments section below.

Written By
More from Amal Paul

California Witnesses 6 Self Driving GM Car Accidents In The Last Month

GMs’ Cruise Automation Division revealed that as many as 6 of their...
Read More