Suffolk University • Sawyer Business School

Classification: Predicting Yes/No

Logistic regression as odds, kNN and Naive Bayes as contrasts, the confusion matrix in depth, precision vs. recall, ROC vs. PR — on 30,000 credit-card customers, 22% of whom default

Session 4 of 13 · Mon Oct 5
Module: Classification & Decisions
150 Minutes
Mondays 5:00 PM
Hands-On Learning
01 // LEARNING OBJECTIVES

What You'll Learn Today

By the end of tonight you'll have both the concept and the working code — every objective maps to a cell you run yourself.

01

Read Logistic Regression as Odds

The sigmoid turns a linear score into a probability; each coefficient is a log-odds change. e^0.69 ≈ 2 means 'doubles the odds' — the sentence a credit officer can repeat.

02

Contrast Three Classifiers

Logistic regression draws a line, kNN votes among neighbors, Naive Bayes multiplies likelihoods. Same data, three decision boundaries, three sets of strengths.

03

Master the Confusion Matrix

Four cells, four business meanings. Precision, recall, F1, specificity — computed by hand once, so you never again trust a single accuracy number.

04

Choose ROC-AUC or PR-AUC

ROC for balanced problems and ranking quality; precision–recall when the positive class is rare and false alarms cost money. Fraud at 0.2% makes the difference brutal.

02 // SESSION AGENDA

Today's Journey

A business question, a dataset, a model — and the judgment to evaluate it.

5:00 PM - 5:20 PM

HW1 Debrief & Meet the Data: Credit-Card Default

20 minutes

Three sign-up lists and three confusion matrices from your HW1 Telco submissions, then a new dataset: 30,000 credit-card customers in Taiwan, six months of bill and payment history, and a label for whether they defaulted the following month. 22.1% did.

  • Baseline: 'nobody defaults' is 77.9% accurate and would bankrupt the bank
  • Features: credit limit, age, six months of payment status, bill amounts, payment amounts
5:20 PM - 5:40 PM

Logistic Regression: A Line in Log-Odds

20 minutes

Why linear regression fails on a 0/1 target, the sigmoid that fixes it, and coefficients as log-odds. Payment status last month has the biggest coefficient — and exponentiating it tells you exactly how much a late payment multiplies the odds of default.

  • predict_proba gives the probability; predict just applies a 0.5 cutoff you never chose
  • C is inverse regularization strength: it's ridge/lasso for classification
5:40 PM - 6:05 PM

kNN & Naive Bayes: Two Other Ways to Draw a Boundary

25 minutes

kNN: no training, just voting among the k nearest customers — and a decision boundary that follows the data everywhere. Naive Bayes: multiply likelihoods, fast and surprisingly good on text. Decision-boundary plots on two features make the three models' personalities visible.

  • kNN needs scaling and gets slow at scale; k controls bias–variance exactly like polynomial degree
  • Naive Bayes assumes independence it doesn't have — and often works anyway
6:05 PM - 6:15 PM

Break

10 minutes

Stretch, coffee, pick a competition teammate — the midterm launches next week.

6:15 PM - 6:45 PM

The Confusion Matrix, Cell by Cell

30 minutes

True positive: a defaulter we flagged. False negative: a defaulter we approved — the expensive cell. False positive: a good customer we declined — the cell that loses revenue and trust. Precision, recall, F1, specificity computed by hand, then classification_report.

  • Precision: of those we flagged, how many defaulted? Recall: of those who defaulted, how many did we flag?
  • F1 is the harmonic mean — useful, but it assumes both mistakes cost the same. They never do
6:45 PM - 7:15 PM

ROC vs. Precision–Recall & Class Imbalance

30 minutes

The ROC curve sweeps every threshold and plots recall against false-positive rate; AUC is the probability a random defaulter scores above a random good customer. Then the PR curve — and why at 22% default ROC is fine, but at 0.2% fraud only PR tells the truth. class_weight='balanced' as the first fix for imbalance; why SMOTE is usually unnecessary.

  • Logistic regression on this data: AUC ≈ 0.72 with raw features, ≈ 0.77 with the payment-status columns treated as categories
  • Imbalance fixes in order: choose the right metric, reweight, tune the threshold (next week), then and only then resample
7:15 PM - 7:30 PM

Wrap-Up: The Cliffhanger

15 minutes

