deepretro.utils.metrics

Utility helpers for model evaluation, in particular threshold optimisation for binary classification using the F1-score on the precision–recall curve.

Threshold optimization utilities for binary classification.

deepretro.utils.metrics.find_optimal_threshold(y_true, probabilities)[source]

Find the classification threshold that maximises F1-score.

Sweeps the precision-recall curve and picks the threshold where the harmonic mean of precision and recall is highest.

Parameters:
  • y_true (array-like, shape (n_samples,)) – True binary labels (0 or 1).

  • probabilities (array-like, shape (n_samples,)) – Predicted probabilities for the positive class.

Returns:

  • threshold (float) – Optimal classification threshold.

  • f1 (float) – F1-score at the optimal threshold.

Return type:

tuple[float, float]

Examples

>>> import numpy as np
>>> from deepretro.utils.metrics import find_optimal_threshold
>>> y = np.array([0, 0, 1, 1])
>>> proba = np.array([0.1, 0.4, 0.6, 0.9])
>>> thr, f1 = find_optimal_threshold(y, proba)
>>> 0.0 < thr < 1.0
True
>>> f1 > 0.0
True