Skip to main content

Welcome to a Grasp lesson

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

Lesson 63

Logistic Regression with scikit-learn

Recommended resources

Introduction

In our previous lesson, we established the theoretical foundation of logistic regression. You learned why linear regression is ill-suited for classification tasks and how the sigmoid function elegantly transforms a linear equation's unbounded output into a meaningful probability between 0 and 1. We also touched upon the mathematical origins of this function, connecting it to the concept of log-odds.

Today, we transition from theory to practice. Your learning outcome is to train and evaluate a logistic regression model using scikit-learn. We will walk through the entire workflow in Python: loading data, splitting it for training and testing, fitting a model, and most importantly, using robust metrics to evaluate its performance. This practical application is a critical step toward your goal of building transformative AI.


The Standard Scikit-Learn Workflow

Scikit-learn is a cornerstone library for machine learning in Python. It provides a consistent and user-friendly interface for most common algorithms. The process for training a supervised learning model like logistic regression typically follows these five steps:

  1. Prepare Data: Load your dataset and separate it into features (X) and the target variable (y).
  2. Split Data: Divide your data into a training set (to teach the model) and a testing set (to evaluate its performance on unseen data).
  3. Instantiate Model: Create an instance of the model class from scikit-learn (e.g., LogisticRegression()).
  4. Train Model: "Fit" the model to the training data using the .fit() method. This is where the learning happens—the model finds the optimal weights (w) and bias (b).
  5. Make Predictions: Use the trained model to make predictions on the test data with the .predict() method.

Let's see this entire workflow in action.

Machine Learning Tutorial Python - 8: Logistic Regression (Binary Classification)

The video 'Machine Learning Tutorial Python - 8' by codebasics provides a clear, step-by-step demonstration of implementing this workflow for logistic regression.

Please watch from 07:52 to 13:52. Follow along as the presenter: Loads the data using pandas. Splits the data into training and testing sets using train_test_split. Imports LogisticRegression and creates an instance of the model. Trains the model using the .fit(X_train, y_train) method. Makes predictions on the unseen test data using .predict(X_test).

This sequence—import, instantiate, fit, predict—is a fundamental pattern you will use repeatedly as you build more complex machine learning models.


Evaluating Model Performance

Now that we have a trained model and a set of predictions, how do we know if it's any good? This "evaluation" step is arguably the most critical part of the process. A model is only useful if we can quantify and trust its performance.

The Baseline: Accuracy

The most intuitive metric is accuracy: the proportion of correct predictions out of the total number of predictions. Scikit-learn provides a convenient .score() method to calculate this.

Machine Learning Tutorial Python - 8: Logistic Regression (Binary Classification)

Let's start with accuracy. The video you just watched continues to show how to calculate this simple metric.

Continue watching from 13:52 to 14:30. Note how the .score() method is used to quickly get the accuracy of the model on the test data.

While simple, accuracy can be dangerously misleading, especially for datasets where one class is much more frequent than another (an "imbalanced dataset"). For example, if you build a model to detect a rare disease (affecting 0.1% of the population) and it achieves 99.9% accuracy by always predicting "no disease," it's a perfect score but a completely useless model.

To make the kind of high-stakes decisions you're aiming for, we need more nuanced metrics.

A Deeper Look: The Confusion Matrix

A confusion matrix provides a much more detailed breakdown of a model's performance. It's a table that compares the actual values to the predicted values and separates them into four categories.

Logistic Regression Evaluation with Confusion Matrix using Scikit-Learn
This is a confusion matrix, a key tool for evaluating a classification model. It breaks down predictions into four categories: True Positives (correctly predicted positive), True Negatives (correctly predicted negative), False Positives (incorrectly predicted positive), and False Negatives (incorrectly predicted negative).
  • True Positives (TP): The model correctly predicted the positive class. (e.g., correctly identified a patient with the disease).
  • True Negatives (TN): The model correctly predicted the negative class. (e.g., correctly identified a patient without the disease).
  • False Positives (FP): The model incorrectly predicted the positive class. (e.g., told a healthy patient they have the disease). This is also known as a "Type I error."
  • False Negatives (FN): The model incorrectly predicted the negative class. (e.g., told a sick patient they are healthy). This is also known as a "Type II error."

