Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 26

Demystifying Logistic Regression

Recommended resources

Welcome to the next module in your machine learning journey! In our previous lessons, we thoroughly explored linear regression and its regularized variants—Ridge, Lasso, and Elastic Net—mastering how to predict continuous numerical values. We concluded by establishing a clear framework for choosing the right regression model based on the data and the problem.

Now, we pivot from regression to classification, one of the most common tasks in machine learning. Instead of predicting how much, we will now predict which one. Our first algorithm in this new domain is Logistic Regression. While its name includes "regression," it is fundamentally a classification algorithm. This lesson will build the core intuition behind how logistic regression works by explaining its three essential components: the sigmoid function, the concept of log-odds, and the decision boundary.

From Linear to Logistic: The Need for a New Approach

Let's start with a simple question: why can't we just use the linear regression models we've already mastered for a classification task?

Imagine a binary classification problem where we want to predict whether a tumor is malignant (1) or benign (0) based on its size. If we try to fit a standard linear regression line to this data, we immediately run into a problem.

On the left, a linear regression line attempts to fit binary data, producing predictions below 0 and above 1. On the right, a logistic regression S-curve correctly constrains its output between 0 and 1, which can be interpreted as a probability.

As you can see, the linear model's predictions can extend far beyond the logical bounds of 0 and 1. What does a prediction of 1.5 or -0.2 mean when our labels are only 0 or 1? This output is not easily interpretable as a probability. We need a way to take the output of a linear equation and "squash" it into the [0, 1] range. This is where the sigmoid function comes in.


The Sigmoid Function: Mapping Output to Probability

The core innovation of logistic regression is the use of the sigmoid function (also called the logistic function) to transform the output. The sigmoid function takes any real-valued number and maps it to a value between 0 and 1, making it perfect for modeling probability.

Its mathematical form is:

Here, is simply the output of the familiar linear equation: , where are the model weights and is the bias term.

The video below from the StatQuest channel provides a wonderful high-level explanation of how this works in practice.

StatQuest: Logistic Regression

This video introduces logistic regression, contrasting it with linear regression and explaining the role of the S-shaped logistic curve.

Watch the following segments to build your intuition: Linear vs. Logistic: This section quickly recaps linear regression and introduces logistic regression for predicting true/false outcomes. The S-Shaped Curve: From this point, Josh Starmer explains how the sigmoid curve maps an input (like mouse weight) to a probability between 0 and 1. Classification Threshold: Finally, watch this short clip to see how these probabilities are used to make a final classification decision, typically using a 0.5 (or 50%) threshold.

The sigmoid function elegantly solves our problem. No matter how large or small the output of our linear equation is, the resulting probability will always be between 0 and 1.


Log-Odds: The "Linear" Core of Logistic Regression

We've established that logistic regression uses a linear model inside a sigmoid function to output a probability. But what is that inner linear model actually representing? It's not modeling the probability directly. Instead, it models the log-odds of the event occurring.

This is often the most confusing part of logistic regression, so let's break it down carefully. The StatQuest channel has another excellent video that clarifies the concepts of odds and log-odds.

Odds and Log(Odds), Clearly Explained!!!

This video provides a clear, step-by-step explanation of odds and log-odds, which are central to understanding what logistic regression models.

Please watch the following segments: What are Odds? This part defines odds as the ratio of an event happening to it not happening, distinguishing it clearly from probability. Odds from Probability: Here, you'll see the direct mathematical relationship: \text{Odds} = \frac{p}{1-p}, where p is the probability. The Log of the Odds (Logit): From this point, the video explains why taking the natural logarithm of the odds is useful. It transforms the skewed range of odds (0 to \infty) into a symmetric range (-\infty to \infty), which is much better suited for linear modeling. The Logit Function: Pay close attention to the final summary here, where it is explicitly stated that the log of the ratio of probabilities is called the logit function and forms the basis for logistic regression.

As you just saw, the logit function is . This is what the linear model at the heart of logistic regression predicts.

