Welcome to Week 10

  • Quick overview of today’s plan:

    • Understand the difference between regression and classification
    • Introduction to logistic regression and why it works for categories
    • How to interpret model results (coefficients and probabilities)
    • Why accuracy isn’t enough and what metrics actually matter
    • Business scenarios: choosing the right evaluation approach

Discussion: Homework & Questions

Questions from Week 9?

  • Machine learning fundamentals?
  • Supervised vs. unsupervised learning?
  • Correlation vs. linear regression?
  • Anything confusing in the quiz or readings?
  • Time to ask!

Activity

Converse with your neighbor and identify…

  • 1 concept from last week that you thought was well explained
  • 1 concept that is still confusing

Predicting Numbers Categories

Not Every Business Question is About Numbers

Week 9 Review: We’ve been predicting continuous values…

  • How much revenue will we generate?
  • What price should we set for this product?
  • What will our customer lifetime value be?

Today’s New Challenge: Many critical decisions involve categories…

  • Will this customer default on their loan? (Yes/No)
  • Is this email spam? (Spam/Not Spam)
  • Should we approve this application? (Approve/Deny)
  • Will this marketing campaign succeed? (Success/Failure)

Pop Quiz: Regression vs. Classification

Rapid fire, classify each as REGRESSION or CLASSIFICATION:

  • Predicting how many units of a product will sell next month
  • Determining if a customer will purchase a premium subscription
  • Estimating insurance claim amounts in dollars
  • Deciding whether to show a promotional offer to a customer
  • Forecasting quarterly revenue
  • Classifying support tickets as “Technical” or “Billing”
  • Predicting employee salary based on experience and education
  • Determining if a loan application should be approved or denied
  • Estimating the number of website visitors next month
  • Classifying emails as “Spam”, “Important”, or “General”

Think-Pair-Share: Regression vs. Classification

In your groups, discuss where classification models are already impacting your daily lives:

Examples might include:

  • Netflix recommending movies you’ll like,
  • sports betting apps predicting which team will win,
  • social media deciding what posts to show you,
  • your bank determining whether to approve a purchase.

Share 2-3 examples with your group and discuss:

  • What categories is the model predicting?
  • How does this affect your experience as a user?

Why Linear Regression Fails

Problems with Linear Regression for Classification:

  • Invalid predictions: Can predict values like -0.3 or 1.8 for Yes/No questions
  • Straight line assumption: Real classification relationships follow S-shaped curves
  • No probability interpretation: A prediction of 0.7 doesn’t clearly mean 70% chance

Example: Credit default prediction

  • Linear regression might predict 1.3 for “will default”
  • What does 1.3 mean? 130% chance of default? Impossible!

Logistic Regression: The S-Shaped Solution

The Logistic Function

Key Innovation: Transform linear predictions into probabilities using the logistic function:

\[p = \frac{1}{1 + e^{-z}}\]

where \(z = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ...\)


Tip

Does \(z\) look familiar? 🤔


This equation takes any number (our risk score \(z\)) and squeezes it between 0 and 1 to create a valid probability.

Creates Valid Probabilities

Magic Properties:

  • Always between 0 and 1 - perfect for probabilities!
  • S-shaped curve - captures realistic business relationships
  • Smooth transitions - gradual probability changes, not jumps
  • 50% at center - when z = 0, probability = exactly 50%

Using the S-Curve for Business Decisions

The S-shaped curve gives us two powerful tools for business decision-making:

1. Probability Estimates: Get the exact chance an event will happen (0-100%)

2. Binary Classifications: Make yes/no decisions using the 50% threshold

Key Insight: Every prediction gives you a probability between 0-100%, but the 50% threshold determines the final yes/no decision.

Business Context Activity

Now that you understand logistic regression gives us both probability estimates AND classifications, let’s think about when each is most valuable:

In your groups, identify:

  1. 2 business scenarios where you’d want PROBABILITY ESTIMATES (exact percentages)
  2. 2 business scenarios where you’d want CLASSIFICATIONS (yes/no decisions)

