F1-Optimal Threshold Selection with precision_recall_curve
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
You have binary labels `y_true` and the positive-class probabilities `y_proba` from a logistic regression model. Write `f1_optimal_threshold(y_true, y_proba)` returning, as a Python `float`, the threshold that maximises F1.
`precision_recall_curve` returns `precision` and `recall` of length `n_thresholds + 1`: the final point (precision=1.0, recall=0.0) has no threshold. F1 computed over the full arrays therefore has one more entry than `thresholds`, and an index that lands on that last point raises `IndexError`.
Requirements:
- Call `precision_recall_curve(y_true, y_proba)` to get `precision`, `recall`, `thresholds`.
- Slice `precision[:-1]` and `recall[:-1]` (or clip the index) before the argmax.
- Compute `f1 = 2 * p * r / (p + r)`, with `p + r == 0` giving `0.0` (never `nan`).
- Return `float(thresholds[ix])` where `ix = np.argmax(f1)`.
Example:
```python
y_true = np.array([0, 1, 0, 1, 1, 0])
y_proba = np.array([0.1, 0.3, 0.35, 0.6, 0.8, 0.9])
f1_optimal_threshold(y_true, y_proba) # -> 0.3
```
At threshold 0.3, five examples score at or above it, with 3 true positives and 2 false positives, so precision is 0.6, recall is 1.0 and F1 is 0.75, the highest of the six thresholds. Here `thresholds` has 6 entries and `precision` and `recall` have 7.
```hint Order of thresholds
Check whether `thresholds` comes back in ascending or descending order. It decides which threshold `np.argmax` returns when two points tie.
```
```hint A case worth testing
Build an input whose highest score belongs to a negative example and compute F1 at that threshold by hand, with and without the guard.
```
### Constraints & Assumptions
- `y_true` and `y_proba` are 1-D arrays of the same length, and `y_true` holds 0/1 labels with at least one positive.
- `y_proba` holds the predicted probability of class 1.
- An example counts as predicted positive when its score is at or above the threshold, as in `precision_recall_curve`.
- Use scikit-learn's `precision_recall_curve` with its default arguments.
### What a Strong Answer Covers
- Why `precision` and `recall` are one entry longer than `thresholds`, and how the code keeps the three arrays aligned.
- When `p + r` is zero, what the unguarded division produces, and how that value misleads `np.argmax`.
- Which threshold is returned when several share the best F1.
- Returning a built-in `float`, and the time and space cost.
### Follow-up Questions
- Why does a `nan` in `f1` break the selection?
- How would you break ties when multiple thresholds share the max F1?
- Why can a threshold tuned on the training data overstate F1, and which data should you tune it on?
Overview: Write a Python function that returns the probability threshold with the highest F1 score for a logistic regression model, using scikit-learn's precision_recall_curve. It tests keeping the precision, recall and threshold arrays aligned and handling thresholds where precision and recall are both zero.