Repository navigation
Conversation
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
…-sample-30615 Fix:Fix/average precision score single sample 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
| def _binary_uninterpolated_average_precision( | ||
| y_true, y_score, pos_label=1, sample_weight=None | ||
| ): | ||
| if y_true.shape[0] < 2: |
There was a problem hiding this comment.
Unit tests seem to fail because y_true can be a python list without a .shape argument (I think, I am not a scikit-learn maintainer)
| raise ValueError( | ||
| "Average precision requires at least 2 samples to compute a meaningful " | ||
| f"score, but got array with shape = {y_true.shape}" | ||
| ) |
There was a problem hiding this comment.
Probably want to add a new unit test that checks for the scenario when this error should occur.
There was a problem hiding this comment.
Thank you @Innixma for your kind response and for taking the time to review my code.
-
Regarding the unit test failures: It seems that
y_truecan sometimes be a Python list without a.shapeattribute. However, based on my understanding, scikit-learn usually converts lists to NumPy arrays internally. This automatic conversion is why I assumed.shapecould safely be used. -
Additionally, in the same file, there are examples of similar usage. For instance, in the
auc(x, y)function:if x.shape[0] < 2: raise ValueError( "At least 2 points are needed to compute area under curve, but x.shape = %s" % x.shape )
-
Similarly, in the
_binary_uninterpolated_average_precisionfunction:if y_true.shape[0] < 2: raise ValueError( "Average precision requires at least 2 samples to compute a meaningful " f"score, but got array with shape = {y_true.shape}" )
first example assume that
.shapeis available, which is why I followed this approach. -
Regarding your suggestion to add a unit test for this specific scenario: I completely agree and will work on creating a test to handle cases where this error might occur.
-
As for the pipeline failures (e.g.,
scikit-learn.scikit-learn Failing after 14m — Build #20250113.4andLinux_Runs pylatest_conda_forge_mkl failed), I will investigate further and refine the code. I may need to do additional research and adjustments before submitting an updated pull request. -
Lastly, I am new to contributing to scikit-learn, so I may make some mistakes as I learn. I truly appreciate your guidance and patience.
Thank you again for your feedback. I’ll address these points promptly.
There was a problem hiding this comment.
Sounds good, thanks for the detailed response!
|
Thanks for the PR @Shyanil but a decision hasn't been taken in the linked issue which is still labelled as "needs investigation". Therefore I'm closing this PR. Also, it looks a lot like fully LLM generated, see our https://scikit-learn.org/stable/developers/contributing.html#automated-contributions-policy |
Fixes #30615
Solution
The issue was resolved by introducing an additional validation check in the
_binary_uninterpolated_average_precisionfunction. This ensures that the function receives at least two samples (y_true) before attempting to compute the average precision score.Implementation Details
Key Changes:
Input Validation:
A validation check was added to verify that the input
y_truecontains at least two samples. If the condition is not met, aValueErroris raised with a descriptive error message.Error Messaging:
The error message is detailed and aligns with scikit-learn's conventions, explaining why computation with fewer than two samples is not meaningful.
Updated Code:
Key Improvements Over Original Code
Before:
No validation was performed on the number of samples, which could lead to runtime errors or misleading results when processing very small datasets.
After:
Added a validation step to prevent potential errors and provided a clear, actionable error message for users.
Additional Notes
@validate_paramsdecorator and related functions, enhancing robustness.