Then discuss: Why do you think probability estimates are better for some situations while classifications are better for others?

Think about: Risk assessment, decision automation, customer communication, regulatory requirements, etc.

Then we’ll share examples with the class…

Building a Logistic Regression Model

Step 1: Load and explore the ISLP Default dataset

from ISLP import load_data
import pandas as pd

# Load the Default dataset
Default = load_data('Default')

Default[['default', 'balance', 'income']].head()
default balance income
0 No 729.526495 44361.625074
1 No 817.180407 12106.134700
2 No 1073.549164 31767.138947
3 No 529.250605 35704.493935
4 No 785.655883 38463.495879

Step 2: Fit the logistic regression model

from sklearn.linear_model import LogisticRegression

# Prepare features (balance and income) and target
X = Default[['balance', 'income']]
y = (Default['default'] == 'Yes').astype(int)

# Fit logistic regression model
model = LogisticRegression()
model.fit(X, y)

# Extract the coefficients
intercept = model.intercept_[0]
balance_coef = model.coef_[0][0]
income_coef = model.coef_[0][1]

print(f"Intercept: {intercept:.6f}")
print(f"Balance coefficient: {balance_coef:.6f}")
print(f"Income coefficient: {income_coef:.6f}")
Intercept: -11.540468
Balance coefficient: 0.005647
Income coefficient: 0.000021

So, what do these coefficients mean? 🤔

Probability, Odds, and Log-Odds

To interpret logistic regression coefficients, we need three related concepts:

  • Probability: The familiar 0-100% chance something happens
  • Odds: Ratio of probability it happens vs. doesn’t happen (probability / (1 - probability))
  • Log-Odds: Natural logarithm of the odds
Probability Odds Log-Odds Business Interpretation
10% 0.11 -2.20 Very unlikely event
25% 0.33 -1.10 Unlikely event
50% 1.00 0.00 Neutral (decision boundary)
75% 3.00 1.10 Likely event
90% 9.00 2.20 Very likely event

Key insight:

Logistic regression coefficients represent changes in log-odds, not probability!

Interpreting Our Coefficients

Now we can understand what our coefficients mean:

Intercept (-11.540468):

  • When balance = $0 and income = $0, the log-odds of default = -11.54
  • This means very low baseline probability of default (~0.000973%)

Balance coefficient (0.005647):

  • For each $1 increase in balance, log-odds increase by 0.005647
  • For each $1,000 increase in balance, log-odds increase by ~5.6 Probability increases ~95%
  • Positive coefficient = higher balance increases default risk

Income coefficient (0.000021):

  • For each $1 increase in income, log-odds increase by 0.000021
  • For each $10,000 increase in income, log-odds increase by ~0.21
  • Probability increases by only ~5%
  • Tiny positive coefficient = income has minimal effect on default risk

Business Translation:

  • Balance is the primary driver of default risk
  • Income has almost no effect once we account for balance
  • A customer with $2,000 balance is much riskier than one with $1,000 balance, regardless of income

From Coefficients to Predictions

Step 1: Use coefficients in the logistic function

import numpy as np

# Manual calculation for $1,500 balance, $50,000 income
balance = 1500
income = 50000
log_odds = -11.540468 + 0.005647 * balance + 0.000021 * income
probability_manual = 1 / (1 + np.exp(-log_odds))

print(f"Manual calculation: {probability_manual:.4f}")
Manual calculation: 0.1171

Step 2: Compare with scikit-learn

# Scikit-learn prediction (must include both features)
customer_df = pd.DataFrame({'balance': [1500], 'income': [50000]})
probability_sklearn = model.predict_proba(customer_df)[:, 1]

print(f"Scikit-learn prediction: {probability_sklearn[0]:.4f}")
Scikit-learn prediction: 0.1162

Both give similar results for a customer with $1,500 balance and $50,000 income

Small Differences Expected

