Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Linear Regression

Intuition

Practice

In this section, you will practice to implement linear regression models using Python on the ‘Boston Housing’ dataset. Three different methods will be used to implement the linear regression model:

  • Simple implementation with numpy.

  • Vectorization implementation with numpy.

  • Linear regression from sklearn framework

Loading...
Loading...

Dataset

Suppose we have a dataset giving the living areas and prices of 47 houses from Portland, Oregon as:

Living area (feet2)Price (1000$s)
2104400
1600330
2400369
1416232
3000540
......

(full data is available at ex1data2.txt )

Loading...

Exploratory Data Analysis

Price

Source
Output
Loading...
Loading...

From the visualization, we observe that the boxplot of price is left-skewed. This means that the median (300) is less than the mean (340). This is because there are a few houses with very high prices, around 250 to 400.

In addition, it is noticable that there is one outlier with a price of 700. This is a very high price compared to the rest of the prices.

Area

Source
Output
Loading...
Loading...

There is a positive correlation between the living area and the price. This means that the price increases as the living area increases.

Number of Bedrooms

Source
Loading...
Loading...

However, there is a weak correlation between the number of bedrooms and the price. Most of the houses have 3 bedrooms, and the price is not significantly different from the houses with 4 bedrooms.

Data Preprocessing

In this step, we normalize the features using the mean and standard deviation.

xi=xi−μiσi\begin{align*} x_i &= \frac{x_i - \mu_i}{\sigma_i} \end{align*}

where:

  • xix_i is the feature

  • μi\mu_i is the mean of the feature

  • σi\sigma_i is the standard deviation of the feature

By normalizing the features, the gradient descent will converge more quickly and avoid overflow during computation.

Loading...

Linear regression

Closed-form solution

The formula for the regression line is y = 0.00 + 0.86x
Source
Output
Source
Loading...
Loading...

Single variable linear regression with gradient descent

Epoch 0, loss: 0.75
Epoch 100, loss: 0.44
Epoch 200, loss: 0.40
Epoch 300, loss: 0.39
Epoch 400, loss: 0.39
Epoch 500, loss: 0.39
Epoch 600, loss: 0.39
Epoch 700, loss: 0.39
Epoch 800, loss: 0.39
Epoch 900, loss: 0.39
The formula for the regression line is y = 0.00 + 0.44x
Source
Loading...
Loading...

Multivariate linear regression with gradient descent and vectorization

Epoch 001, loss = 0.60
Epoch 101, loss = 0.23
Epoch 201, loss = 0.16
Epoch 301, loss = 0.14
Epoch 401, loss = 0.14
Epoch 501, loss = 0.13

Linear regression with scikit-learn

First, we train the linear regression model using the LinearRegression class from the sklearn framework. Then, we evaluate the model using the mean squared error (MSE) and the coefficient of determination (R2R^2).

Mean Squared Error: 0.26
R^2: 0.73
Source
Output
Source
Loading...
Loading...

Now, we train a linear regression model to inspect the relationship between the number of bedrooms (n_bedrooms) and the price. Similarly, we also evaluate the model using the mean squared error (MSE) and the coefficient of determination (R2R^2).

Mean Squared Error: 0.79
R^2: 0.20
Source
Loading...
Loading...