DEV Community

Cover image for Linear Regression : From Theory to Practice
mohiyaddeen7
mohiyaddeen7

Posted on

Linear Regression : From Theory to Practice

In this guide, we’ll explain linear regression, how it works, and walk you through the process step-by-step. We’ll also cover feature scaling and gradient descent, key techniques for improving your model’s accuracy. Whether you’re analyzing business trends or diving into data science, this guide is a great starting point.


Table of Contents


Introduction

Linear regression is a simple yet powerful tool used to understand relationships between different factors and make predictions. For instance, you might want to know how your study hours impact your test scores, how much a house could sell for based on its size and location, or how sales might increase with more advertising. Linear regression allows us to examine data points — like hours studied or advertising spend — and draw a straight line that best predicts an outcome, such as test scores or sales figures. This technique is valuable in many areas, helping us make informed decisions based on data.

Understanding Supervised Learning

Before diving into linear regression, it’s essential to understand supervised learning, a machine learning approach that uses labeled data to train models. In supervised learning, we provide the model with training examples that include features (input variables) and their corresponding labels (correct outputs).

There are two main types of supervised learning tasks:

  1. Regression: This predicts a continuous value from an infinite range of possible outputs. For example, predicting house prices based on various features.
  2. Classification : This differs from regression by predicting a class or category from a limited set of possible categories. For instance, determining whether an email is spam or not.

What is Linear Regression?

Linear regression is a supervised learning method used in statistics and machine learning to understand the relationship between two types of variables: independent variables (the factors we think influence an outcome) and a dependent variable (the outcome we want to predict).

The goal is to find the best-fit line that represents this relationship using a linear equation. By analyzing labeled data (data with known outcomes), linear regression helps us understand how changes in the independent variables influence the dependent variable.

Terminology

Common Notation In Machine Learning

Simple Linear Regression

Simple linear regression examines the relationship between one dependent variable and one independent variable. It aims to model the relationship by fitting a straight line to the data points, which can be expressed with the equation:

Simple Linear Regression equation

In this equation:

  • y_hat(or f_wb(x)) :The dependent variable, which represents the outcome being predicted. This is the value we aim to estimate based on the input from the independent variable.
  • b : This is the intercept of the regression line. It signifies the expected value of the dependent variable y when the independent variable x is zero. The intercept allows the regression line to adjust vertically to better fit the data.
  • w : The coefficient of the independent variable x. This coefficient indicates how much the dependent variable y_hat changes for a one-unit change in x. A positive w suggests that as x increases, y_hat​ also increases, while a negative w indicates an inverse relationship.
  • x : The independent variable, which serves as the predictor in the model. This variable is the input used to estimate the outcome represented by y_hat.

Multiple Linear Regression

Multiple linear regression extends the concept of simple linear regression by examining the relationship between one dependent variable and two or more independent variables. This approach allows us to model more complex relationships and understand how multiple factors influence the outcome.

Multiple Linear Regression equation

Where :

  • n : Total number of features (independent variables)

Cost Function

The cost function, also known as the loss function, quantifies the difference between the expected (true) values and the predicted values generated by the model. It measures how well the model performs on a given dataset.In simple linear regression, the most commonly used cost function is the Mean Squared Error.

Mean squared error equation

Where :

  • m is the number of training examples
  • y_hat is the predicted value
  • y is the actual or expected value

Feature Scaling

Feature scaling is a crucial step in the preprocessing of data, especially when working with algorithms that rely on distance calculations or gradient descent optimization, such as linear regression, logistic regression, and support vector machines. The purpose of feature scaling is to standardize the range of independent variables or features in the data to ensure that they contribute equally to the model’s learning process.

Common Techniques for Feature Scaling

Mean Normalization

Mean normalization involves adjusting the values of features to have a mean of zero.

Mean normalization equation

Characteristics

  • Data ranges from approximately [−1,1] or close to it.
  • Sensitive to outliers, which can skew the mean and affect the normalization.

Use Cases

  • Linear Regression : Helps in improving convergence during training.
  • Gradient-Based Algorithms : Neural networks and other gradient-based algorithms often converge faster when data is centered around zero.
  • Datasets without Significant Outliers : Particularly effective for datasets with similar ranges and no extreme outliers.

Min-Max Scaling

Min-Max scaling is a technique used to re-scale features to a fixed range, typically [0,1] or [−1,1].

