You are on page 1of 4

Department of Computing

CS471: Machine Learning (Spring 2020)


Class: BESE-8AB

Lab 01: Linear Regression with One Variable

CLO1: Understand machine learning algorithms, tools and techniques.

CLO3: Use modern tools to solve practical problems.

Date: January 24, 2020

Time: 9:00AM – 12:00PM & 2:00PM – 5:00PM

Instructors: Dr. Faisal Shafait


Dr. Muhammad Muneeb Ullah

CS471: Machine Learning Page 1


Lab 01: Linear Regression with One Variable

Introduction

• Linear regression with one variable — Finding the best-fitting straight line through
points of a data set. (link)

Objectives

• Implement univariate linear regression model.

Tools/Software Requirement

Python 3, Scikit-learn, Matplotlib, Numpy, Jupyter

Task Description

Create a Python 3 Jupyter Notebook on Google Colaboratory


(https://colab.research.google.com/). The Jupyter Notebook is an open-source web application
that allows you to create and share documents that contain live code, equations, visualizations
and narrative text. You can find information about Jupyter notebook at
http://jupyter.org/index.html.

Now execute the following code snippet to create some sample data points, and visualization.

# imports
import numpy as np
import matplotlib.pyplot as plt

# generate random data-set
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)

# plot
plt.scatter(x,y,s=10)
plt.xlabel('x')

CS471: Machine Learning Page 2


plt.ylabel('y')
plt.show()

Using the Scikit-learn library (https://scikit-learn.org/stable/), train a linear regression model on


the training data (x, y). Once trained, use the learnt model to predict y for x in the training data.
To evaluate the model, you can use Root Mean Squared Error (RMSE), which is available in
sklearn.metrics:

m
RMSE=
1
∑√
m i=1
(hθ ( x (i) )− y(i) )
2

Compute the RMSE, and present the error rate. Also, print the values of the learnt parameters θ0
and θ1.

Finally, plot the linear hypothesis along with the training data, like this:

CS471: Machine Learning Page 3


Deliverables

• Students are required to upload the solutions in Jupyter Notebook format (.ipynb) on
LMS.

• The file name must contain your name and CMS ID in the following format.
<Lab01_your CMS ID_your name.ipynb>

CS471: Machine Learning Page 4

You might also like