In our previous lessons, we've developed a robust understanding of logistic regression for binary classification, culminating in a practical exercise where you trained and evaluated a model to distinguish between two outcomes. But what happens when a problem has three, ten, or even a hundred possible categories? How do we adapt our binary classifier for these more complex scenarios?
This lesson directly addresses that question. We will explore the primary strategies for extending logistic regression to handle multi-class classification problems. You will learn about the clever "divide and conquer" approach of One-vs-Rest (OvR) and the more integrated, generalized model known as Softmax (or Multinomial) Regression. Understanding these extensions is crucial for applying logistic regression to a much wider range of real-world problems, from image classification to document categorization.
The Challenge: Beyond Two Classes
The binary logistic regression model you've worked with uses the sigmoid function to output a single probability, . The probability of the other class is simply . This works perfectly when there are only two options.
However, if we have classes (e.g., classifying news articles into 'Sports', 'Politics', and 'Technology'), we need a model that can produce a probability for each of the classes, with the condition that these probabilities sum to 1. Let's explore two ways to achieve this.
Strategy 1: One-vs-Rest (OvR)
The One-vs-Rest (OvR) strategy, also known as One-vs-All (OvA), is an intuitive and popular method. Instead of building one complex model, it breaks the multi-class problem down into multiple binary classification problems.
For a problem with classes, OvR trains separate binary classifiers. Each classifier is trained to distinguish one class (the "one") from all the other classes combined (the "rest").
For example, if we are classifying iris flowers into three species (Setosa, Versicolor, Virginica), OvR would train:
- Classifier 1: Predicts "Setosa" vs. "Not Setosa" (i.e., Versicolor or Virginica).
- Classifier 2: Predicts "Versicolor" vs. "Not Versicolor" (i.e., Setosa or Virginica).
- Classifier 3: Predicts "Virginica" vs. "Not Virginica" (i.e., Setosa or Versicolor).
When a new, unseen flower needs to be classified, it is passed through all three classifiers. Each classifier outputs a probability (or a confidence score). The class corresponding to the classifier that outputs the highest score is chosen as the final prediction.
The article "One-vs-Rest and One-vs-One for Multi-Class Classification" from Machine Learning Mastery provides a clear explanation of this approach.
One-vs-Rest and One-vs-One for Multi-Class Classification
This resource explains the core idea behind the One-vs-Rest (OvR) strategy and shows how to implement it in scikit-learn.
Please read the first part of the article, from the beginning of the OvR section down to just before the heading "One-Vs-One for Multi-Class Classification." Pay attention to the conceptual breakdown and the two ways scikit-learn supports OvR: through the multi_class='ovr' argument and the OneVsRestClassifier wrapper class.
A key advantage of OvR is its modularity; it can use any binary classifier as its building block. However, a potential downside is that each binary classifier is trained on a potentially imbalanced dataset (e.g., one class vs. nine other classes).
The image below shows the decision boundaries created by a One-vs-Rest logistic regression model. You can see how three binary boundaries combine to partition the feature space into three distinct regions.

Strategy 2: Softmax (Multinomial) Regression
While OvR is effective, a more direct and mathematically elegant approach is to generalize logistic regression itself. This generalization is called Softmax Regression or Multinomial Logistic Regression. Instead of training multiple independent models, softmax regression is a single model that is trained to handle all classes simultaneously.
The core idea is to compute a "score" for each class and then convert those scores into probabilities using the softmax function.
Let's break down the intuition. For a given input , the model first calculates a linear score for each class :
Here, is a unique weight vector for class . These scores can be any real number, so they aren't probabilities yet. To turn them into a valid probability distribution, we need to ensure they are all positive and sum to 1. The softmax function does this in two steps:
- Exponentiate: It takes the exponential of each score () to make them all positive.
- Normalize: It divides each exponentiated score by the sum of all exponentiated scores.
This results in the probability of class :
This function beautifully ensures that each probability is between 0 and 1, and that all probabilities sum to 1. The following video provides an excellent step-by-step construction of this idea.
Logistic regression 5.2: Multiclass - Softmax regression
This video from Herman Kamper's lecture series introduces softmax regression as a principled extension of binary logistic regression.
Please watch from the beginning of the video. The presenter does a fantastic job of motivating the need for a multi-class model and then building the softmax function from the ground up: starting with linear scores, then exponentiating for positivity, and finally normalizing to get probabilities.
Softmax as a Generalization of Sigmoid
One of the most satisfying insights is that softmax regression is a true generalization of the binary logistic regression we already know. If you set the number of classes to 2, the softmax function mathematically simplifies to become identical to the sigmoid function.
This demonstrates a clear evolutionary path: the sigmoid function is simply a special case of the softmax function for two classes. The following clip from the same video explains this connection.
Logistic regression 5.2: Multiclass - Softmax regression
This short segment demonstrates how softmax regression simplifies to binary logistic regression when K=2.
Please watch the final section of the video, "Relationship to Binary Logistic Regression." This will solidify your understanding of how these two concepts are deeply connected.
Comparing OvR and Softmax
So, which strategy should you use?
- One-vs-Rest (OvR) trains independent binary classifiers. Because the classifiers are trained independently, their output probabilities are not guaranteed to sum to 1, and a heuristic (like picking the max score) is needed.
- Softmax Regression trains a single, unified model where the parameters for all classes are learned jointly. This typically leads to better performance because the model learns to discriminate between all classes at once, and it outputs a true probability distribution.
The image below, from the scikit-learn documentation, provides a compelling visual comparison. For this particular dataset, the multinomial (softmax) model achieves slightly higher accuracy and creates decision boundaries that seem to separate the classes more cleanly than the OvR approach.

