Overview

8 Yes, no, or maybe so? Logistic regression and classification

This chapter explains classification as the task of assigning labels rather than predicting a continuous number, such as deciding whether something is spam or not spam, or whether an event happened or did not happen. Because binary outcomes do not fit linear regression well, logistic regression is introduced as a better tool for classification problems. Using a toddler meltdown example, the chapter shows how logistic regression models the probability of a yes-or-no outcome as the time without a nap increases, producing an S-shaped curve that stays between 0 and 1. A probability threshold, often 0.5, then converts that estimated probability into a final class prediction.

The chapter then shows how logistic regression is fit in Python with scikit-learn, including how to reshape input data, fit the model, extract coefficients, and predict probabilities for new cases. It also explains the meaning of the log-odds: the linear part inside the logistic function is equivalent to the natural log of the odds, which makes it easier to interpret changes in risk and compare one situation to another. In the toddler example, moving from four to six hours without a nap dramatically increases the odds of a meltdown, illustrating how the slope of the model translates into changing likelihoods. The chapter emphasizes that logistic regression is useful not because it predicts exact outcomes with certainty, but because it gives a principled estimate of class probabilities.

To judge whether a classification model is any good, the chapter shifts from fitting to validation. It recommends train/test splits and cross-validation to estimate how well a model generalizes to unseen data, rather than relying on how well it fits the training set. It then argues that accuracy can be misleading, especially when classes are imbalanced, and uses a wolf-alarm example to show why false positives and false negatives matter more than a single percent-correct number. Precision and recall are introduced as better measures for classification, along with the tradeoff between them. Finally, the chapter explains how ROC curves, AUC, and precision-recall curves summarize performance across thresholds, helping you choose an operating point that fits the real costs of missed events versus false alarms.

Data showing time without a nap and whether a toddler had a meltdown
A logistic regression model of time without a nap and whether a toddler had a meltdown
Finding the predicted probability of a toddler meltdown, given that it’s been 4.78 hours without a nap
Plotting the logistic function in Python using matplotlib
A logistic regression model predicting the probability of a toddler meltdown based on hours without a nap
Comparing the logistic function (the S-curve) with the log-odds function (the straight line). Note that when the log-odds line crosses y = 0, the sigmoid function has a value of y = 0.5.
A logistic regression of 100 samples for hours without a nap and the probability of a meltdown
The receiver operating characteristic (ROC) curve for my logistic regression toddler meltdown prediction, across different thresholds
The precision-recall curve (PRC) for our toddler meltdown dataset

Summary

  • Classification models produce outputs that differ from regression models, because they output classes (true/false or dog/cat/bird) rather than continuous values.
  • While logistic regression produces a probability, it can be turned into a classification model using a threshold like 0.50.
  • Accuracy is a misleading metric in classification, especially for imbalanced datasets, where one class is overrepresented relative to another (e.g., far more non-spam than spam emails).
  • Precision and recall are effective measures that help nudge a preference for false positives (false alarms) versus false negatives (missed alarms).
  • In the machine learning workflow, we use train/test splits or cross-validation to hold back a dataset that the model was not trained on and then evaluate how well it performs on data it has not seen before. Unless fitting your model is computationally expensive, choose cross-validation.
  • Use the receiver operating characteristic (ROC) curve to measure model performance across different thresholds, but if the data is imbalanced, use the precision-recall curve (PRC) instead.
  • For the ROC or PRC, use the area under the curve (AUC) to summarize performance as a single number, rather than relying solely on accuracy, which can be misleading.

FAQ

What is the main difference between classification and regression?Classification predicts a category or label, such as spam/not spam or dog/cat/bird, while regression predicts a continuous numeric value, such as temperature. Logistic regression is used for classification, not for predicting continuous outcomes.
Why is linear regression not a good fit for binary classification problems?Binary classification outputs only two values, usually 0 and 1. A straight line does not naturally model probabilities between 0 and 1, so linear regression is awkward and unhelpful for this kind of problem. Logistic regression uses an S-shaped sigmoid curve instead.
What does logistic regression actually predict?Logistic regression predicts the probability that an event belongs to a class, usually the positive class. For example, it can estimate the probability of a toddler meltdown given the number of hours without a nap.
What is a sigmoid function in logistic regression?A sigmoid function, also called a logistic function, is the S-shaped curve used by logistic regression. Its output always stays between 0 and 1, which makes it ideal for representing probabilities.
How do you turn a logistic regression probability into a yes-or-no prediction?You choose a threshold, often 0.5 by default. If the predicted probability is below the threshold, the prediction is class 0; if it is at or above the threshold, the prediction is class 1.
What do the coefficients in logistic regression represent?The coefficients are the intercept and slope of the linear part inside the logistic function’s exponent. These values determine the shape of the S-curve and are usually found using maximum likelihood estimation.
Why are log-odds important in logistic regression?Log-odds convert probabilities into a linear scale that is easier to analyze. In logistic regression, the linear expression inside the model corresponds to the log-odds, making it easier to interpret how the input variable changes the chance of the event.
How do train/test splits help validate a classification model?A train/test split uses part of the data to train the model and holds out the rest to test how well the model performs on unseen data. This helps estimate whether the model generalizes beyond the data it was fit on.
Why can accuracy be misleading in classification problems?Accuracy can look high even when a model is bad at detecting the event of interest, especially when classes are imbalanced. For example, if wolves are rare, a model that always predicts “no wolf” can still achieve high accuracy but miss every real wolf.
When should you use precision, recall, ROC, or PRC?Use precision and recall when you want to understand false positives and false negatives, especially in imbalanced datasets. Use the ROC curve and AUC to summarize performance across thresholds when classes are reasonably balanced. For severely imbalanced data, the precision-recall curve is often more informative.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Statistics ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Statistics ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Statistics ebook for free