Loading content...
In binary classification tasks, evaluating model performance requires balancing two often competing metrics: Precision (how accurate are the positive predictions?) and Recall (how many actual positives were captured?). The F-beta score provides an elegant solution by computing the weighted harmonic mean of these two metrics.
Before diving into the F-beta score, let's establish the foundational metrics:
Precision measures the proportion of positive predictions that are actually correct: $$\text{Precision} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Positives}}$$
Recall (also called Sensitivity or True Positive Rate) measures the proportion of actual positives that were correctly identified: $$\text{Recall} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Negatives}}$$
A simple arithmetic mean of Precision and Recall can be misleading because it doesn't properly penalize extremely low values in either metric. The harmonic mean addresses this by heavily penalizing cases where either Precision or Recall is very low—ensuring that a model cannot achieve a high score by excelling in only one dimension.
The F-beta score extends this concept by introducing a weighting parameter β (beta):
$$F_\beta = (1 + \beta^2) \cdot \frac{\text{Precision} \cdot \text{Recall}}{(\beta^2 \cdot \text{Precision}) + \text{Recall}}$$
Interpretation of β:
Common variants include F2 (β=2, recall-focused) and F0.5 (β=0.5, precision-focused).
Implement a function that computes the F-beta score for binary classification. Given arrays of actual labels and predicted labels, along with a beta weighting parameter, calculate and return the F-beta score rounded to three decimal places.
Edge Case Handling:
actual_labels = [1, 0, 1, 1, 0, 1]
predicted_labels = [1, 0, 1, 0, 0, 1]
beta_weight = 10.857Let's break down the calculation step by step:
Step 1: Build the Confusion Matrix • True Positives (TP) = 3 (positions 0, 2, 5 where both actual and predicted are 1) • True Negatives (TN) = 2 (positions 1, 4 where both are 0) • False Positives (FP) = 0 (no cases where actual is 0 but predicted is 1) • False Negatives (FN) = 1 (position 3 where actual is 1 but predicted is 0)
Step 2: Calculate Precision and Recall • Precision = TP / (TP + FP) = 3 / (3 + 0) = 1.0 • Recall = TP / (TP + FN) = 3 / (3 + 1) = 0.75
Step 3: Apply F-beta Formula with β = 1 • F1 = (1 + 1²) × (1.0 × 0.75) / ((1² × 1.0) + 0.75) • F1 = 2 × 0.75 / 1.75 = 1.5 / 1.75 ≈ 0.857
actual_labels = [1, 1, 1, 0, 0, 0]
predicted_labels = [1, 1, 0, 0, 0, 1]
beta_weight = 10.667Step 1: Build the Confusion Matrix • True Positives (TP) = 2 (positions 0, 1 where both are 1) • True Negatives (TN) = 2 (positions 3, 4 where both are 0) • False Positives (FP) = 1 (position 5 where actual is 0 but predicted is 1) • False Negatives (FN) = 1 (position 2 where actual is 1 but predicted is 0)
Step 2: Calculate Precision and Recall • Precision = TP / (TP + FP) = 2 / (2 + 1) = 0.667 • Recall = TP / (TP + FN) = 2 / (2 + 1) = 0.667
Step 3: Apply F-beta Formula with β = 1 • F1 = 2 × (0.667 × 0.667) / (0.667 + 0.667) • F1 = 2 × 0.444 / 1.334 ≈ 0.667
When Precision equals Recall, the F1-score equals both.
actual_labels = [1, 1, 1, 1, 0, 0]
predicted_labels = [1, 0, 1, 1, 0, 1]
beta_weight = 20.75Step 1: Build the Confusion Matrix • True Positives (TP) = 3 (positions 0, 2, 3 where both are 1) • True Negatives (TN) = 1 (position 4 where both are 0) • False Positives (FP) = 1 (position 5 where actual is 0 but predicted is 1) • False Negatives (FN) = 1 (position 1 where actual is 1 but predicted is 0)
Step 2: Calculate Precision and Recall • Precision = TP / (TP + FP) = 3 / (3 + 1) = 0.75 • Recall = TP / (TP + FN) = 3 / (3 + 1) = 0.75
Step 3: Apply F-beta Formula with β = 2 • F2 = (1 + 2²) × (0.75 × 0.75) / ((2² × 0.75) + 0.75) • F2 = 5 × 0.5625 / (3 + 0.75) = 2.8125 / 3.75 = 0.75
With β = 2, Recall is weighted more heavily, but since Precision and Recall are equal here, the result matches both values.
Constraints