Implementation in scikit-learn
Fortunately, scikit-learn makes it very easy to use either strategy. The LogisticRegression class you're already familiar with has a multi_class parameter:
LogisticRegression(multi_class='ovr')implements the One-vs-Rest strategy.LogisticRegression(multi_class='multinomial')implements softmax regression.LogisticRegression(multi_class='auto')is the default. It will generally choose'multinomial'if the selected solver supports it (which most do), and'ovr'otherwise.
The following resource provides a complete walkthrough of using softmax regression in practice.
Deep Dive into Softmax Regression
This article from Towards Data Science shows how to apply softmax regression to the famous MNIST handwritten digit classification task.
Please read the sections "Softmax Regression in Scikit-Learn" and "Example: Classifying Handwritten Digits." This will demonstrate the practical workflow: loading data for a 10-class problem, training the LogisticRegression model (which will use softmax by default), and evaluating it with tools like the confusion matrix and classification report, which you are already familiar with.
An Important Distinction: Multi-Class vs. Multi-Label
Before we conclude, it's vital to distinguish between two types of problems.
- Multi-class classification assumes that each sample belongs to exactly one class. The digit '7' is a '7', and nothing else. Iris flowers are either Setosa, Versicolor, or Virginica, but not more than one. Softmax regression is designed for this.
- Multi-label classification allows each sample to be assigned one or more labels. A news article could be about 'Politics' and 'Europe'. A movie could be tagged as 'Action', 'Sci-Fi', and 'Comedy'.
You would not use a single softmax classifier for a multi-label problem, as it is fundamentally designed to pick only one "best" class. Instead, the standard approach for multi-label problems is to train one binary classifier for each label (e.g., a 'Politics'/'Not Politics' classifier, a 'Europe'/'Not Europe' classifier, etc.). This structure is very similar to OvR, but the interpretation is different.
The "Practice Question" in the Towards Data Science article provides a great thought experiment on this topic.
Deep Dive into Softmax Regression
This short section poses and answers a question that gets to the heart of the multi-class versus multi-label distinction.
Please read the "Practice Question" and its solution. This will help solidify when to use softmax regression and when to use a different strategy.
Conclusion
In this lesson, you've expanded your logistic regression toolkit from binary to multi-class problems. You now understand the two dominant strategies for achieving this extension.
Key Takeaways:
- One-vs-Rest (OvR) is a "divide and conquer" strategy that breaks a K-class problem into K independent binary classification problems.
- Softmax (Multinomial) Regression is a direct generalization of logistic regression. It trains a single, unified model that outputs a true probability distribution across all classes.
- Softmax regression is mathematically equivalent to binary logistic regression when there are only two classes, showing a clear evolutionary link.
- It's crucial to distinguish between multi-class (mutually exclusive categories, suitable for softmax) and multi-label (multiple labels per item, requires independent binary classifiers) problems.
We have now concluded our deep dive into linear models for both regression and classification. In the next module, we will shift gears and explore a completely different approach to classification with our first instance-based algorithm: K-Nearest Neighbors (KNN).