Skip to content

Fix:Fix/average precision score single sample 30615 - #1

Merged
Shyanil merged 4 commits into
mainfrom
fix/average-precision-score-single-sample-30615
Jan 12, 2025
Merged

Shyanil merged 4 commits into
mainfrom
fix/average-precision-score-single-sample-30615

Conversation

@Shyanil

@Shyanil Shyanil commented Jan 12, 2025

Copy link
Copy Markdown
Owner

Fixes scikit-learn#30615

Fix for Issue scikit-learn#30615 - average_precision_score with single sample

Solution

I solved this by adding a validation check in the _binary_uninterpolated_average_precision function to ensure there are at least two samples before attempting to calculate the average precision score.

Implementation

def _binary_uninterpolated_average_precision(
    y_true, y_score, pos_label=1, sample_weight=None
):
    # Add validation for minimum samples
    if len(y_true) < 2:
              raise ValueError(
                  f"Average precision requires at least 2 samples. Got {len(y_true)}."
                  " A single sample cannot form a precision-recall curve."
              )
    
    precision, recall, _ = precision_recall_curve(
        y_true, y_score, pos_label=pos_label, sample_weight=sample_weight
    )
    # Return the step function integral
    return max(0.0, -np.sum(np.diff(recall) * np.array(precision)[:-1]))

Changes

  • Added input validation to check for minimum number of samples
  • Improved error message that explains why single sample computation is not possible
  • Follows scikit-learn's error messaging conventions

Let me clearly explain the changes in a before/after format to show exactly what was modified in the codebase.

BEFORE (Original Code):

def _binary_uninterpolated_average_precision(
    y_true, y_score, pos_label=1, sample_weight=None
):
    precision, recall, _ = precision_recall_curve(
        y_true, y_score, pos_label=pos_label, sample_weight=sample_weight
    )
    # Return the step function integral
    return max(0.0, -np.sum(np.diff(recall) * np.array(precision)[:-1]))

AFTER (Fixed Code):

def _binary_uninterpolated_average_precision(
    y_true, y_score, pos_label=1, sample_weight=None
):
    # Add validation for minimum samples
    if len(y_true) < 2:
              raise ValueError(
                  f"Average precision requires at least 2 samples. Got {len(y_true)}."
                  " A single sample cannot form a precision-recall curve."
              )
    
    precision, recall, _ = precision_recall_curve(
        y_true, y_score, pos_label=pos_label, sample_weight=sample_weight
    )
    # Return the step function integral
    return max(0.0, -np.sum(np.diff(recall) * np.array(precision)[:-1]))

The average_precision_score function was returning misleading results
(1.0 or 0.0) when given a single sample. Added validation to require
at least 2 samples and provide a clear error message.

Fixes scikit-learn#30615
The average_precision_score function was returning misleading results
(1.0 or 0.0) when given a single sample. Added validation to require
at least 2 samples and provide a clear error message.

Fixes scikit-learn#30615
The average_precision_score function was returning misleading results
(1.0 or 0.0) when given a single sample. Added validation to require
at least 2 samples and provide a clear error message.

Fixes scikit-learn#30615
The average_precision_score function was returning misleading results
(1.0 or 0.0) when given a single sample. Added validation to require
at least 2 samples and provide a clear error message.

Fixes scikit-learn#30615
@Shyanil
Shyanil merged commit 7778785 into main Jan 12, 2025
@Shyanil
Shyanil deleted the fix/average-precision-score-single-sample-30615 branch January 12, 2025 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

average_precision_score produces unexpected output when scoring a single sample

1 participant