0% found this document useful (0 votes)
24 views6 pages

Lab 2 Linear Regression Representation

Uploaded by

rainbow.ouk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Lab 2 Linear Regression Representation

Uploaded by

rainbow.ouk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Laboratory Report 2:

Linear Regression Representation and


Implementation
Your Name

Abstract
This laboratory report explores the fundamental concepts and im-
plementation of linear regression in the context of predicting house
prices based on their sizes. We introduce the mathematical represen-
tation of the linear regression model, implement it in Python, and
analyze its performance on a small dataset. The report covers model
initialization, fitting, visualization, and prediction, providing a com-
prehensive understanding of the linear regression process.

Contents
1 Introduction 2

2 Objectives 2

3 Theoretical Background 2
3.1 Linear Regression Model . . . . . . . . . . . . . . . . . . . . . 2
3.2 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

4 Methodology 3
4.1 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2.1 Data Initialization . . . . . . . . . . . . . . . . . . . . 4
4.2.2 Model Definition . . . . . . . . . . . . . . . . . . . . . 4
4.2.3 Model Fitting . . . . . . . . . . . . . . . . . . . . . . . 4
4.2.4 Visualization . . . . . . . . . . . . . . . . . . . . . . . 5
4.2.5 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . 5

1
5 Results and Discussion 5

6 Conclusion 6

7 Future Work 6

1 Introduction
Linear regression is a fundamental technique in machine learning and statis-
tics, used for modeling the relationship between a dependent variable and
one or more independent variables. In this laboratory exercise, we focus on
simple linear regression, where we predict house prices (dependent variable)
based on their sizes (independent variable).

2 Objectives
The primary objectives of this laboratory exercise are to:

1. Develop a thorough understanding of the linear regression model rep-


resentation.

2. Implement the linear regression model fw,b (x) in Python.

3. Explore the effects of weight (w) and bias (b) on model predictions.

4. Visualize the relationship between input features and target variables.

5. Make predictions using the implemented model.

3 Theoretical Background
3.1 Linear Regression Model
The simple linear regression model is represented by the equation:

fw,b (x) = wx + b (1)


where:

• fw,b (x) is the predicted output

• x is the input feature

2
• w is the weight (slope of the line)

• b is the bias (y-intercept)

3.2 Notation
Table 1 presents the notation used throughout this report.

Notation Description Mathematical Python


a Scalar Non-bold -
a Vector Bold -
x Input features x = (x1 , . . . , xn )⊤ x_train
y Target values y = (y1 , . . . , ym )⊤ y_train
m Number of training examples m∈N m
w Weight parameter w∈R w
b Bias parameter b∈R b
fw,b (x(i) ) Model output for x(i) fw,b : R → R f_wb

Table 1: Notation used in the linear regression model

4 Methodology
4.1 Dataset
We use a small dataset of house sizes and their corresponding prices. Table
2 shows the dataset used in this laboratory exercise.

Size (2 ) Price (1000 $)


1000 100
1500 120
2000 150
2200 180
2500 210
4500 400
5000 450
5500 480

Table 2: House size and price dataset

3
4.2 Implementation
We implement the linear regression model using Python and its scientific
computing libraries. The implementation process is divided into the following
steps:

4.2.1 Data Initialization


We begin by initializing our training data using NumPy arrays.
1 import numpy as np
2
3 x_train = np . array ([1000 , 1500 , 2000 , 2200 , 2500 , 4500 , 5000 ,
5500])
4 y_train = np . array ([100 , 120 , 150 , 180 , 210 , 400 , 450 , 480])

Listing 1: Data Initialization

4.2.2 Model Definition


We define a function to compute the model output for any given input x.
1 def c o m p u t e _ m od e l _ o u t p u t (x , w , b ) :
2 """
3 Compute the prediction of the linear model
4
5 Args :
6 x ( ndarray ) : Input features
7 w ( float ) : Weight parameter
8 b ( float ) : Bias parameter
9
10 Returns :
11 ndarray : Predicted output
12 """
13 return w * x + b

Listing 2: Linear Model Definition

4.2.3 Model Fitting


For this exercise, we manually set the weight and bias parameters. In prac-
tice, these parameters would be optimized using techniques such as gradient
descent.
1 # Set weight and bias
2 w = 0.1
3 b = 50
4

4
5 # Compute predictions
6 predictions = c o m p u t e _ m od e l _ o u t p u t ( x_train , w , b )
7 print ( f " Predictions : { predictions } " )

Listing 3: Model Fitting

4.2.4 Visualization
We visualize the data points and the fitted line using Matplotlib.
1 import matplotlib . pyplot as plt
2
3 plt . figure ( figsize =(10 , 6) )
4 plt . scatter ( x_train , y_train , marker = ’x ’ , c = ’r ’ , label = ’ Data
points ’)
5 plt . plot ( x_train , predictions , c = ’b ’ , label = ’ Linear model ’)
6 plt . title ( " House Prices vs . Size " )
7 plt . xlabel ( " Size ( sq ft ) " )
8 plt . ylabel ( " Price (1000 s of dollars ) " )
9 plt . legend ()
10 plt . grid ( True )
11 plt . show ()

Listing 4: Data and Model Visualization

4.2.5 Prediction
Finally, we use our model to predict the price of a house with a specific size.
1 x_new = 3000
2 predicted_price = co m p u t e _ m o d e l _ o u t p u t ( x_new , w , b )
3 print ( f " The predicted price for a house with { x_new } sq ft is
: $ { predicted_price :.2 f } thousand " )

Listing 5: Price Prediction

5 Results and Discussion


[In this section, you would typically include the output of your code, such
as the predictions, the visualization plot, and the prediction for the new
data point. You would also discuss the results, their implications, and any
limitations of the current model.]

5
6 Conclusion
This laboratory exercise has provided a comprehensive introduction to lin-
ear regression, covering its mathematical representation, implementation in
Python, and application to a real-world problem of house price prediction.
We have successfully:

• Implemented the linear regression model fw,b (x).

• Explored the effects of weight (w) and bias (b) on model predictions.

• Visualized the relationship between house sizes and prices.

• Made predictions using our implemented model.

The simple linear regression model serves as a foundation for more com-
plex machine learning algorithms and provides valuable insights into the
relationship between variables.

7 Future Work
Future studies could explore:

• Implementing gradient descent to optimize the model parameters.

• Extending the model to multiple features (multiple linear regression).

• Applying regularization techniques to prevent overfitting.

• Comparing the performance of linear regression with other machine


learning algorithms.

You might also like