-
Notifications
You must be signed in to change notification settings - Fork 45
Add robust metric #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimotheeMathieu
wants to merge
16
commits into
scikit-learn-contrib:main
Choose a base branch
from
TimotheeMathieu:robust_metric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add robust metric #122
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b3639a9
add robust metric maker
ae055b0
add docstring
c43de90
fix doctring example
c266b21
add doc
81b0333
Add example and doc
8f829e9
make example executable and try another link
56cd4e7
add make_huber_metric to api doc
e141e8a
fix doc
834a00b
fix doc api
e7a006f
add more explanation and change names of variables
a48bc1b
add test robust cv
4a4983d
Merge branch 'main' of https://github.com/scikit-learn-contrib/scikit…
1e16ab2
add to changelog, add trimmed mean example to example
6f3e31d
Merge branch 'main' into robust_metric
TimotheeMathieu 495b852
black reformat
0a3843a
fix docstring
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
================================================================ | ||
An example of a robust cross-validation evaluation in regression | ||
================================================================ | ||
In this example we compare `LinearRegression` (OLS) with `HuberRegressor` from | ||
scikit-learn using cross-validation. | ||
|
||
We show that a robust cross-validation scheme gives a better | ||
evaluation of the generalisation error in a corrupted dataset. | ||
|
||
In this example, we do robust cross-validation by using an alternative to the | ||
empirical mean to aggregate the errors. This alternative is a robust estimator | ||
of the mean (the trimmed mean is an example of such a robust estimator, but here | ||
we use Huber's estimator). This robust estimator of the mean is used on each | ||
fold of the cross-validation and then, we return the empirical mean of the | ||
obtained robust scores to get the final score. | ||
""" | ||
print(__doc__) | ||
|
||
import numpy as np | ||
from sklearn.metrics import mean_squared_error, make_scorer | ||
from sklearn.model_selection import cross_val_score | ||
from sklearn_extra.robust import make_huber_metric | ||
from sklearn.linear_model import LinearRegression, HuberRegressor | ||
|
||
robust_mse = make_huber_metric(mean_squared_error, c=9) | ||
rng = np.random.RandomState(42) | ||
|
||
X = rng.uniform(size=100)[:, np.newaxis] | ||
y = 3 * X.ravel() | ||
# Remark y <= 3 | ||
|
||
y[[42 // 2, 42, 42 * 2]] = 200 # outliers | ||
|
||
print("Non robust error:") | ||
for reg in [LinearRegression(), HuberRegressor()]: | ||
print( | ||
reg, | ||
" mse : %.2F" | ||
% ( | ||
np.mean( | ||
cross_val_score( | ||
reg, X, y, scoring=make_scorer(mean_squared_error) | ||
) | ||
) | ||
), | ||
) | ||
|
||
|
||
print("\n") | ||
print("Robust error:") | ||
for reg in [LinearRegression(), HuberRegressor()]: | ||
print( | ||
reg, | ||
" mse : %.2F" | ||
% ( | ||
np.mean( | ||
cross_val_score(reg, X, y, scoring=make_scorer(robust_mse)) | ||
) | ||
), | ||
) |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.