Logistic Regression
Normal Equation
Machine Learning
Data Science
Algorithm

Can we use Normal Equation for Logistic Regression ?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Logistic regression is a commonly used statistical method for binary classification problems. It models the probability that a given input belongs to a certain category. One central aspect of logistic regression is its parameter estimation, which is often accomplished through methods such as Gradient Descent. However, people frequently ask whether the Normal Equation, a closed-form solution used in Linear Regression, can also be used for Logistic Regression. Here's an in-depth analysis of why the Normal Equation isn't generally suitable for Logistic Regression and how it contrasts with other methods.

Understanding Logistic Regression

Logistic Regression is designed for situations where the dependent variable is binary. It uses the logistic function to estimate probabilities:

h_θ(x)=11+eθTxh\_{\theta}(x) = \frac{1}{1 + e^{-\theta^T x}}

In the above equation, `h` is the hypothesis function, `x` is the input feature vector, and `\theta` is the parameter vector. Unlike linear regression, the output is not a linear function but rather a probability ranging between 0 and 1.

Why the Normal Equation Isn't Suitable

The Normal Equation is a method used to find the optimal parameters for linear regression without iteration:

θ=(XTX)1XTy\theta = (X^T X)^{-1}X^T y

Despite its effectiveness in linear regression, the Normal Equation is not generally applicable to logistic regression for several reasons:

  1. Non-linear Hypothesis: The logistic function is inherently non-linear, so the Normal Equation, which assumes a linear hypothesis, does not work directly.
  2. Non-convex Cost Function: The cost function for logistic regression is non-convex, which complicates finding a closed-form solution. The cost function is derived from the likelihood of the sigmoid output and is given by:
    J(θ)=1m_i=1m[y(i)log(h_θ(x(i)))+(1y(i))log(1h_θ(x(i)))]J(\theta) = -\frac{1}{m} \sum\_{i=1}^{m} \left[y^{(i)} \log(h\_{\theta}(x^{(i)})) + (1-y^{(i)}) \log(1-h\_{\theta}(x^{(i)}))\right]
    Unlike the Mean Squared Error (MSE) used in linear regression, this function does not lend itself to easy minimization via a closed-form solution.
  3. Invertibility Concerns: The matrix XTXX^T X must be invertible for the Normal Equation to be applicable. In practical scenarios, especially when features are correlated, this matrix might not be invertible, which restricts the use of the Normal Equation even further.

Better Alternatives

  1. Gradient Descent: The most widely used algorithm for optimizing logistic regression. It iteratively adjusts the parameters to minimize the cost function, adapting well to a variety of cost surfaces.
  2. Stochastic Gradient Descent (SGD): An efficient variant of Gradient Descent that is particularly useful for large datasets. Instead of computing the gradient from the entire dataset, SGD updates parameters incrementally with each data point.
  3. L-BFGS: A numerical optimization algorithm that approximates the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. It is particularly good for optimization problems with a large number of parameters.

Examples

Let's look at a simple example to underscore these points. Consider a dataset with two features aiming to predict a binary output:

2 | 3 | 0 1 | 2 | 0 4 | 5 | 1 6 | 8 | 1


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.