Lab 2 Linear Regression Representation
Lab 2 Linear Regression Representation
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:
3. Explore the effects of weight (w) and bias (b) on model predictions.
3 Theoretical Background
3.1 Linear Regression Model
The simple linear regression model is represented by the equation:
2
• w is the weight (slope of the line)
3.2 Notation
Table 1 presents the notation used throughout this report.
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.
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
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 } " )
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 ()
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 " )
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:
• Explored the effects of weight (w) and bias (b) on model predictions.
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: