Understanding Linear Regression. Mathematical Foundations and Implementations in PyTorch and TensorFlow

Featured image

Understanding Linear Regression. Mathematical Foundations and Implementations in PyTorch and TensorFlow

Blog Post by - Shivang Kumar

Contents

Introduction

Linear regression stands as one of the most fundamental algorithms in the realm of machine learning and predictive analytics. Rooted in statistical modeling, it serves as a cornerstone technique, particularly for those embarking on their data science journey. The beauty of linear regression lies in its simplicity and interpretability, making it an invaluable tool for making predictions based on relationships between numerical variables.

At its core, linear regression aims to establish a linear relationship between a dependent variable (often denoted as y) and one or more independent variables (denoted as x). The model predicts the outcome (y) as a linear combination of the input variables (x). This simplicity, however, belies its power and versatility in various applications, ranging from economics and healthcare to engineering and beyond.

In the upcoming sections, we will delve deep into the mathematical underpinnings of linear regression. Understanding these fundamentals is crucial for appreciating how the algorithm makes predictions and how it can be fine-tuned for better performance. We will explore the key concepts such as the linear regression equation, the cost function, and the optimization process.

Following this, we’ll transition into the practical aspects of linear regression, showcasing its implementation in two of the most popular machine learning frameworks: PyTorch and TensorFlow. These sections will not only include detailed code examples but will also provide insights into the unique features and approaches of each framework. Whether you are a novice in the field or looking to refresh your knowledge, this comprehensive guide will equip you with both the theoretical understanding and practical skills to master linear regression.

Stay tuned as we embark on this exciting journey to unravel the intricacies of linear regression and harness its power in PyTorch and TensorFlow!. And will try to predict House price which Linear regression model

Alt Text

The Mathematics of Linear Regression

Definition of Linear Regression

Linear regression is a statistical method used in machine learning and data analysis for modeling the relationship between a dependent variable and one or more independent variables. The essence of linear regression is to find a linear relationship or trend in the data. This approach is particularly useful for predicting the value of the dependent variable based on the values of the independent variables.

Alt Text

The Linear Equation: y = mx + c The fundamental equation of linear regression is y = mx + c. Here:

y represents the dependent variable - the variable we aim to predict or estimate. x represents the independent variable - the variable we use for making predictions. m is the slope of the line, which indicates the rate at which y changes for a unit change in x. c is the y-intercept, the value of y when x is 0. In the context of our house price example, if y represents house prices and x represents house sizes, m would indicate how much the price increases per square meter, and c would be the base price of a house when its size is zero.

Cost Function: Mean Squared Error (MSE)

Alt Text

The Mean Squared Error (MSE) is a widely used cost function for linear regression. It measures the average squared difference between the actual and predicted values. The formula for MSE is:

MSE = N 1 ​ ∑ i=1 N ​ (actual y ​ −predicted y ​ ) 2

where N is the number of data points. The goal in linear regression is to minimize this MSE, which would imply that our model’s predictions are as close to the actual values as possible.

Optimization: Gradient Descent Gradient descent is an optimization algorithm used to minimize the MSE in linear regression. It works by iteratively adjusting the parameters m and c to reduce the cost (MSE). The algorithm takes steps proportional to the negative of the gradient (or approximate gradient) of the function at the current point. If correctly implemented, it leads us to the minimum of the function, i.e., the best values of m and c for our linear model.

Implementing Linear Regression in PyTorch

pip install torch torchvision

For specific installation instructions tailored to your system (including GPU support), visit the PyTorch official website.

import torch

# Dummy data
# Features: size, bedrooms, age of the house
features = torch.tensor([[1200, 3, 20],
                         [1500, 4, 15],
                         [850, 2, 25],
                         [950, 2, 30],
                         [1700, 5, 10],
                         [1300, 3, 22]], dtype=torch.float32)

# Prices in $1000s
prices = torch.tensor([[200],
                       [250],
                       [180],
                       [190],
                       [300],
                       [220]], dtype=torch.float32)
