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:
where: • is the predicted output, • is the input feature, • is the y-intercept or bias term, • is the slope or weight associated with the input feature.
For multiple features, the hypothesis extends to:
Or, in vectorized form:
where: • is the matrix of input features, • 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:
where is the number of training examples, and is the actual output for the -th example.
Batch Gradient Descent
Batch gradient descent is an iterative optimization algorithm used to minimize the cost function . It updates the parameters in the opposite direction of the cost function's gradient:
- Initialize the parameters to some initial values (often zeros).
- Repeat until convergence: • Calculate predictions: • Compute the gradient: • Update parameters:
The learning rate 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 () | Target () |
| 1 | 2 |
| 2 | 2.5 |
| 3 | 3.5 |
| 4 | 5 |
Batch Gradient Descent Steps:
• Initialize , . • Set learning rate . • Calculate gradient: • For : • For : • Update: • •
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 Points | Description |
| Hypothesis Representation | |
| Cost Function | |
| Gradient Descent Update | |
| Convergence Criteria | Small gradient change, min cost change, or set epochs |
| Common Preprocessing | Feature 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.

