Convert predicted probabilities to 0/1 labels at a threshold

Quick Overview

Write a function that turns positive-class probabilities from a classifier into 0/1 labels at a given threshold, with ties counted as positive. It tests vectorized NumPy, dtype handling, how the cutoff relates to scikit-learn's predict, and how error costs should move the threshold.

Convert predicted probabilities to 0/1 labels at a threshold

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `labels_at_threshold(y_proba, threshold)`, converting a 1-D float array `y_proba` of positive-class probabilities with shape `(n,)` (as from `predict_proba(X)[:, 1]`) into hard 0/1 labels. The function must follow these rules: - `label = 1 if p >= threshold else 0`. The comparison is inclusive, so `p == threshold` is positive. - Return a new `np.ndarray` of shape `(n,)` and dtype `int` (use `.astype(int)`), not booleans. - Do not modify `y_proba` in place. - `threshold` is a plain float in `[0, 1]`. For example: ```python y_proba = np.array([0.1, 0.5, 0.9]) labels_at_threshold(y_proba, 0.5) # array([0, 1, 1]) labels_at_threshold(y_proba, 0.9) # array([0, 0, 1]) ``` ```hint Think in arrays NumPy comparisons work on a whole array at once. Check which dtype a comparison returns. ``` ### Constraints & Assumptions - Tests use one fixed `y_proba` of 12 distinct values with thresholds 0.5, 0.3 and 0.9, checked with `assert_array_equal`. - `threshold` is a float in `[0, 1]`. ### What a Strong Answer Covers - An inclusive comparison, so a probability equal to the threshold is labeled 1. - A vectorized implementation that returns a new integer array and leaves the input unchanged. - How the result relates to calling `predict` on a scikit-learn classifier, including what happens at exactly 0.5. - How the costs of the two error types should move the threshold away from 0.5. ### Follow-up Questions - How would you choose a threshold when false negatives cost 5x as much as false positives? - How would you vectorize this without an explicit loop? - What changes if the model's probabilities are not well calibrated?

Overview: Write a function that turns positive-class probabilities from a classifier into 0/1 labels at a given threshold, with ties counted as positive. It tests vectorized NumPy, dtype handling, how the cutoff relates to scikit-learn's predict, and how error costs should move the threshold.

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

Implement labels_at_threshold(y_proba, threshold), converting a 1-D float array y_proba of positive-class probabilities with shape (n,) (as from predict_proba(X)[:, 1]) into hard 0/1 labels.

The function must follow these rules:

  • label = 1 if p >= threshold else 0 . The comparison is inclusive, so p == threshold is positive.
  • Return a new np.ndarray of shape (n,) and dtype int (use .astype(int) ), not booleans.
  • Do not modify y_proba in place.
  • threshold is a plain float in [0, 1] .

For example:

y_proba = np.array([0.1, 0.5, 0.9])
labels_at_threshold(y_proba, 0.5)  # array([0, 1, 1])
labels_at_threshold(y_proba, 0.9)  # array([0, 0, 1])

Constraints & Assumptions

  • Tests use one fixed y_proba of 12 distinct values with thresholds 0.5, 0.3 and 0.9, checked with assert_array_equal .
  • threshold is a float in [0, 1] .

What a Strong Answer Covers Guidance

  • An inclusive comparison, so a probability equal to the threshold is labeled 1.
  • A vectorized implementation that returns a new integer array and leaves the input unchanged.
  • How the result relates to calling predict on a scikit-learn classifier, including what happens at exactly 0.5.
  • How the costs of the two error types should move the threshold away from 0.5.

Follow-up Questions Guidance

  • How would you choose a threshold when false negatives cost 5x as much as false positives?
  • How would you vectorize this without an explicit loop?
  • What changes if the model's probabilities are not well calibrated?