Logistic regression models the probability of a binary outcome by applying the sigmoid function to a linear combination of predictors, producing coefficients interpretable as odds ratios, and evaluated using confusion matrices, ROC curves, and AUC — making it the foundational classification technique that underpins AI/machine learning classification systems.
Core Concepts at a Glance
Concept
Definition
Why It Matters
Logistic regression
Models the log-odds of a binary outcome as a linear function of predictors: $\ln(P/(1-P)) = b_0 + b_1 x_1 + \cdots$
Extends regression to yes/no outcomes; the simplest classifier
Sigmoid function
$P = 1/(1+e^{-z})$ — maps any real number to a probability between 0 and 1
Solves the problem of linear regression predicting impossible probabilities
Odds ratio
$e^{b_i}$ — the multiplicative change in odds for a one-unit increase in $x_i$
The primary way to interpret logistic regression coefficients
Confusion matrix
A 2$\times$2 table cross-tabulating predicted vs. actual outcomes (TP, FP, FN, TN)
The foundation for all classification evaluation metrics
ROC curve / AUC
Plots sensitivity vs. 1-specificity at all thresholds; AUC summarizes overall discrimination
Evaluates the model independent of any specific threshold
The Logistic Regression Procedure
Step by Step
Confirm binary outcome. Ensure the response variable is binary (0/1, yes/no, success/failure).
Explore the data:
- Compare predictor distributions between the two outcome groups (box plots, bar charts)
- Check outcome rate (base rate) — imbalanced classes require special attention
Fit the model using sm.Logit(y, X).fit() (statsmodels) or LogisticRegression().fit(X, y) (sklearn).
Interpret coefficients:
- Exponentiate each coefficient: $e^{b_i}$ = odds ratio
- "For each one-unit increase in $x_i$, the odds of the outcome are multiplied by $e^{b_i}$, holding all other variables constant"
- Check p-values and 95% CIs for the odds ratios
Evaluate the model:
- Confusion matrix at the chosen threshold
- Accuracy, sensitivity, specificity, precision, F1 score
- ROC curve and AUC
Choose the threshold:
- Consider the relative costs of false positives vs. false negatives
- The threshold is a values decision, not just a statistical one
Check fairness:
- Evaluate error rates separately for each relevant group
- Equal accuracy does not guarantee equal error rates
Key Python Code
import statsmodels.api as sm
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (confusion_matrix, classification_report,
roc_curve, auc)
import numpy as np
# --- STATSMODELS (inference: coefficients, p-values, CIs) ---
X_sm = sm.add_constant(df[['x1', 'x2', 'x3']])
model = sm.Logit(df['y'], X_sm).fit()
print(model.summary())
# Odds ratios
print("Odds Ratios:")
print(np.exp(model.params).round(3))
print("95% CI for Odds Ratios:")
print(np.exp(model.conf_int()).round(3))
# --- SKLEARN (prediction and evaluation) ---
clf = LogisticRegression(max_iter=1000)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
y_prob = clf.predict_proba(X_test)[:, 1]
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
print(classification_report(y_test, y_pred))
# ROC and AUC
fpr, tpr, thresholds = roc_curve(y_test, y_prob)
print(f"AUC: {auc(fpr, tpr):.3f}")
Excel Procedure
Step
Action
1. Enter data
Binary outcome (0/1) in one column; predictors in separate columns
2. Note
Excel's Data Analysis ToolPak does NOT include logistic regression. Use the Solver add-in to maximize the log-likelihood, or use Python/R
3. Alternative
Export data to CSV and analyze in Python (recommended)
The Threshold Concept: Thinking in Odds
Probability, odds, and log-odds are three representations of the same quantity. Logistic regression models log-odds because they range from $-\infty$ to $+\infty$, making them suitable for a linear model. The odds ratio ($e^{b}$) is the most interpretable form of the coefficient.
Currency
Range
Formula from P
Formula to P
Probability ($P$)
0 to 1
—
—
Odds
0 to $\infty$
$\text{Odds} = \frac{P}{1-P}$
$P = \frac{\text{Odds}}{1+\text{Odds}}$
Log-odds (logit)
$-\infty$ to $+\infty$
$\text{Logit} = \ln\left(\frac{P}{1-P}\right)$
$P = \frac{1}{1+e^{-\text{Logit}}}$
Key Insight
Details
P = 0.50 corresponds to odds = 1 and logit = 0
The midpoint; where the outcome is equally likely
Coefficients are additive on the log-odds scale
$b_1 = 0.5$ means log-odds increase by 0.5 per unit
Odds ratios are multiplicative on the odds scale
$e^{0.5} = 1.65$ means odds multiplied by 1.65 per unit
Probability changes are not constant
Same log-odds change produces different probability changes depending on starting probability