Lab 03 - Linear Regression
Lab 03 - Linear Regression
BESE – 13B
03rd February 2025
This laboratory exercises the python implementation of linear regression. Linear regression is
a basic supervised learning technique in which parameters are trained on a dataset to fit a
model that best approximates that dataset.
Objectives
Theory
Linear Regression is a very basic supervised learning technique. To calculate the loss in each
training example, the difference between a hypothesis and the label (y) is calculated. The
hypothesis is a linear equation of the features (x) in the dataset with the coefficients acting as
the weight parameters. These weight parameters are initialized to random values at the start
but are then trained over time to learn the model. The cost function is used to calculated the
error between the predicted y^ and the actual y.
A major problem in the training is that the weights that are trained may fit the model for only
the data it is given. This means that the model will not generalize to examples outside the
dataset and is referred to as “overfitting”. Such overfitting makes the machine learning
implementation very impractical for real-life applications where data has high variation. To
prevent overfitting of the model, a modification in the cost function and gradient descent is
implemented. This modification is called regularization and is itself controlled by a
hyperparameter (lambda).
cost_function(X, y, lambd)
The X and y are the features and labels of either the training or the cross-validation datasets.
This is useful as it can be used for either the training examples or the cross-validation
examples of the dataset. The lambd is the regularization parameter (Note that lambda is a
keyword reserved in python). The function will calculate the losses to return the overall cost
value. The cost function is given by:
m
1
J ( w )= ∑ ¿¿
2m i=1
The m is the number of the examples in the dataset and n is the total number of features (or
non-bias weights) in the hypothesis. Write the code for the cost function and implement it for
your training and cross-validation datasets to print out the cost. Provide the code and all
relevant screenshots of the final output.
The alpha is the learning rate (hyperparameter 1) and lambd is the regularization parameter
(hyperparameter 2). The gradient descent algorithm is given as follows:
m
∂J 1 λ
d w j= = ∑ (h( x (i ))– y (i )) x j(i) + w j
∂ w j m i=1 m
m
∂J 1
db= = ∑ (h(x ( i)) – y (i) )
∂ b m i=1
∂J
w j :=w j−α
∂wj
∂J
b :=b−α
∂wj
For the submission, you will need to run the gradient descent algorithm once to update the
weights. You will need to print the weights, training cost and validation cost both before and
after the weight update. Provide the code and all relevant screenshots of the final output.