-
Notifications
You must be signed in to change notification settings - Fork 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
[Feature] WithoutLiersCV
model selection
#595
Open
FBruzzesi
wants to merge
2
commits into
koaning:main
Choose a base branch
from
FBruzzesi:feature/withoutliers
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.
+130
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,28 @@ | ||
import numpy as np | ||
import pytest | ||
from sklearn.model_selection import GroupKFold, GroupShuffleSplit, KFold, StratifiedKFold | ||
|
||
from sklego.model_selection import WithoutLiersCV | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"cv_strategy", [KFold(2), KFold(3, shuffle=True), StratifiedKFold(5), GroupKFold(2), GroupShuffleSplit(3)] | ||
) | ||
@pytest.mark.parametrize("anomalous_label", [-1, 1]) | ||
def test_split_without_anomalies(cv_strategy, anomalous_label): | ||
|
||
size = 1000 | ||
|
||
X = np.random.randn(size, 3) | ||
y = (np.random.randn(size) > 1.5).astype(int) | ||
groups = np.random.randint(0, 10, size) | ||
|
||
y[y == 1] = anomalous_label | ||
|
||
cv = WithoutLiersCV(cv_strategy, anomalous_label=anomalous_label) | ||
|
||
for inliner_index, test_index in cv.split(X, y, groups): | ||
y_train = y[inliner_index] | ||
assert np.all(y_train != anomalous_label) | ||
|
||
assert cv.get_n_splits(X, y, groups) == cv_strategy.get_n_splits(X, y, groups) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd want @MBrouns to weight in on the name 😅 just to make sure.
But I'm also wondering if it's perhaps easier to the enduser to not require an anomalous label ... wouldn't it perhaps be better to pass in an outlier model? this outlier model could then internally train on X and determine which items are outliers. Or am I overthinking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the conversation in the issue my understanding is slightly different. The goal of the CV is to validate anomaly detectors that do not train with different labels, namely the novelty detection ones. Therefore passing a novelty detection model would not be possible in the first place.
Now I agree that the name would suit both implementations 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@koaning Potentially we could have two CV strategies:
WithoutLiersCV
: takes any outlier detection model, train onX
, and excludes outliers fromtrain_indexes
NoveltyDetectorCV
: what's in this PR, to be able to train a novelty detection algorithm on non-anomalous labels and evaluate on both anomalous and not.