Skip to main content

Logistic Regression Introduction

Logistic regression is a popular machine learning algorithm used for binary classification tasks. Despite its name, logistic regression is not a regression algorithm but a classification algorithm that models the probability of a binary outcome (such as 0 or 1, true or false, spam or not spam). It is a widely-used algorithm due to its simplicity, interpretability, and solid performance on many types of classification problems.

In this article, we will cover:

  • What logistic regression is and how it works.
  • The key assumptions behind logistic regression.
  • The mathematical foundation of the algorithm.
  • Common use cases and applications.
  • Key advantages and limitations.

1. What is Logistic Regression?​

At its core, logistic regression is used to model the probability of a binary outcome, typically coded as 0 or 1. Unlike linear regression, which predicts a continuous value, logistic regression predicts a probability that a given input belongs to a particular class.

The algorithm makes use of the logistic (or sigmoid) function to ensure that the predicted values fall between 0 and 1, which can be interpreted as probabilities.

Key Formula:​

The key equation for logistic regression is:

P(y=1∣X)=11+e−(β0+β1x1+β2x2+⋯+βnxn)P(y=1 | X) = \frac{1}{1 + e^{-(\beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_n x_n)}}

Where:

  • P(y=1∣X)P(y=1|X) is the probability that the output yy is 1 given the input features XX.
  • β0\beta_0 is the intercept (bias term).
  • β1,β2,…,βn\beta_1, \beta_2, \dots, \beta_n are the coefficients (weights) associated with the input features x1,x2,…,xnx_1, x_2, \dots, x_n.
  • ee is Euler’s number (the base of the natural logarithm).

The logistic function outputs a value between 0 and 1, interpreted as the fitted probability of the positive class under the model. A threshold converts that score into an action. A threshold of 0.5 is a convention, not a universal optimum:

  • If P(y=1∣X)≥0.5P(y=1|X) \geq 0.5, the output is predicted as class 1.
  • If P(y=1∣X)<0.5P(y=1|X) < 0.5, the output is predicted as class 0.

2. Key Assumptions of Logistic Regression​

Logistic regression relies on several assumptions to provide reliable and interpretable results. It’s important to ensure that these assumptions are met, or the model may underperform.

1. Outcome Model:​

  • The basic derivation covers a binary outcome. Multinomial logistic regression directly models multiple classes, while one-vs-rest fits several binary problems. These are established logistic-regression variants, not merely a post-processing trick.

2. Linear Relationship Between Features and Log-Odds:​

  • Logistic regression assumes that the log-odds of the dependent variable are linearly related to the independent variables. This means that the model predicts the log-odds of the outcome, not the raw outcome itself.

The log-odds are expressed as:

log-odds(p)=ln⁡(p1−p)\text{log-odds}(p) = \ln \left( \frac{p}{1-p} \right)

This is the logarithm of the ratio of the probability of the event occurring to the probability of it not occurring. The log-odds are then modeled as a linear function of the input features.

3. Independence of Errors:​

  • Logistic regression assumes that the errors (residuals) are independent of each other, meaning the observations are not correlated with each other. This assumption is particularly important in time-series or sequential data where correlation between observations is common.

4. No Multicollinearity:​

  • The independent variables should not be too highly correlated with each other (multicollinearity). High multicollinearity can make it difficult to estimate the coefficients accurately and affect model interpretability.

5. Sufficiently Large Sample Size:​

  • Logistic regression performs well with a sufficiently large sample size. Small datasets can lead to overfitting, where the model captures noise rather than meaningful patterns in the data.

3. Mathematical Foundation of Logistic Regression​

3.1. The Sigmoid Function​

The core of logistic regression is the sigmoid function, also known as the logistic function, which maps any real-valued number into the range [0, 1]. This makes it suitable for modeling probabilities.

The sigmoid function is defined as:

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

Where ( z ) is the linear combination of input features:

z=β0+β1x1+β2x2+⋯+βnxnz = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_n x_n

The output of the sigmoid function is a value between 0 and 1, which can be interpreted as the probability that the given input belongs to class 1.

3.2. Log-Likelihood and Maximum Likelihood Estimation​

Logistic regression uses maximum likelihood estimation (MLE) to estimate the model parameters (coefficients). The goal is to find the parameter values that maximize the likelihood of the observed data.