So, the full relationship is:

This equation reveals why logistic regression is a member of the family of "linear classifiers." While the relationship between the features and the probability is non-linear (due to the sigmoid curve), the relationship between the features and the log-odds of the outcome is linear. By solving for in the equation above, we arrive back at our sigmoid function.

For a more detailed look at the mathematics connecting these concepts, the following resource is very helpful.

Logistic Regression in Machine Learning

This article from GeeksForGeeks clearly lays out the components of logistic regression, including the sigmoid function and the log-odds equation.

First, read the section on the Sigmoid Function to reinforce what you've just seen. Next, review the derivation in the section How does it work?. This shows how the linear function z is passed into the sigmoid function \sigma(z). Now, focus on the section Logistic Regression Equation and Odds. This is the crucial part that derives the sigmoid probability formula starting from the assumption that the log-odds are linear. Finally, scan the Terminologies section to solidify your understanding of terms like Odds, Log-Odds (Logit), and Coefficient.


The Decision Boundary: From Probability to Classification

We now have a model that outputs a probability. To make a final classification (e.g., "0" or "1"), we must define a decision boundary. This is typically done by setting a probability threshold, most commonly 0.5.

  • If , we predict class 1.
  • If , we predict class 0.

Let's think about what a probability of 0.5 means in the context of our model.

  • If , then the odds are .
  • The log-odds are .

This means the decision boundary corresponds to the exact point where our linear model's output is zero:

This equation defines a line in 2D, a plane in 3D, or a hyperplane in higher dimensions. This hyperplane is the decision boundary that linearly separates the two classes in the feature space. Any point on one side of the hyperplane is classified as 1, and any point on the other side is classified as 0.

This image beautifully illustrates the entire process:

This graph shows the three key steps: (1) The x-axis represents the log-odds, which is the output of the linear model. (2) This value is transformed by the S-shaped sigmoid function to produce a probability on the y-axis. (3) A decision boundary (here at log-odds = 0) is used to classify the outcome based on the resulting probability.

The Real Python article provides excellent visualizations for both one-dimensional and two-dimensional cases, clearly showing this linear separation.

Logistic Regression in Python

This article provides clear visualizations of the decision boundary in action.

First, look at the figure in the section on Single-Variate Logistic Regression. Notice how the dashed line (the logit/linear function) crosses zero at a specific x-value. This point corresponds to where the solid sigmoid curve crosses the 0.5 probability mark, defining the decision boundary. Next, examine the figure in the section on Multi-Variate Logistic Regression. Here, the decision boundary is a straight line that separates the two classes (white and green circles) in the 2D feature space. This line represents all points where the linear function equals zero.

One final note: how does the model find the best weights () and bias ()? In linear regression, we used Ordinary Least Squares to minimize the sum of squared residuals. In logistic regression, this approach doesn't work. Instead, the model is fit using a method called Maximum Likelihood Estimation (MLE). You don't need to know the derivation, but the intuition is that MLE finds the parameters that maximize the probability of observing the actual data you have.


Conclusion

In this lesson, you have built the foundational intuition for logistic regression. We have moved from the world of continuous prediction to binary classification by understanding how to adapt a linear model for this new task.

Key Takeaways:

  • Logistic regression is a classification algorithm that models the probability of a binary outcome.
  • It uses the sigmoid function to "squash" the output of a linear equation into the range [0, 1].
  • The underlying linear model does not predict the probability directly but rather the log-odds (logit) of the event. The relationship between features and log-odds is linear.
  • A decision boundary, typically at a probability of 0.5 (which corresponds to log-odds of 0), is used to make the final classification. This boundary is a linear hyperplane in the feature space.

You now understand the "how" and "why" of logistic regression's mechanics. In our next lesson, we will discuss the practical side: when is it an appropriate algorithm to choose? We will then begin our deep dive into the essential metrics used to evaluate classification models, starting with the confusion matrix.