From this matrix, we can derive more powerful metrics.

Precision, Recall, and F1-Score

  • Precision: Answers the question, "Of all the times the model predicted positive, how often was it correct?"

    High precision is critical when the cost of a False Positive is high. For example, in spam filtering, you want to be very precise to avoid putting an important email in the spam folder (a false positive).

  • Recall (or Sensitivity): Answers the question, "Of all the actual positive cases, how many did the model correctly identify?"

    High recall is critical when the cost of a False Negative is high. In medical diagnosis, you want high recall to avoid missing any actual cases of disease (a false negative).

  • F1-Score: The harmonic mean of Precision and Recall. It provides a single score that balances both concerns.

The scikit-learn library makes it easy to compute all these metrics at once using a classification_report.

Hands-On Machine Learning: Logistic Regression with Python and Scikit-Learn

The 'Hands-On Machine Learning' video by Ryan & Matt Data Science gives an excellent walkthrough of creating a confusion matrix and interpreting the full classification report.

Watch from 12:49 to 15:28. Pay close attention to: How to generate a confusion_matrix. The explanation of the different cells in the matrix. How to generate and interpret a classification_report, including the definitions of precision, recall, and F1-score.

Understanding the trade-off between precision and recall is central to applied machine learning. Deciding whether it's worse to have a False Positive or a False Negative depends entirely on the problem context—a skill essential for the kind of advanced, pressure-tested decision-making you aim to master.


Fine-Tuning the Model

So far, we have used LogisticRegression() with its default settings. However, "training" a model also involves choosing the right configuration. These settings, known as hyperparameters, control the learning process itself.

Two of the most important hyperparameters for logistic regression are penalty and C. These control regularization, a technique to prevent overfitting by penalizing model complexity. You encountered this idea in Module 7 with the bias-variance tradeoff.

Scikit-learn Logistic Regression Parameters and Solvers

To train a model effectively, we must understand its key hyperparameters. The 'Mastering Logistic Regression with Scikit-Learn' guide from DigitalOcean explains the most important ones.

Please read the section titled 'Scikit-learn Logistic Regression Parameters and Solvers'. Focus on understanding the roles of the penalty and C parameters. penalty can be 'l1' or 'l2', corresponding to different ways of measuring model complexity. C is the inverse of regularization strength.

Here's the key relationship to remember for the hyperparameter C:

  • Small C: Strong regularization. The model is pushed to be simpler, which can help prevent overfitting but might lead to underfitting.
  • Large C: Weak regularization. The model is allowed to be more complex to fit the training data better, but this increases the risk of overfitting.

Finding the optimal values for these hyperparameters is a process called hyperparameter tuning, which is a more advanced topic we will revisit later. For now, it's important to know that these levers exist for you to control and optimize your model.


Conclusion

You have now successfully bridged the gap from theory to practice. You've learned the standard procedure for implementing a machine learning model in scikit-learn and, more importantly, how to critically evaluate its performance beyond simple accuracy.

Key Takeaways:

  • The standard scikit-learn workflow is to prepare data, split, instantiate, fit, and predict.
  • Accuracy is an easy-to-understand metric but can be misleading, especially with imbalanced data.
  • The confusion matrix provides a detailed breakdown of a model's predictions into True/False Positives and Negatives.
  • Precision and Recall are powerful metrics that measure different aspects of a model's performance, and there is often a trade-off between them.
  • The F1-Score provides a balanced measure of both precision and recall.
  • You can "tune" a logistic regression model using hyperparameters like penalty and C to control regularization.

Preview of the Next Lesson:
We've now covered a foundational classification algorithm. In the next lesson, we will explore a completely different approach with the k-Nearest Neighbors (KNN) algorithm. Unlike logistic regression, which learns a mathematical function to separate the data, KNN is a non-parametric method that makes predictions based on the principle of "similarity." This will broaden your toolkit and show you that there are often multiple, distinct ways to solve the same problem.