Min-Max scaling equation

Characteristics

  • Fixed Range : Scales data to a specific range, usually [0,1].
  • Sensitivity to Outliers : It can be affected significantly by outliers, which may distort the scaling of the other values.

Use Cases

  • Image Processing : Commonly used in deep learning models like Convolutional Neural Networks (CNN’s), where pixel values are scaled to [0,1].
  • Distance-Based Algorithms : Essential for algorithms that rely on distance calculations, such as k-nearest neighbors (KNN), k-means clustering, and support vector machines (SVM), to ensure equal contribution from all features.
  • Tree-Based Models : Although less critical for tree-based models (like decision trees and random forests) compared to other algorithms, it can still help in scenarios where features have vastly different scales.

Z-Score Standardization

Z-score standardization, also known as standard scaling, transforms features to have a mean of zero and a standard deviation of one. This technique is particularly useful for algorithms that assume normally distributed data.

Z-score standardization equation

Where :

  • sigma is the standard deviation of the feature.

Characteristics

  • Mean Centered : Centers data at zero.
  • Unit Variance : Ensures a standard deviation of one.
  • Robustness to Outliers : More robust compared to Min-Max scaling but still sensitive to extreme outliers.

Use Cases

  • Neural Networks : Enhances performance and speeds up convergence during training.
  • Principal Component Analysis (PCA) and Linear Discriminant Analysis (LDA) : Required for these techniques to ensure all features contribute equally.
  • Gaussian Naive Bayes: Improves classification performance by normalizing input features.

Robust Scaling

Robust scaling is a technique used to scale features based on the median and interquartile range (IQR). This method is particularly useful for datasets with significant outliers, as it reduces the influence of these outliers on the scaled values.

Robust Scaling equation

Where :

  • IQR(x) is the interquartile range of the feature, defined as the difference between the 75th and 25th percentiles of the training set

Characteristics

  • Median Centered : Centers the data around the median instead of the mean, making it more resilient to outliers.
  • Interquartile Range (IQR) : Scales the data using the IQR, which is the difference between the 75th percentile (Q3) and the 25th percentile (Q1) of the training data. This helps preserve the distribution’s robustness.

Use Cases

  • Data with Outliers : Effective in scenarios where outliers are present.
  • Finance: Useful in financial datasets that may contain extreme values.
  • Environmental Data : Applies well to environmental datasets where measurements can vary widely.

Gradient Descent

Gradient descent is a powerful optimization algorithm used to train machine learning models, including linear regression. Its primary goal is to minimize the error between expected and predicted values.

Initially, the slope of the cost function may be steep at a starting (arbitrary) point. As the algorithm iterates and updates parameters, the slope gradually decreases, guiding the model toward the lowest point of the cost function, known as the point of convergence or local minima. At this convergence point, the cost function reaches its minimum value, indicating that the model predictions are as close as possible to the actual values. Once the parameters reach this point, further updates yield minimal changes to predictions, demonstrating that the optimization process has effectively identified the best-fitting parameters for the data.

The process involves the following key steps:

  1. Initialization : Start with random values for the model parameters (e.g., the intercept b and coefficients w).
  2. Calculate the Gradient : Compute the gradient of the cost function with respect to the model parameters. This gradient represents the direction and rate of change of the cost function.
  3. Update Parameters : Adjust the model parameters in the opposite direction of the gradient to reduce the error. The update rule is given by:
  4. Iterate : Repeat the process until the changes in the cost function are minimal or a specified number of iterations is reached.

TIPS : Plot iterations (x-axis) versus cost (y-axis). If the plot shows a smooth, downward trend, your implementation is likely correct.

gradient cost versus iterations plot

Types of Gradient Descent

Batch Gradient Descent

  • Advantages : Provides a stable and accurate estimate of the gradient since it uses the entire dataset. It can converge directly to the global minimum for convex functions.
  • Disadvantages : Can be very slow for large datasets since it processes all samples in every iteration.
  • Use Cases : Often used in scenarios where the dataset is small enough to fit in memory, such as linear regression or logistic regression on tabular data.

Stochastic Gradient Descent (SGD)

  • Advantages : Faster updates since it processes one sample at a time, which can lead to quicker convergence. It can help escape local minima due to its inherent noise.
  • Disadvantages : Convergence is more erratic and may oscillate around the minimum, making it less stable.
  • Use Cases : Commonly applied in online learning scenarios, real-time prediction, or when dealing with large datasets that cannot be processed in their entirety, such as training neural networks on image data.