# Creating a simple PyTorch model for predicting house prices
class LinearHouseModel(nn.Module):
    def __init__(self):
        super(LinearHouseModel, self).__init__()
        # Assuming house prices depend on 3 features
        self.linear = nn.Linear(3, 1)

    def forward(self, x):
        return self.linear(x)

# Creating a new model
mean = features.mean(0, keepdim=True)
std = features.std(0, unbiased=False, keepdim=True)
normalized_features = (features - mean) / std
model = LinearHouseModel()
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
# The model with the normalized data
num_epochs = 1000
train_losses = []
for epoch in range(num_epochs):
    # Forward pass
    outputs = model(normalized_features)
    loss = criterion(outputs, prices)

    # Backward and optimize
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    train_losses.append(loss.item())
plt.plot(range(num_epochs), train_losses, label='Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss Over Time')
plt.legend()
plt.show()

Alt Text

# Predictions
predictions = model(normalized_features).detach().numpy()

# Plotting the actual and predicted prices
plt.scatter(np.arange(len(prices)), prices, label='Actual Prices')
plt.scatter(np.arange(len(predictions)), predictions, label='Predicted Prices', color='red')
plt.xlabel('House')
plt.ylabel('Price (in $1000s)')
plt.title('Actual vs Predicted House Prices')
plt.legend()
plt.show()

Alt Text

Implementing Linear Regression in TensorFlow

pip install tensorflow
# Dummy data
# Features: size, bedrooms, age of the house
features = np.array([[1200, 3, 20],
                         [1500, 4, 15],
                         [850, 2, 25],
                         [950, 2, 30],
                         [1700, 5, 10],
                         [1300, 3, 22]], dtype=float)

# Prices in $1000s
prices = np.array([[200],
                       [250],
                       [180],
                       [190],
                       [300],
                       [220]], dtype=float)
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

mean = features.mean(axis=0)
std = features.std(axis=0)
normalized_features = (features - mean) / std

model = keras.Sequential([
    keras.layers.Dense(1, input_shape=(3,))
from tensorflow.keras.optimizers import SGD

sgd = SGD(learning_rate=0.01)
model.compile(optimizer=sgd, loss='mean_squared_error')
history = model.fit(normalized_features, prices, epochs=1000, verbose=0)
plt.plot(range(num_epochs), train_losses, label='Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss Over Time')
plt.legend()
plt.show()

Alt Text

# Predictions
predictions = model(normalized_features).detach().numpy()

# Plotting the actual and predicted prices
plt.scatter(np.arange(len(prices)), prices, label='Actual Prices')
plt.scatter(np.arange(len(predictions)), predictions, label='Predicted Prices', color='red')
plt.xlabel('House')
plt.ylabel('Price (in $1000s)')
plt.title('Actual vs Predicted House Prices')
plt.legend()
plt.show()

Alt Text

Conclusion

Summarizing Key Points In this comprehensive guide, we delved into the world of linear regression, one of the most fundamental algorithms in machine learning and predictive analytics. We started by exploring the mathematical foundations of linear regression, including the linear equation y = mx + c, the Mean Squared Error (MSE) as the cost function, and the gradient descent optimization method.

We then transitioned to practical implementations, demonstrating how to build linear regression models in two of the leading machine learning frameworks: PyTorch and TensorFlow. Each framework was explored through the lens of a simple yet illustrative example: predicting house prices based on house size.

Differences and Similarities in Implementation While PyTorch and TensorFlow both effectively handle linear regression tasks, their approaches and syntax exhibit some differences:

PyTorch offers a more granular level of control with explicit training loops, making it a go-to choice for research and development where such flexibility is crucial. TensorFlow, particularly with its Keras API, provides a more high-level, streamlined approach, making it user-friendly, especially for beginners and in production environments. Despite these differences, both frameworks share core similarities, such as the use of tensors for data representation, backpropagation for learning, and a strong community support.