Compare test AUC with accuracy at two thresholds for a logistic regression

Quick Overview

Fit a pinned scikit-learn logistic regression and report test ROC AUC plus accuracy at thresholds of 0.5 and 0.2. It tests why a ranking metric ignores the decision threshold while accuracy depends on it, and how to choose an operating point.

Compare test AUC with accuracy at two thresholds for a logistic regression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `auc_is_threshold_independent(X_train, y_train, X_test, y_test)`. Fit `LogisticRegression(solver='lbfgs', penalty='l2', C=1.0, max_iter=1000, tol=1e-6)` on the training data and return the 3-tuple `(auc, acc_at_05, acc_at_02)` of Python floats for the test set. - `auc` is the ROC AUC of `predict_proba(X_test)[:, 1]`. - `acc_at_05` and `acc_at_02` are the accuracies of labels thresholded at 0.5 and 0.2 using `p >= t`. - Return all three as Python `float` values. - Results are checked with `assert_allclose(rtol=1e-5)`, and `acc_at_05 != acc_at_02` is asserted. ```hint What each metric reads Compute all three metrics from the same probability vector, then ask which of them would change if every probability went through a strictly increasing function. ``` ### Constraints & Assumptions - The grader uses fixed 2-feature arrays: 20 training rows and 8 test rows, with both classes present and the data not perfectly separable. - Labels are assumed to be 0 and 1, so column 1 of `predict_proba` is the positive class. ### What a Strong Answer Covers - A model built with exactly the pinned hyperparameters and fit once on the training data. - AUC computed from the continuous positive-class probabilities, with no threshold applied. - Both accuracies computed with the `p >= t` rule and returned as Python floats. - Why moving the threshold changes accuracy while the AUC stays the same. ### Follow-up Questions - When is a high AUC misleading despite poor real-world performance? - How would you pick the operating threshold instead of defaulting to 0.5? - For which test sets would the accuracies at 0.5 and 0.2 come out equal?

Overview: Fit a pinned scikit-learn logistic regression and report test ROC AUC plus accuracy at thresholds of 0.5 and 0.2. It tests why a ranking metric ignores the decision threshold while accuracy depends on it, and how to choose an operating point.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement auc_is_threshold_independent(X_train, y_train, X_test, y_test). Fit LogisticRegression(solver='lbfgs', penalty='l2', C=1.0, max_iter=1000, tol=1e-6) on the training data and return the 3-tuple (auc, acc_at_05, acc_at_02) of Python floats for the test set.

  • auc is the ROC AUC of predict_proba(X_test)[:, 1] .
  • acc_at_05 and acc_at_02 are the accuracies of labels thresholded at 0.5 and 0.2 using p >= t .
  • Return all three as Python float values.
  • Results are checked with assert_allclose(rtol=1e-5) , and acc_at_05 != acc_at_02 is asserted.

Constraints & Assumptions

  • The grader uses fixed 2-feature arrays: 20 training rows and 8 test rows, with both classes present and the data not perfectly separable.
  • Labels are assumed to be 0 and 1, so column 1 of predict_proba is the positive class.

What a Strong Answer Covers Guidance

  • A model built with exactly the pinned hyperparameters and fit once on the training data.
  • AUC computed from the continuous positive-class probabilities, with no threshold applied.
  • Both accuracies computed with the p >= t rule and returned as Python floats.
  • Why moving the threshold changes accuracy while the AUC stays the same.

Follow-up Questions Guidance

  • When is a high AUC misleading despite poor real-world performance?
  • How would you pick the operating threshold instead of defaulting to 0.5?
  • For which test sets would the accuracies at 0.5 and 0.2 come out equal?