Mini-batch Gradient Descent(MBD)

  • Advantages : Combines the advantages of both batch and stochastic gradient descent. It leads to faster convergence than batch gradient descent and more stable convergence than SGD. It can also leverage vectorization for efficient computation.
  • Disadvantages : Choosing the size of the mini-batch can be challenging and may affect convergence speed and stability.
  • Use Cases : Frequently used in deep learning applications, especially when training on large datasets, such as image classification tasks in convolutional neural networks (CNN's) or natural language processing models.

Gradient Descent for Simple Linear Regression

Gradient Descent Steps for Simple Linear Regression

  1. Initialization Start with initial values for the model parameters. These values can be chosen randomly or set to zero.

initialization gradient descent for simple linear regression

  1. Calculate the Gradient Compute the gradient of the cost function with respect to the model parameters. This gradient represents the direction and rate of change of the cost function.

calculate the gradients in gradient descent for simple linear regression

calculate the gradients in gradient descent for simple linear regression

  1. Update Parameters Adjust the model parameters in the opposite direction of the gradient to reduce the error. The update rule is given by:

update parameters in gradient descent for simple linear regression

update parameters in gradient descent for simple linear regression

where :

  • J(w, b) is the cost function, which is the mean squared error (MSE) used above.
  • Alpha is the learning rate, a small positive number between 0 and 1. It controls the size of the step that gradient descent takes downhill to reach the point of convergence or a local minimum.

TIPS : Start with a small learning rate (e.g., 0.01) and gradually increase it. If the cost decreases smoothly, it’s a good rate. If it fluctuates or diverges, reduce the learning rate. A learning rate that’s too large can cause gradient descent to overshoot, never reach the minimum, and fail to converge.

  1. Iterate : Repeat the process until the changes in the cost function are minimal or a specified number of iterations is reached.

Python Implementation of Gradient Descent for Simple Linear Regression

Python Implementation of Gradient Descent for Simple Linear Regression

Gradient Descent for Multiple Linear Regression

Gradient Descent Steps for Multiple Linear Regression

  1. Initialization Begin with random values for each parameter, including the intercept b and the weights w for each feature.

initialization gradient descent for multiple linear regression

  1. Calculate the Gradients Compute the gradient of the cost function with respect to the model parameters.

calculate the gradients in gradient descent for multiple linear regression

calculate the gradients in gradient descent for multiple linear regression

Vector Form

calculate the gradients in gradient descent for multiple linear regression

calculate the gradients in gradient descent for multiple linear regression

Where :

  • x_subscript_j_superscript_i is the j_th feature of the i_th training example
  • x_superscript_T is the transpose of vector x
  1. Update Parameters Adjust the model parameters in the opposite direction of the gradient to reduce the error. The update rule is given by:

update parameters in gradient descent for multiple linear regression

  1. Iterate Repeat the process until the changes in the cost function are minimal or a specified number of iterations is reached.

Python Implementation of Gradient Descent for Simple Linear Regression

Python Implementation of Gradient Descent for Simple Linear Regression


Conclusion

Congratulations!! 💫 In this post, we’ve explored the fundamentals of linear regression and multiple linear regression, walked through the process of implementing gradient descent, and discussed key techniques like feature scaling to optimize model performance. By understanding how to initialize model parameters, compute gradients, and iteratively update weights, you’re now well-equipped to implement linear regression algorithms and boost their performance on real-world datasets.

Whether you’re working with simple linear regression or navigating the complexities of multiple features, mastering gradient descent and grasping its core principles will significantly enhance your ability to develop accurate and efficient machine learning models. Keep experimenting, refining your skills, and embracing the learning process — it’s just as important as the results themselves!

Stay tuned for more insights into machine learning techniques and web development topics. Happy learning as you continue exploring and building smarter models! 💫💫

Let's connect on LinkedIn 🚀

"This article was originally posted on Medium, where I share more insights on data analysis, machine learning, and programming. Feel free to check it out and follow me there for more content!"

Please Like, Share and Follow 🙏.

Feel free to ask any questions in the comments section—I'll respond promptly and thoroughly to your inquiries. Your doubts are warmly welcomed and will receive swift and comprehensive replies. ❤️

Top comments (0)