Improve type annotations in sklearn.metrics._regression#357
Merged
debonte merged 5 commits intomicrosoft:mainfrom May 30, 2025
Merged
Improve type annotations in sklearn.metrics._regression#357debonte merged 5 commits intomicrosoft:mainfrom
sklearn.metrics._regression#357debonte merged 5 commits intomicrosoft:mainfrom
Conversation
Various sklearn metrics return floats or ndarrays based on the value of `multioutput` parameter. This commit adds overloads for the separate paths.
Various sklearn metrics return either a standard Python float, or a numpy flating point scalar type. E.g. ``` >>> import numpy as np >>> from sklearn.metrics import mean_absolute_error, median_absolute_error >>> a = np.array([1,2,3]) >>> b = np.array([4,5,6]) >>> type(mean_absolute_error(a,b)) float >>> type(median_absolute_error(a,b)) numpy.float64 ``` This commit fixes the type annotations for the following functions: - `mean_absolute_error` - `mean_absolute_percentage_error` - `mean_squared_error` - `r2_score` - `mean_tweedie_deviance` - `d2_pinball_score` - `d2_absolute_error_score`
The docs say float or ndarray but there is not ndarray return path.
Contributor
Author
|
@microsoft-github-policy-service agree |
Contributor
|
@matkozak, thanks for the contribution! Can you please add some unit tests for these overloads similar to https://github.com/microsoft/python-type-stubs/blob/main/tests/sklearn/preprocessing_tests.py? |
debonte
approved these changes
May 30, 2025
Originally these were not overloads, but now they are.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve type annotations in scikit-learn's regression metrics to more accurately represent the relationship between input parameters and return types. Specifically:
ndarraywhenmultioutput="raw_values"and floats for other multioutput options.floatfor functions that explicitly convert their result withfloat(result),Floattype alias for functions that return NumPy floating-point types (np.float64, etc.)d2_tweedie_scorereturn type which claims to return "float or ndarray of floats" in the docstring but has no code path that returns anndarray.