machine learning
linear regression
batch gradient descent
regression analysis
supervised learning

Machine learning - Linear regression using batch gradient descent

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction to Linear Regression

Linear regression is one of the simplest and most commonly used algorithms in machine learning. It is applicable for predictive analysis where the objective is to predict a continuous output variable based on one or more input variables, also known as features. Linear regression assumes a linear relationship between the input variables and the single output variable.

Mathematical Formulation

The hypothesis for a simple linear regression model is given by:

h_θ(x)=θ_0+θ_1xh\_\theta(x) = \theta\_0 + \theta\_1 x

where: • hθ(x)h_\theta(x) is the predicted output, • xx is the input feature, • θ0\theta_0 is the y-intercept or bias term, • θ1\theta_1 is the slope or weight associated with the input feature.

For multiple features, the hypothesis extends to:

h_θ(X)=θ_0+θ_1x_1+θ_2x_2+...+θ_nx_nh\_\theta(X) = \theta\_0 + \theta\_1 x\_1 + \theta\_2 x\_2 + ... + \theta\_n x\_n

Or, in vectorized form:

h_θ(X)=Xθh\_\theta(X) = X \cdot \theta

where: • XX is the matrix of input features, • θ\theta is the vector containing the model parameters.

Cost Function

The cost function evaluates the performance of our linear regression model. It is usually chosen to be the mean squared error between the predictions and the actual target values:

J(θ)=12m_i=1m(h_θ(x(i))y(i))2J(\theta) = \frac{1}{2m} \sum\_{i=1}^{m} (h\_\theta(x^{(i)}) - y^{(i)})^2

where mm is the number of training examples, and y(i)y^{(i)} is the actual output for the ii-th example.

Batch Gradient Descent

Batch gradient descent is an iterative optimization algorithm used to minimize the cost function J(θ)J(\theta). It updates the parameters θ\theta in the opposite direction of the cost function's gradient:

  1. Initialize the parameters θ\theta to some initial values (often zeros).
  2. Repeat until convergence: • Calculate predictions: y^=Xθ\hat{y} = X \cdot \theta • Compute the gradient: θJ(θ)=1mXT(Xθy)\nabla_\theta J(\theta) = \frac{1}{m} X^T (X \cdot \theta - y) • Update parameters: θ:=θαθJ(θ)\theta := \theta - \alpha \nabla_\theta J(\theta)

The learning rate α\alpha is a hyperparameter that determines the step size in the direction of the gradient.

Example

Consider a simple dataset with one feature for an illustrative example. Assume we have:

Feature (xx)Target (yy)
12
22.5
33.5
45

Batch Gradient Descent Steps:

• Initialize θ0=0\theta_0 = 0, θ1=0\theta_1 = 0. • Set learning rate α=0.01\alpha = 0.01. • Calculate gradient: • For θ0\theta_0: 14(hθ(x(i))y(i))\frac{1}{4} \sum (h_\theta(x^{(i)}) - y^{(i)}) • For θ1\theta_1: 14((hθ(x(i))y(i))x(i))\frac{1}{4} \sum ((h_\theta(x^{(i)}) - y^{(i)}) \cdot x^{(i)}) • Update: • θ0:=θ0α×gradient for θ0\theta_0 := \theta_0 - \alpha \times \text{gradient for } \theta_0θ1:=θ1α×gradient for θ1\theta_1 := \theta_1 - \alpha \times \text{gradient for } \theta_1

These steps iterate until the cost function does not reduce significantly, indicating convergence.

Key Considerations

Choice of Learning Rate: Too small a learning rate can lead to a long convergence time, while too large a learning rate can cause the algorithm to overshoot the minimum or diverge. • Feature Scaling: Preprocessing steps like normalization or standardization can significantly affect the convergence speed of gradient descent. • Convergence Criteria: Generally defined by a small enough gradient, minimal changes in successive iterations, or set number of epochs.

Summary Table

Key PointsDescription
Hypothesis Representationhθ(x)=θ0+i=1nθixih_\theta(x) = \theta_0 + \sum_{i=1}^{n} \theta_i x_i
Cost FunctionJ(θ)=12mi=1m(hθ(x(i))y(i))2J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)})^2
Gradient Descent Updateθ:=θαθJ(θ)\theta := \theta - \alpha \nabla_\theta J(\theta)
Convergence CriteriaSmall gradient change, min cost change, or set epochs
Common PreprocessingFeature scaling, handling missing data

Conclusion

Batch gradient descent is a powerful and widely used optimization algorithm for training linear regression models. Understanding its underlying mechanics and considerations are crucial for effectively applying linear regression to various predictive modeling problems in machine learning.


Course illustration
Course illustration

All Rights Reserved.