We now have a probability for every customer. We have not decided what to do with it. predict() used 0.5 — who chose that? Next session: costs, thresholds, calibration, and profit curves. HW2 launches tonight.

  • HW2 (due Mon Oct 19 — no class Oct 12): a credit-default classifier in a leak-proof pipeline, with ROC-AUC, PR-AUC, the confusion matrix, and odds ratios in plain English
  • Reading: ISLP Chapter 4 (Classification), 4.1–4.4
FEATURED VIDEO

StatQuest — Logistic Regression

The sigmoid, log-odds, and why we fit a line in a different space.

FEATURED VIDEO

StatQuest — ROC and AUC, Clearly Explained

The best sixteen minutes you can spend before Session 5.

03 // INTERACTIVE RESOURCES

Tools & Learning Materials

Notebooks, readings, explorers, and the stories behind the methods.

Hands-On

Session 4 Notebook — Classification on Credit-Card Default

Logistic regression with odds ratios, kNN and Naive Bayes with decision-boundary plots, the confusion matrix by hand, classification_report, ROC and PR curves, and class_weight='balanced'.

Open in Colab
Play

The Threshold & Profit Explorer — Preview

Next week's explorer, available now: drag the threshold across the two score distributions and watch precision, recall, and the confusion matrix move. Ignore the profit panel until Session 5 — or don't.

Open the explorer
Interactive

MLU-Explain — Logistic Regression

The sigmoid, log-odds, and the decision boundary, interactively. Pairs with tonight's odds-ratio reading.

Open the explainer
Interactive

MLU-Explain — ROC & AUC

Slide the threshold and watch the point walk along the ROC curve. The cleanest explanation of what AUC is (and is not) on the web.

Open the explainer
Interactive

MLU-Explain — Precision & Recall

Precision, recall, F1 and the confusion matrix with a live example — the vocabulary of Session 4 in one scroll.

Open the explainer
Reading

ISLP, Chapter 4 — Classification

Logistic regression, why not linear regression, multiple logistic regression, and generative classifiers (LDA, QDA, Naive Bayes). Sections 4.1–4.4 are tonight; 4.5 compares them.

Read Chapter 4 (free PDF)
Reading

scikit-learn User Guide — Classification Metrics

Every metric from tonight with its formula: precision, recall, F1, ROC-AUC, average precision, and the confusion matrix. The section on multiclass averaging will save you later.

Open the guide
Data

Default of Credit Card Clients — the dataset

Yeh & Lien's 2009 dataset from a Taiwanese bank: 30,000 clients, April–September 2005. The documentation explains the payment-status codes (−1 = paid duly, 1 = one month late …) that make the strongest features.

Read the documentation
Story

Story: AI Credit Models — More Approvals at the Same Risk (vendor-reported)

Lenders using ML underwriting report approving materially more applicants at constant default rates — Upstart has claimed ~43% more approvals at the same loss rate. Vendor numbers, so read them as claims; the mechanism (better ranking than a scorecard) is exactly tonight's AUC.

Read the claims
Video

StatQuest — Logistic Regression & ROC/AUC, Clearly Explained

Search the index for 'Logistic Regression' and 'ROC and AUC'. The ROC video in particular is the best 16 minutes you can spend before next week. Also embedded on this page.

Watch on YouTube
Refresher

Refresher: Naive Bayes Classification (ISOM 730 notebook)

Prof. Arslan's earlier notebook on Gaussian and multinomial Naive Bayes, including the text-classification example. Pairs with the SVM refresher if you want a fourth boundary shape. Hosted in the course repo — opens in Colab with one click.

Open in Colab
Refresher

Refresher: Müller & Guido, Ch. 5 — Metrics, Confusion Matrices, ROC (official notebook)

The book chapter on evaluation: precision, recall, F1, ROC and AUC, with the imbalanced-class discussion. A slower second pass over tonight.

Open in Colab
04 // HANDS-ON ACTIVITIES

Learning by Doing

Open the notebook and build it with us.

Activity 1

Who Defaults Next Month? Three Classifiers, One Confusion Matrix

30,000 credit-card customers, 22% default rate. Fit logistic regression, kNN, and Gaussian Naive Bayes in the same pipeline, compare ROC-AUC and PR-AUC, and then read the logistic model's confusion matrix cell by cell in the bank's language.

