Skip to main content

Welcome to a Grasp lesson

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

Lesson 3

Understanding Logistic Regression: Probability and Decision Boundaries

Recommended resources

Welcome to the next lesson in our review of machine learning fundamentals. In our previous discussion, we focused on regression and how to control model complexity using L1 and L2 regularization. Now, we'll pivot from predicting continuous values to one of the most common tasks in machine learning: classification.

Today, we'll dissect logistic regression, a foundational algorithm for binary classification. While its name includes "regression," you'll see it's purely a classification method. For your interview, being able to clearly articulate how it works is essential. This lesson will equip you to describe exactly how logistic regression models the probability of an outcome and how that probability translates into a decision boundary that separates classes.

From Linear Regression to Classification

Let's start by considering why we can't just use the linear regression models we've been discussing for a classification task, like predicting whether a financial transaction is fraudulent (1) or not (0).

A linear model, , outputs a value that can range from to . This is problematic for two reasons:

  1. The output doesn't conform to a probability, which must lie between 0 and 1.
  2. In a binary {0, 1} setting, an output of, say, 3 or -0.5 has no intuitive meaning.

This is the core problem logistic regression is designed to solve. It elegantly adapts the linear model framework to produce a valid probability. The video below provides a concise explanation of this transition.

Logistic Regression - THE MATH YOU SHOULD KNOW!

This video from CodeEmporium starts by clearly explaining the limitations of using linear regression for classification tasks.

Watch the segment from the beginning up to the introduction of logistic regression. Pay attention to the arguments against using linear models for both binary and multi-class problems.


Modeling Probability with the Sigmoid Function

The solution is to take the output of the linear equation, , and pass it through a function that "squashes" the entire real number line into the (0, 1) interval. This function is the sigmoid function, also called the logistic function.

The sigmoid function is defined as:

Here, is the output of our linear model, . The result, , is a number between 0 and 1, which we can interpret as the probability that the given input belongs to the positive class (y=1).

So, the hypothesis of our logistic regression model is:

The video below by Emma Ding provides an exceptionally clear, two-step summary of this process, which is a great framework for an interview explanation.

From Scratch: How to Code Logistic Regression in Python for Machine Learning Interviews!

This video is tailored for interview preparation and breaks down logistic regression into two simple steps.

Watch the initial explanation from the introduction through the two main steps. This will give you a strong high-level narrative. Then, watch the segment detailing the equations to connect this narrative to the math.

To solidify these concepts, let's turn to a more detailed textual resource.

GitHub - Devinterview-io/logistic-regression-interview-questions: 🟣 Logistic Regression interview questions and answers to help you prepare for your next machine learning and data science interview in 2026. · GitHub

This document, formatted as a series of interview questions, provides concise and accurate definitions of the core components of logistic regression.

First, read the section on logistic regression to get an overview. Then, read the section on the sigmoid function, "What is the sigmoid function", focusing on its mathematical representation and its role in converting the linear output into a probability.


From Probability to a Decision Boundary

Now that our model outputs a probability, how do we make a final classification? We apply a decision rule. The most common rule is to set a threshold at 0.5:

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

This simple threshold has a profound geometric meaning. Let's look at the sigmoid function's graph. You'll notice that whenever its input . Since our input is , our decision rule becomes:

  • Predict 1 if .
  • Predict 0 if .

The equation defines the decision boundary. This is the surface (a line in 2D, a plane in 3D, a hyperplane in higher dimensions) that separates the feature space into two regions: the "predict 1" region and the "predict 0" region. It is a linear boundary because the equation defining it is linear in the features .

The image below, from a lecture by Andrew Ng, visualizes this exact concept.

This diagram shows how the sigmoid function \(g(z)\) maps the linear output \(z = \theta^T x\) to a probability. The decision rule "predict y=1 if \(h_\theta(x) \ge 0.5\)" directly corresponds to the condition \(\theta^T x \ge 0\), which defines the decision boundary.

The following reading provides a much more detailed exploration with visualizations, showing how this boundary manifests in one and two dimensions.

Principles and Techniques of Data Science - 19  Logistic Regression II

These course notes from Berkeley's Data 100 course give a thorough walkthrough of how decision rules lead to decision boundaries, complete with Python code and plots.

Please read the section 19.1 Decision Boundaries. Focus on understanding how a threshold applied to the model's probability output translates into a boundary in the feature space. Pay attention to the visualizations for both the 1-feature and 2-feature cases.

When a logistic regression model is trained on a dataset with multiple features, this boundary separates the data points. Points on one side are classified as belonging to one class, and points on the other side to the other class.

An example of a linear decision boundary produced by a logistic regression model on a 2D dataset. The model has learned a line that separates the feature space into a "Class 0" region (yellow) and a "Class 1" region (purple). The color gradient shows how the predicted probability changes as you move away from the boundary.

A Note on Model Training and Regularization

How does the model find the right parameters () to place this boundary correctly? It's done through an optimization process called Maximum Likelihood Estimation, which aims to find the values that maximize the probability of observing the true labels in the training data. This is typically solved using iterative methods like Gradient Descent.

Importantly, just like linear regression, logistic regression can overfit. In our last lesson, we saw how regularization penalizes large coefficients. The same principle applies here. In fact, regularization is critical for logistic regression. If the data is perfectly separable by a line, the "optimal" coefficients can grow towards infinity to make the sigmoid curve infinitely steep, leading to an overconfident model. By adding an L1 or L2 penalty, we constrain the size of the coefficients, ensuring a more robust and generalizable decision boundary. Most standard implementations, like scikit-learn's LogisticRegression, apply L2 regularization by default.


Conclusion

In this lesson, we have deconstructed logistic regression, a cornerstone of classification. You are now prepared to explain its mechanics with the precision required for a technical interview.

Key Takeaways for Your Interview:

  • Purpose: Logistic regression is a classification algorithm that models the probability of a binary outcome.
  • Modeling Probability: It uses the sigmoid (or logistic) function to map the output of a linear model () to a probability between 0 and 1. This probability is .
  • Decision Boundary: By applying a threshold (typically 0.5) to the output probability, a decision rule is created. This rule is equivalent to checking the sign of , and the equation defines a linear decision boundary in the feature space.
  • Linearity: It's important to remember that while logistic regression models a non-linear probability function, the decision boundary it produces is linear.

In our next lesson, we will explore a completely different approach to classification: decision trees. Unlike the linear models we've studied, decision trees create non-linear decision boundaries by recursively partitioning the feature space, offering a powerful alternative with different strengths and weaknesses.