The manual calculation and scikit-learn prediction may differ slightly (e.g., 0.1171 vs 0.1162) due to rounding in the displayed coefficients. Scikit-learn uses the full precision coefficients internally.

Evaluating Model Performance

Now that we’ve built our model, let’s evaluate how well it performs on new data:

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Split data into training and test sets
X = Default[['balance', 'income']]
y = (Default['default'] == 'Yes').astype(int)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)

# Train the model on training data
model = LogisticRegression()
model.fit(X_train, y_train)

# Make predictions on test data
y_pred = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.1%}")
Model Accuracy: 97.1%

Critical Question

With an accuracy of 97%+, should we base our model’s performance solely on this metric?

Is this a good model for our business needs?

Beyond Accuracy: Why 97% Can Be Terrible

The Accuracy Trap

Shocking Example: Credit card fraud detection with 100,000 transactions:

  • Fraudulent transactions: 1,000 (1%)
  • Legitimate transactions: 99,000 (99%)

Two models:

  • Model A (Lazy): Always predicts “legitimate” → 99.0% accuracy
  • Model B (Smart): Catches 80% of fraud, 2% false alarms → 97.8% accuracy

Which would you choose for your business?


Important

The Problem: Model A provides ZERO business value but has higher accuracy!

Understanding Classification Errors

The Confusion Matrix: Where Our Model Goes Wrong

Why These Errors Matter:

  • False Positives (FP) are harmful because:
    • Good customers get denied credit or charged higher rates
    • Customer frustration leads to churn and lost revenue
    • Wasted resources investigating “false alarms”
  • False Negatives (FN) are harmful because:
    • Risky customers get approved and actually default
    • Direct financial losses from unpaid debts
    • Missed opportunities to offer risk-appropriate pricing

Key Insight: Better performance metrics focus on the errors that matter most to your business!

Scenario 1: Medical Cancer Screening

You work for a hospital developing an AI system to screen for early-stage cancer.

Business Context:

  • System analyzes medical scans to flag potential cancer cases
  • Doctors use predictions to decide whether to order additional tests
  • Early detection dramatically improves patient outcomes

Error Costs:

  • False Positive: Healthy patient flagged as having cancer → $1,500 in unnecessary follow-up tests, patient anxiety
  • False Negative: Cancer patient marked as healthy → $50,000+ in delayed treatment costs, potentially life-threatening

Question?

Which error type should you prioritize minimizing? Why?

Answer: Minimize False Negatives - Missing cancer cases is life-threatening and costs 30x more than unnecessary tests. Better to have some false alarms than miss actual cancer.

Scenario 2: Credit Card Fraud Detection

You work for a bank building a fraud detection system for credit card transactions.

Business Context:

  • System analyzes transactions in real-time to flag potential fraud
  • Flagged transactions are automatically blocked from processing
  • Customer satisfaction and transaction volume are key business metrics

Error Costs:

  • False Positive: Legitimate transaction blocked → Angry customer at checkout, potential customer churn, lost transaction fees
  • False Negative: Fraudulent transaction approved → $200 average fraud loss (relatively small compared to transaction volume)

Question?

Which error type should you prioritize minimizing? Why?

Answer: Minimize False Positives - Blocking legitimate customers causes immediate frustration and churn. $200 fraud loss is small compared to losing a customer relationship.

Scenario 3: Marketing Campaign Targeting

You work for a company planning targeted email marketing campaigns for product promotions.

Business Context:

  • System predicts which customers are likely to purchase based on promotions
  • Marketing budget is limited, so targeting efficiency matters
  • Missing potential customers means lost sales opportunities

Error Costs:

  • False Positive: Send promotion to non-buyer → $2 wasted marketing cost per customer
  • False Negative: Miss a potential buyer → $5 lost profit opportunity per customer

Question?

Should you focus on minimizing false positives, false negatives, or balance both equally? Why?

Answer: Balance both errors - False negatives cost 2.5x more ($5 vs $2), but both matter for campaign ROI. Need efficiency AND coverage for marketing success.

