ML Lab 06 Manual - Linear Regression 1 (Version 6)
ML Lab 06 Manual - Linear Regression 1 (Version 6)
Machine Learning
Introduction
Objectives
Lab Conduct
Machine Learning
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. This cost is used to determine how the weights are to be adjusted
in what is called the gradient descent algorithm. The gradient descent uses a
step size (alpha) as a hyperparameter which can be tuned. This
hyperparameter is varied to determine the model that best fits the dataset.
Machine Learning
Download a dataset containing several columns. You will need to select any 3 of
the columns as features and one of the columns as label of the dataset. Ensure
that the label is of continuous values. Load the dataset into your python
program as NumPy arrays (Xtrain ,ytrain). Print the dataset (you need to show any
5 rows of the dataset).
x j [ i ] – x¿
x j ❑scaled [i]=
x¿ – x¿
You will use these rescaled values in the upcoming tasks. Print the rescaled
dataset (you need to show any 5 rows of the dataset).
Machine Learning
In the tasks 3 and 4, you will write the cost function and gradient descent
function respectively. In task 5, you will use both of these functions in order to
perform linear regression.
In this task, you will write a cost function that calculates the overall loss across
a set of training examples:
cost_function(X, y)
The X and y are the features and labels of the training dataset. The function will
return the cost value. The cost function is given by:
m
1
J (w , b)= ∑ ¿¿
2m i =1
The m is the number of the training examples in the dataset. Write the code for
the cost function and implement it to print out the cost. Provide the code and all
relevant screenshots of the final output.
Machine Learning
### TASK 3 SCREENSHOT ENDS HERE ###
gradient_descent(X, y, alpha)
The X and y are the features and labels of the training dataset, alpha is the
learning rate which is a tuning hyperparameter. The gradient descent algorithm
is given as follows:
m
∂J 1
d w j= = ∑ (h( x (i ))– y (i )) x j(i)
∂ w j m i=1
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 and cost both before and
after the weight update. Provide the code and all relevant screenshots of the
final output.
Machine Learning
### TASK 4 CODE ENDS HERE ###
Machine Learning
provide at least 3 plots. Mention the alpha value in the plot titles. Ensure all the
axes are labeled appropriately. Note down the weights at the final epochs.
Machine Learning