Adventures in Machine Learning

Understanding Logistic Regression: Evaluating Model Performance with AUC Sensitivity and Specificity

Logistic Regression as a Method for Model Fitting: Understanding Sensitivity, Specificity, and AUC

As we delve into the world of machine learning and predictive modeling, one of the most commonly used tools is logistic regression. In this article, we will dive into the definition and usage of logistic regression, and explore how sensitivity, specificity, and AUC are used to evaluate model performance.

Logistic regression is a statistical method used to analyze a dataset with a binary outcome variable. It is called a regression because it predicts the probability of the binary outcome variable based on the values of the input variables.

Logistic regression generates a regression model with coefficients that can be used to predict the probability of the outcome, given a set of input variable values.

Sensitivity and Specificity as Model Metrics

Once the logistic regression model has been fit, we can use metrics such as sensitivity and specificity to evaluate its performance. Sensitivity measures the proportion of actual positives that are correctly identified by the model, while specificity measures the proportion of negatives that are correctly identified.

True positive rate (TPR) and true negative rate (TNR) are other terms used to describe sensitivity and specificity.

  • Sensitivity = TP / (TP + FN)
  • Specificity = TN / (TN + FP)

To calculate sensitivity, we divide the number of true positives (TP) by the total number of actual positives (TP + false negatives (FN)).

Similarly, to calculate specificity, we divide the number of true negatives (TN) by the total number of actual negatives (TN + false positives (FP)).

ROC Curve as a Visualization Tool

A receiver operating characteristic (ROC) curve is a graph that plots the true positive rate against the false positive rate (FPR) for different probability thresholds of the logistic regression model. The ROC curve is used to evaluate the performance of the model by measuring how well it can distinguish between positive and negative samples.

The ROC curve is constructed by plotting the TPR on the y-axis against the FPR on the x-axis. The more the ROC curve is shifted toward the top left corner, the better the model’s performance.

A diagonal line from the bottom left to the top right corner represents the performance of a random classifier, while a curve that lies above this line indicates that the model is better than random.

AUC as a Measure of Model Performance

The area under the ROC curve (AUC) is a measure of the model’s performance. It represents the probability that the model will rank a randomly chosen positive instance higher than a randomly chosen negative instance.

The AUC value ranges from 0 to 1, with a value of 0.5 indicating that the model is no better than random, and a value of 1 indicating perfect performance. An AUC value of 0.7 to 0.8 is considered acceptable, while a value of 0.8 to 0.9 is considered excellent.

An AUC value of 0.9 or higher indicates outstanding performance.

Example of Calculating AUC for Logistic Regression Model in Python

Now, let’s explore an example of how to calculate AUC for a logistic regression model in Python. We will use the popular Python libraries pandas, numpy, and scikit-learn.

Importing Necessary Packages and Dataset

First, we need to import the necessary packages and load in our dataset. We will be using the breast cancer dataset from scikit-learn, which includes classification data for diagnosing breast cancer.

import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score

#Loading data
data = load_breast_cancer()
X = pd.DataFrame(, columns=data.feature_names)
y = pd.DataFrame(, columns=["Cancer"])

#Dividing data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Fitting the Logistic Regression Model

Next, we will fit the logistic regression model. We will use the fit method on the logistic regression object to train the model on our training data.

#Training a logistic regression model
lr = LogisticRegression(), y_train)

Now that we have fit the model to our training data, we can use it to make predictions on our test data. We will use the predict_proba method to predict the probability of each test case belonging to class 1 (cancer-positive).

#Predicting probabilities for test set
y_pred_proba = lr.predict_proba(X_test)

Calculating the AUC

Finally, we can calculate the AUC score for our model. We will use the roc_auc_score function from scikit-learn to compute the AUC score.

This function takes in the true labels (y_test) and the predicted probabilities (y_pred_proba) as input.

#Calculating the AUC score
auc = roc_auc_score(y_test, y_pred_proba[:, 1])
print("AUC score:", auc)

The output will be the AUC score for our logistic regression model, which can be used to evaluate its performance.


In this article, we have explored the concept of logistic regression and its utility in predictive modeling. We have also looked at how sensitivity, specificity, ROC curves, and AUC scores can be used to evaluate the performance of logistic regression models.

Furthermore, we presented an example of how to calculate the AUC score for a logistic regression model in Python using scikit-learn. That said, learning to evaluate machine learning algorithms effectively is crucial and can take your models from good to great.

Logistic regression is a widely used statistical method for predicting binary outcomes. To evaluate the performance of the model, metrics such as sensitivity, specificity, ROC curve, and AUC score can be used.

Sensitivity and specificity measure the model’s ability to correctly identify positive and negative samples. ROC curve and AUC score provide a graphical and numerical measure of model performance.

In this article, we explored how to calculate AUC for a logistic regression model in Python with the breast cancer dataset. Understanding these concepts is essential in evaluating machine learning algorithms effectively.

Whether you are a data scientist or a business analyst interpreting models, understanding and measuring model performance will help you make informed decisions that impact your bottom line.

Popular Posts