1

Frame it

Unit: one cardholder. Target: default next month. Horizon: one month. Decision: reduce the credit line / call / do nothing. Baseline: 77.9% accuracy by predicting 'no default' for everyone.

2

Fit three models in one pipeline

Same preprocessing, swap the last step: LogisticRegression, KNeighborsClassifier(k=25), GaussianNB. Record ROC-AUC and average precision for each.

3

Read the odds

np.exp(coef_) for the logistic model. Find the feature with the largest odds ratio and say it in a sentence a credit officer would understand.

4

Read the matrix

Confusion matrix at the default threshold. Name each cell in business terms. Compute precision and recall by hand, then confirm with classification_report.

python
import numpy as np
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import roc_auc_score, average_precision_score, confusion_matrix

credit = fetch_openml(data_id=42477, as_frame=True)
X, y = credit.data, (credit.target == '1').astype(int)      # 1 = defaulted next month
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, stratify=y, random_state=835)

models = {
    'logistic': LogisticRegression(max_iter=2000),
    'knn-25':   KNeighborsClassifier(n_neighbors=25),
    'naive bayes': GaussianNB(),
}
for name, clf in models.items():
    pipe = make_pipeline(StandardScaler(), clf).fit(X_tr, y_tr)
    p = pipe.predict_proba(X_te)[:, 1]
    print(f'{name:12s} ROC-AUC {roc_auc_score(y_te, p):.3f}   PR-AUC {average_precision_score(y_te, p):.3f}')

logit = make_pipeline(StandardScaler(), LogisticRegression(max_iter=2000)).fit(X_tr, y_tr)
print(confusion_matrix(y_te, logit.predict(X_te)))   # rows: actual, cols: predicted

The three models rank customers similarly (AUC within a few points) — but look at the confusion matrix: at the default threshold, logistic regression flags only a fraction of the defaulters. That is not a modeling failure. It is a decision nobody made: predict() silently assumes a missed default costs the same as a wrongly declined customer. Next week we price the two mistakes and let the price choose the threshold.

05 // KEY TAKEAWAYS

What to Remember

Logistic regression is linear in log-odds: exponentiate a coefficient to get an odds ratio, and say it in words — 'a late payment last month roughly doubles the odds of default.'

The confusion matrix has four cells with four business meanings. Precision asks 'of those we flagged…', recall asks 'of those who actually…'. Accuracy averages over both mistakes and hides which one you are making.

ROC-AUC measures ranking; PR-AUC measures usefulness on rare positives. At 22% default either works; at 0.2% fraud only precision–recall tells the truth. And predict()'s 0.5 threshold is a decision you have not yet made.

06 // HOMEWORK

Before Next Session

Complete these before next Monday — they are the practice that makes the next session land.

Homework #2 (launches tonight) Due Mon Oct 19, 11:59 PM, on Canvas (no class Oct 12 — October Break). Build a credit-default classifier on the Taiwan data in a leak-proof pipeline (logistic regression required; a second model of your choice optional). Split before you fit anything, stratified by the target. Report ROC-AUC and PR-AUC on the test set, show the confusion matrix next to the 'nobody defaults' baseline, and interpret the three largest odds ratios in plain English. Under every question, one short paragraph on what the number means for the bank. The threshold-and-cost part follows in Session 5 as HW3. Reading before Session 5: ISLP 4.1–4.4.
07 // PRACTICE

Test Yourself

Exam-style questions for the ideas in this session — Classification & Decisions. Every wrong answer names the misconception behind it.

Q19 150 flagged, 120 right, 200 real churners. Precision and recall by hand. Q20 Identical ROC-AUC, wildly different PR-AUC. Which one does fraud care about? Q21 A $50 offer, a $600 save. The break-even probability is not 0.5. Q22 A logistic coefficient of 0.69. Say it in odds. Q23 AUC 0.88, but the 0.85 bucket churns at 55%. What broke? Q24 Top decile captures 38% of buyers. Compute the lift and cash it in.

All of Decisions →

Coming Next

Session 5: From Scores to Decisions — Thresholds, Costs & Calibration

The expected-value framework, profit curves, TunedThresholdClassifierCV, calibration plots, lift and gain charts, and targeting under a budget. Plus: the Midterm Model Competition launches.

Decision ThresholdsCalibrationCompetition Launch