Skip to main content

Welcome to a Grasp lesson

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

Lesson 68

Multiclass Logistic Regression with scikit-learn

Recommended resources

Hello! Welcome back.

In our last lesson, you did an excellent job building a binary logistic regression model from scratch. By implementing the sigmoid function, cross-entropy loss, and gradient descent, you gained a deep understanding of how a classifier learns.

Today, we'll build on that foundation to tackle a more common and powerful scenario: multiclass classification. While binary classification handles "yes/no" questions, multiclass classification lets us answer "which one?" from a set of several possible categories. Our goal is to train and evaluate multiclass logistic regression models, but this time, we'll leverage the power and efficiency of the scikit-learn library.

From Binary to Multiclass: The Strategies

How can we adapt an algorithm designed for two classes (0 or 1) to handle three, ten, or even a hundred classes? There are two primary strategies for extending logistic regression to multiclass problems.

  1. One-vs-Rest (OvR): Also known as One-vs-All. This strategy is quite intuitive. If you have K classes, you train K separate binary logistic regression classifiers. Each classifier is trained to distinguish one class (the "one") from all other classes combined (the "rest"). When you want to predict a new instance, you run it through all K classifiers, and the one that outputs the highest probability wins.

  2. Multinomial (Softmax) Regression: Instead of training multiple independent classifiers, this approach generalizes logistic regression in a single model. It uses a function called Softmax, which is the multiclass equivalent of the sigmoid function you learned about. It computes a score for each class and then converts those scores into a probability distribution where all probabilities sum to 1.


Understanding Softmax Regression

Since you appreciate the "why" behind the algorithms, let's briefly look at how Softmax works. It's a natural extension of the concepts from our last lesson.

  1. Linear Score: For each class k (out of K total classes), the model calculates a linear score, just like in binary regression: . This gives us a vector of scores .

  2. Exponentiation: The scores are then exponentiated to make them all positive: .

  3. Normalization: Finally, each exponentiated score is divided by the sum of all exponentiated scores. This step normalizes the values so they become probabilities that sum to 1. The probability of the instance belonging to class k is:

This is the Softmax function.

To see this in action, watch the following short video from Andrew Ng's DeepLearning.AI course. It provides a clear conceptual overview and a numerical example.

Softmax Regression (C2W3L08)

This video, 'Softmax Regression' from DeepLearningAI, explains the core idea behind generalizing logistic regression to handle multiple classes. It walks through the structure of the output layer and the mathematics of the softmax function.

Watch the segment from 00:00 to 08:08. Focus on: How the problem is framed for multiple classes (0:00 - 1:32). The role of the softmax layer in producing a probability for each class (1:32 - 2:55). The mathematical steps of the softmax function, including a concrete numerical example (2:55 - 8:08).

Now, let's put this theory into practice.


Implementing Multiclass Logistic Regression with scikit-learn

While it's valuable to understand the mechanics, for practical applications, we use libraries like scikit-learn. They provide highly optimized, robust, and easy-to-use implementations.

We'll walk through a classic multiclass problem: recognizing handwritten digits. The following video by codebasics is an excellent guide. You'll see how to load the data, train a model, and evaluate its performance in just a few lines of code.

Machine Learning Tutorial Python - 8 Logistic Regression (Multiclass Classification)

The video 'Logistic Regression (Multiclass Classification)' by codebasics provides a complete, practical walkthrough. We'll use it to guide our implementation.

Watch from the beginning to 08:22. This covers setting up the problem and training the model. Intro & Data Loading (0:00 - 4:49): See how to load the built-in digits dataset from scikit-learn and explore its structure. Data Splitting & Model Training (4:49 - 8:22): Observe the standard practice of splitting data into training and testing sets, and then creating and fitting the LogisticRegression model.

As you saw in the video, the core steps are:

  1. Load Data: scikit-learn has convenient functions like load_digits() for datasets.
  2. Split Data: train_test_split is used to create training and testing sets, which is crucial for unbiased model evaluation.
  3. Instantiate and Train: You create an instance of the LogisticRegression class and call the .fit(X_train, y_train) method. scikit-learn handles all the gradient descent optimization internally!

Evaluating Multiclass Classification Performance

After training, how do we know if our model is any good? The .score() method gives us the overall accuracy, which is a good starting point. However, accuracy doesn't tell the whole story. It doesn't show us what kinds of mistakes the model is making. For that, we use a confusion matrix.

A confusion matrix for a K-class problem is a KxK table where:

  • The rows represent the True Labels.
  • The columns represent the Predicted Labels.
  • The cell at (i, j) contains the number of instances of true class i that were predicted as class j.