The log-likelihood function for logistic regression is given by:

ℓ(β)=∑i=1n[yiln⁡(P(yi=1∣Xi))+(1−yi)ln⁡(1−P(yi=1∣Xi))]\ell(\beta) = \sum_{i=1}^{n} \left[ y_i \ln(P(y_i=1 | X_i)) + (1 - y_i) \ln(1 - P(y_i=1 | X_i)) \right]

This function measures how well the model’s predicted probabilities align with the actual outcomes, and the MLE process aims to maximize this likelihood by adjusting the model’s coefficients.

3.3. Gradient Descent​

Since there is no closed-form solution for the coefficients in logistic regression, we typically use gradient descent or a variant like stochastic gradient descent (SGD) to optimize the log-likelihood function. The algorithm iteratively updates the coefficients in the direction that increases the likelihood until convergence.


4. Common Use Cases of Logistic Regression​

Logistic regression is highly versatile and used in a wide range of fields where binary classification is required. Some common use cases include:

4.1. Spam Detection:​

Logistic regression is widely used to classify whether an email is spam or not spam based on features such as email content, sender, or subject line.

4.2. Customer Churn Prediction:​

Businesses use logistic regression to predict whether a customer is likely to leave based on usage patterns, demographics, and other factors.

4.3. Credit Scoring:​

Financial institutions use logistic regression to predict whether a loan applicant will default on a loan, based on factors such as credit history, income, and employment status.

4.4. Medical Diagnosis:​

Logistic regression is used in healthcare to predict whether a patient has a particular condition based on medical test results, symptoms, and other factors.

4.5. Marketing Campaigns:​

Logistic regression helps marketers predict whether a customer will respond to a particular campaign based on historical behavior and demographics.


5. Advantages of Logistic Regression​

1. Simplicity and Interpretability:​

Logistic regression is easy to understand and interpret, especially because the output can be interpreted as probabilities. The coefficients indicate how each feature influences the probability of the outcome.

2. Clear Preprocessing Semantics:​

The decision rule is well defined in original units, but scaling is strongly recommended for many numerical solvers and makes regularization act more comparably across features. Fit the scaler on training data only.

3. Efficient for Large Datasets:​

Logistic regression is computationally efficient and can handle large datasets with many observations. It performs well when the data is linearly separable and the decision boundary is simple.

4. Can Handle Nonlinear Relationships (with Extensions):​

Although logistic regression is linear by default, it can handle nonlinear relationships by incorporating interaction terms or polynomial features.

5. Well-Suited for Probabilistic Outputs:​

Logistic regression provides probabilistic predictions, which are useful in cases where you want to know the confidence of the model's predictions (e.g., risk scoring).


6. Limitations of Logistic Regression​

1. Linear Decision Boundary:​

Logistic regression assumes that the decision boundary between the two classes is linear. This makes it unsuitable for problems with complex, nonlinear relationships unless you engineer new features (e.g., polynomial features) to introduce nonlinearity.

2. Sensitive to Outliers:​

Logistic regression is sensitive to outliers. A single large outlier can have a disproportionate influence on the model’s decision boundary.

3. Class Imbalance Changes Evaluation and Decisions:​

Logistic regression can rank rare events well, but accuracy and a 0.5 threshold may be inappropriate. Use stratified or structure-aware validation, class-relevant metrics, calibrated probabilities when needed, and a threshold chosen from costs. Class weighting changes the fitted objective and can change calibration.

4. Correlated Features Complicate Interpretation:​

Logistic regression does not assume feature independence. Strong collinearity can make individual coefficient estimates unstable; regularization may improve prediction but does not turn coefficients into causal effects.


Summary​

Logistic regression is a fundamental algorithm in the machine learning toolkit, especially useful for binary classification problems. It is known for its simplicity, efficiency, and interpretability, making it a go-to choice in many real-world applications. Despite its limitations in handling complex, nonlinear relationships, it remains a powerful and popular model in scenarios where the data is linearly separable and easily interpretable.

In the next section, we will dive deeper into the mathematical theory behind logistic regression, covering the derivation of the cost function, optimization techniques, and regularization methods like Lasso and Ridge to improve the model's robustness and generalization ability.