Compute ROC AUC and average precision for a logistic regression under class imbalance

Quick Overview

Fit a logistic regression on a heavily imbalanced dataset and report ROC AUC, average precision and the test positive rate with scikit-learn. It tests how each metric treats false positives and true negatives, what each one scores at random, and how to read the results when positives are rare.

Compute ROC AUC and average precision for a logistic regression under class imbalance

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `roc_vs_pr_under_imbalance(X_train, y_train, X_test, y_test)` on a fixed, heavily imbalanced dataset (about 5% positives). Fit a logistic regression, score the test set, and return `(roc_auc, average_precision, positive_rate)` as Python floats. - Fit `LogisticRegression(solver='lbfgs', penalty='l2', C=1.0, max_iter=1000, tol=1e-6)` on the training data. - Score with `predict_proba(X_test)[:, 1]`. - Compute the metrics with `roc_auc_score` and `average_precision_score`. - `positive_rate` is `y_test.mean()`. ```python roc_auc, ap, pos_rate = roc_vs_pr_under_imbalance(X_train, y_train, X_test, y_test) # roc_auc >> ap, and pos_rate ~ 0.05 is AP's random baseline assert roc_auc > ap ``` ```hint Look at the denominators Write the false positive rate and precision as fractions of confusion-matrix counts, and check which of them contains the true negatives. ``` ```hint Compare against chance Work out what each metric would be for a scorer that assigns scores at random, independent of the label. ``` ### Constraints & Assumptions - Labels are 0/1 with about 5% positives, and the test set contains both classes. - The model uses the hyperparameters exactly as given, with no class weights or resampling. - Values are checked with `assert_allclose(rtol=1e-5)`, and `roc_auc` must be substantially higher than `average_precision`. ### What a Strong Answer Covers - Fitting the pinned model, scoring with the positive-class probability column, and returning three Python floats. - How each metric is built from confusion-matrix counts, and which count each one leaves out. - The random baseline of each metric, and why average precision has to be read against the positive rate. - Why ROC AUC stays high on this data while average precision stays low, and what each number says about the model. ### Follow-up Questions - Why is average precision preferred over ROC AUC when positives are rare? - How would you turn the probability scores into predictions without a fixed 0.5 threshold? - If you retrained with `class_weight='balanced'`, how would you expect the two metrics to change?

Overview: Fit a logistic regression on a heavily imbalanced dataset and report ROC AUC, average precision and the test positive rate with scikit-learn. It tests how each metric treats false positives and true negatives, what each one scores at random, and how to read the results when positives are rare.

Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement roc_vs_pr_under_imbalance(X_train, y_train, X_test, y_test) on a fixed, heavily imbalanced dataset (about 5% positives). Fit a logistic regression, score the test set, and return (roc_auc, average_precision, positive_rate) as Python floats.

  • Fit LogisticRegression(solver='lbfgs', penalty='l2', C=1.0, max_iter=1000, tol=1e-6) on the training data.
  • Score with predict_proba(X_test)[:, 1] .
  • Compute the metrics with roc_auc_score and average_precision_score .
  • positive_rate is y_test.mean() .
roc_auc, ap, pos_rate = roc_vs_pr_under_imbalance(X_train, y_train, X_test, y_test)
# roc_auc >> ap, and pos_rate ~ 0.05 is AP's random baseline
assert roc_auc > ap

Constraints & Assumptions

  • Labels are 0/1 with about 5% positives, and the test set contains both classes.
  • The model uses the hyperparameters exactly as given, with no class weights or resampling.
  • Values are checked with assert_allclose(rtol=1e-5) , and roc_auc must be substantially higher than average_precision .

What a Strong Answer Covers Guidance

  • Fitting the pinned model, scoring with the positive-class probability column, and returning three Python floats.
  • How each metric is built from confusion-matrix counts, and which count each one leaves out.
  • The random baseline of each metric, and why average precision has to be read against the positive rate.
  • Why ROC AUC stays high on this data while average precision stays low, and what each number says about the model.

Follow-up Questions Guidance

  • Why is average precision preferred over ROC AUC when positives are rare?
  • How would you turn the probability scores into predictions without a fixed 0.5 threshold?
  • If you retrained with class_weight='balanced' , how would you expect the two metrics to change?