Essential Classification Metrics

Beyond accuracy, we need metrics that align with business priorities:

  • Precision: Minimizes false positives (avoid angry customers)
  • Recall: Minimizes false negatives (catch critical cases)
  • F1-Score: Balances precision and recall (marketing efficiency)
  • ROC-AUC: Measures ranking quality (risk-based pricing)

Deep Dive in Your Reading

Each metric involves important mathematical formulas and trade-offs. Chapter 24 provides detailed explanations, examples, and business applications you’ll need for Thursday’s lab and upcoming assignments.

Focus on: confusion matrices, the derivative error metrics, and when to use each metric.

Choosing the Right Metric for Your Business

Metric Selection Framework

The key question: What business outcome are you optimizing?

  • Use PRECISION when:
    • False positives cost more than false negatives
    • Customer experience is critical
    • Example: Credit card fraud (don’t block legitimate purchases)
  • Use RECALL when:
    • False negatives cost more than false positives
    • Safety/compliance is critical
    • Example: Medical screening (don’t miss diseases)
  • Use F1-SCORE when:
    • Both error types matter equally
    • You need balanced performance
    • Example: Marketing targeting (efficiency + coverage)
  • Use ROC-AUC when:
    • You need to rank customers by risk
    • Stratification and pricing decisions
    • Example: Insurance premium tiers

Business Scenario Challenge

Your turn to be the expert! For each scenario…

Scenarios:

  1. Airport Security: TSA screening for dangerous items
  2. Job Resume Screening: Initial filter for qualified candidates
  3. Product Quality Control: Detecting defective products
  4. Customer Churn Prediction: Identify customers likely to cancel
  5. Medical Diagnosis: AI system supporting doctor decisions

For each:

Think about which errors you would want to minimize (or the evaluation metrics you’d want to use) and why?

Scenario Challenge: Answers

Business-aligned metric selection:

1. Airport SecurityRecall

  • Missing dangerous items = catastrophic
  • False alarms inconvenient but acceptable

2. Job Resume ScreeningPrecision

  • Don’t waste time interviewing unqualified candidates
  • Can always expand search if needed

3. Product Quality ControlRecall

  • Shipping defects damages brand reputation
  • Better to reject some good products than ship bad ones

4. Customer Churn PredictionF1-Score

  • Need both efficiency (precision) and coverage (recall)
  • Balance retention costs with intervention effectiveness

5. Medical DiagnosisRecall

  • Missing diseases can be life-threatening
  • Better to order unnecessary tests than miss conditions

Pattern:

Safety/compliance scenarios prioritize recall, efficiency scenarios prioritize precision, business optimization balances both.

Key Takeaways

  • Classification vs. Regression – Predicting categories requires different algorithms and evaluation approaches
  • Logistic Regression – The S-shaped logistic function transforms linear predictions into valid probabilities
  • Model Interpretation – Positive coefficients increase odds; larger coefficients = stronger effects
  • Beyond Accuracy – Business-focused metrics (precision, recall, F1, ROC-AUC) align with real costs and priorities

Connection to Thursday’s Lab

This Week’s Lab Preview

In Thursday’s lab, you’ll get hands-on practice with:

  • Building logistic regression models using real medical data to predict cancer
  • Applying proper data preparation techniques for classification
  • Evaluating models using precision, recall, F1-score, and ROC-AUC
  • Making business-aligned evaluation decisions based on error costs

Come prepared to apply today’s concepts in a medical diagnosis context!

Questions & Next Steps

Looking Ahead

Thursday Lab: Hands-On Classification with Medical Data

Homework: Thursday’s lab also serves as this week’s homework assignment

Any Final Questions?

  • About classification vs. regression?
  • About logistic regression interpretation?
  • About evaluation metrics?
  • About Thursday’s lab?

Office Hours & Resources

  • Office hours (aka Thursday lab): Available for additional support
  • Discussion board: Post questions about course topics
  • Lab materials: Available via Canvas
See you Thursday for hands-on classification analysis!