Correct predictions lie on the diagonal (where i=j), while all off-diagonal cells represent errors. This helps you instantly see if the model is confusing specific classes (e.g., frequently mistaking a '3' for an '8').

Let's see how to generate and interpret one.

Machine Learning Tutorial Python - 8 Logistic Regression (Multiclass Classification)

Continuing with the codebasics video, this next segment focuses on model evaluation, particularly the confusion matrix.

Watch from 08:22 to the end (13:58). Pay close attention to: How the model's accuracy is calculated with the .score() method. How to generate predictions for the test set. How to create a confusion matrix and visualize it as a heatmap to easily spot where the model is performing well and where it is failing.

Test your understanding!

Imagine you are looking at a confusion matrix for the digits dataset. You see a non-zero number in the cell at row 9, column 4. What does this tell you about your model's performance?

Show answer

This means that there was at least one instance where the true handwritten digit was a '9', but your model incorrectly predicted it as a '4'. The diagonal elements represent correct predictions, so any value off the diagonal indicates a specific type of misclassification.


OvR vs. Multinomial: Which to Choose?

In scikit-learn, you can control the multiclass strategy with the multi_class parameter in LogisticRegression. The default is 'auto', which tries to pick the best strategy based on the data and the chosen solver. You can explicitly set it to 'ovr' for One-vs-Rest or 'multinomial' for Softmax regression.

Comparison of Multinomial and One-vs-Rest Logistic Regression Decision Boundaries
Decision boundaries for Multinomial (left) vs. One-vs-Rest (right) logistic regression. Notice how the Multinomial approach creates boundaries that are simultaneously optimized for all classes, which can sometimes lead to better performance.
Comparison of Multinomial vs. One-vs-Rest Logistic Regression Confusion Matrices
Example confusion matrices for Multinomial and OvR strategies. Both models perform well on this dataset, but for more complex problems, the differences can be more pronounced.

So, which one should you use? The following article provides an excellent breakdown.

Mastering Logistic Regression with Scikit-Learn

The article 'Mastering Logistic Regression with Scikit-Learn' from DigitalOcean offers a detailed comparison of the OvR and Multinomial strategies and discusses important model parameters.

Please read the following sections: 'Multiclass Logistic Regression in Scikit-Learn': This section compares the One-vs-Rest (OvR) and Multinomial strategies, detailing their advantages and limitations. 'Scikit-learn Logistic Regression Parameters and Solvers': This provides a great overview of crucial parameters like penalty, C, and solver. 'Hyperparameter Tuning for Logistic Regression': This section explains how to use techniques like GridSearchCV to find the best parameters for your model.

Key takeaways from the reading:

  • 'multinomial' (Softmax) is often more accurate, especially if classes overlap, because it models the probabilities jointly. However, it's computationally more intensive and only compatible with specific solvers (like 'lbfgs', 'newton-cg', 'saga').
  • 'ovr' is faster and works with all solvers (including 'liblinear', which is great for large or sparse datasets). It's a solid baseline approach.
  • Hyperparameter Tuning is crucial. Finding the optimal C (regularization strength) and solver for your specific problem can significantly boost performance. Using GridSearchCV is a systematic way to do this.

Beyond the confusion matrix, you can also use metrics like precision, recall, and F1-score for multiclass problems. scikit-learn allows you to calculate these by specifying an averaging method ('micro', 'macro', or 'weighted') to aggregate the scores from each class. The article "Logistic Regression and Evaluation Metrics" explains this well in its "Evaluation Metrics for Multiclass Classification" section, which you can refer to for more detail.


Conclusion

Congratulations! You've successfully made the leap from binary to multiclass classification. You now have the tools to tackle a much wider range of real-world problems.

Key Takeaways:

  • Logistic regression can be extended to multiclass problems using the One-vs-Rest (OvR) or Multinomial (Softmax) strategies.
  • scikit-learn provides a powerful LogisticRegression class that makes training and evaluating these models straightforward.
  • The confusion matrix is an essential tool for understanding a classifier's performance in detail, showing not just if it made errors, but what kind of errors it made.
  • Choosing the right strategy ('ovr' vs. 'multinomial') and tuning hyperparameters like solver and C are key to building high-performing models.

Preview of the Next Lesson:

So far, we've focused on logistic regression, which is a powerful linear model. It works by creating linear decision boundaries to separate classes. In the next lesson, we will explore a completely different type of classifier: the Decision Tree. This is a non-linear model that learns a set of hierarchical rules, which can capture more complex patterns in data.