Pick the classification threshold that maximizes Youden's J on an ROC curve

Quick Overview

Implement a function that builds an ROC curve from predicted probabilities and returns the decision threshold with the largest Youden's J statistic. It tests how scikit-learn produces candidate thresholds, how ties are broken, and how to handle a model that performs no better than chance.

Pick the classification threshold that maximizes Youden's J on an ROC curve

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `youden_threshold(y_true, y_proba)`. Compute the ROC curve of the predicted probabilities, form Youden's `J = tpr - fpr` at every operating point, and return the threshold that maximizes J as a Python `float`. The function must: - Use `roc_curve(y_true, y_proba, drop_intermediate=False)`, which is required to match the reference thresholds. - Take `ix = int(np.argmax(J))` and return `float(thresholds[ix])`. - Break ties by taking the first index, which is the highest threshold. Index 0 of the ROC arrays is sklearn's artificial `(0, 0)` point, with threshold `np.inf` and J = 0. It is selected only if the largest J is at most 0. For example: ```python youden_threshold([0, 0, 1, 1], [0.1, 0.4, 0.35, 0.8]) # -> 0.8 youden_threshold([0, 1, 0, 1, 1, 0, 1], [0.15, 0.3, 0.4, 0.55, 0.62, 0.7, 0.85]) # -> 0.55 ``` In the first call, the thresholds 0.8 and 0.35 both reach J = 0.5, and the tie rule picks the higher one. In the second, a cut at 0.55 flags 3 of the 4 positives and 1 of the 3 negatives, so J = 0.75 - 1/3 (about 0.417), the largest value on the curve. ```hint Threshold direction Check which rule `roc_curve` uses to turn a threshold into predictions (strictly above, or at or above) and in which order it returns the thresholds. The tie rule depends on that order. ``` ### Constraints & Assumptions - `y_true` is a 1-D array of 0/1 labels and `y_proba` is a 1-D float array of the same length. - The grader uses a fixed 12-element input and compares the result with `assert_allclose(rtol=1e-9)`. Its best J is 2/3, so the artificial index-0 point cannot win there. ### What a Strong Answer Covers - What J measures on the ROC plot and why its maximum is a reasonable default cut. - How the candidate thresholds are produced, including the effect of `drop_intermediate`. - The tie-breaking order and how it follows from the threshold order. - The artificial first point and what the function returns for a model at or below chance. - Running time and memory. ### Follow-up Questions - How would you extend this to a cost-sensitive threshold instead of plain J? - Why can returning `np.inf` happen, and how would you guard against it? - Why should the threshold be chosen on held-out predictions and not on the data the model was fit on?

Overview: Implement a function that builds an ROC curve from predicted probabilities and returns the decision threshold with the largest Youden's J statistic. It tests how scikit-learn produces candidate thresholds, how ties are broken, and how to handle a model that performs no better than chance.

Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement youden_threshold(y_true, y_proba). Compute the ROC curve of the predicted probabilities, form Youden's J = tpr - fpr at every operating point, and return the threshold that maximizes J as a Python float.

The function must:

  • Use roc_curve(y_true, y_proba, drop_intermediate=False) , which is required to match the reference thresholds.
  • Take ix = int(np.argmax(J)) and return float(thresholds[ix]) .
  • Break ties by taking the first index, which is the highest threshold.

Index 0 of the ROC arrays is sklearn's artificial (0, 0) point, with threshold np.inf and J = 0. It is selected only if the largest J is at most 0.

For example:

youden_threshold([0, 0, 1, 1], [0.1, 0.4, 0.35, 0.8])  # -> 0.8
youden_threshold([0, 1, 0, 1, 1, 0, 1],
                 [0.15, 0.3, 0.4, 0.55, 0.62, 0.7, 0.85])  # -> 0.55

In the first call, the thresholds 0.8 and 0.35 both reach J = 0.5, and the tie rule picks the higher one. In the second, a cut at 0.55 flags 3 of the 4 positives and 1 of the 3 negatives, so J = 0.75 - 1/3 (about 0.417), the largest value on the curve.

Constraints & Assumptions

  • y_true is a 1-D array of 0/1 labels and y_proba is a 1-D float array of the same length.
  • The grader uses a fixed 12-element input and compares the result with assert_allclose(rtol=1e-9) . Its best J is 2/3, so the artificial index-0 point cannot win there.

What a Strong Answer Covers Guidance

  • What J measures on the ROC plot and why its maximum is a reasonable default cut.
  • How the candidate thresholds are produced, including the effect of drop_intermediate .
  • The tie-breaking order and how it follows from the threshold order.
  • The artificial first point and what the function returns for a model at or below chance.
  • Running time and memory.

Follow-up Questions Guidance

  • How would you extend this to a cost-sensitive threshold instead of plain J?
  • Why can returning np.inf happen, and how would you guard against it?
  • Why should the threshold be chosen on held-out predictions and not on the data the model was fit on?