-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG+1] Fix LOF and Isolation benchmarks #9798
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
67a85ec
fix lof bench
albertcthomas 5a0c557
fix iforest bench
albertcthomas 9825623
make LOF benchmark an outlier detection benchmark
albertcthomas 16804a1
remove predict_time for LOF as there is no predict on a test set
albertcthomas 809fbe0
add X.astype(float) that was inadvertently removed in a previous commit
albertcthomas 8696938
fix and clarify randomness in LOF benchmark
albertcthomas aaf9e51
fix and clarify randomness in iforest benchmark
albertcthomas 3e06c31
SEED -> random_state
albertcthomas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,16 @@ | |
|
||
A test of LocalOutlierFactor on classical anomaly detection datasets. | ||
|
||
Note that LocalOutlierFactor is not meant to predict on a test set and its | ||
performance is assessed in an outlier detection context: | ||
1. The model is trained on the whole dataset which is assumed to contain | ||
outliers. | ||
2. The ROC curve is computed on the same dataset using the knowledge of the | ||
labels. | ||
In this context there is no need to shuffle the dataset because the model | ||
is trained and tested on the whole dataset. The randomness of this benchmark | ||
is only caused by the random selection of anomalies in the SA dataset. | ||
|
||
""" | ||
|
||
from time import time | ||
|
@@ -14,31 +24,28 @@ | |
from sklearn.metrics import roc_curve, auc | ||
from sklearn.datasets import fetch_kddcup99, fetch_covtype, fetch_mldata | ||
from sklearn.preprocessing import LabelBinarizer | ||
from sklearn.utils import shuffle as sh | ||
|
||
print(__doc__) | ||
|
||
np.random.seed(2) | ||
random_state = 2 # to control the random selection of anomalies in SA | ||
|
||
# datasets available: ['http', 'smtp', 'SA', 'SF', 'shuttle', 'forestcover'] | ||
datasets = ['shuttle'] | ||
|
||
novelty_detection = True # if False, training set polluted by outliers | ||
datasets = ['http', 'smtp', 'SA', 'SF', 'shuttle', 'forestcover'] | ||
|
||
plt.figure() | ||
for dataset_name in datasets: | ||
# loading and vectorization | ||
print('loading data') | ||
if dataset_name in ['http', 'smtp', 'SA', 'SF']: | ||
dataset = fetch_kddcup99(subset=dataset_name, shuffle=True, | ||
percent10=False) | ||
dataset = fetch_kddcup99(subset=dataset_name, percent10=True, | ||
random_state=random_state) | ||
X = dataset.data | ||
y = dataset.target | ||
|
||
if dataset_name == 'shuttle': | ||
dataset = fetch_mldata('shuttle') | ||
X = dataset.data | ||
y = dataset.target | ||
X, y = sh(X, y) | ||
# we remove data with label 4 | ||
# normal data are then those of class 1 | ||
s = (y != 4) | ||
|
@@ -47,7 +54,7 @@ | |
y = (y != 1).astype(int) | ||
|
||
if dataset_name == 'forestcover': | ||
dataset = fetch_covtype(shuffle=True) | ||
dataset = fetch_covtype() | ||
X = dataset.data | ||
y = dataset.target | ||
# normal data are those with attribute 2 | ||
|
@@ -61,54 +68,34 @@ | |
|
||
if dataset_name == 'SF': | ||
lb = LabelBinarizer() | ||
lb.fit(X[:, 1]) | ||
x1 = lb.transform(X[:, 1]) | ||
x1 = lb.fit_transform(X[:, 1].astype(str)) | ||
X = np.c_[X[:, :1], x1, X[:, 2:]] | ||
y = (y != 'normal.').astype(int) | ||
y = (y != b'normal.').astype(int) | ||
|
||
if dataset_name == 'SA': | ||
lb = LabelBinarizer() | ||
lb.fit(X[:, 1]) | ||
x1 = lb.transform(X[:, 1]) | ||
lb.fit(X[:, 2]) | ||
x2 = lb.transform(X[:, 2]) | ||
lb.fit(X[:, 3]) | ||
x3 = lb.transform(X[:, 3]) | ||
x1 = lb.fit_transform(X[:, 1].astype(str)) | ||
x2 = lb.fit_transform(X[:, 2].astype(str)) | ||
x3 = lb.fit_transform(X[:, 3].astype(str)) | ||
X = np.c_[X[:, :1], x1, x2, x3, X[:, 4:]] | ||
y = (y != 'normal.').astype(int) | ||
y = (y != b'normal.').astype(int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. works with python2 and python3? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
|
||
if dataset_name == 'http' or dataset_name == 'smtp': | ||
y = (y != 'normal.').astype(int) | ||
|
||
n_samples, n_features = np.shape(X) | ||
n_samples_train = n_samples // 2 | ||
n_samples_test = n_samples - n_samples_train | ||
y = (y != b'normal.').astype(int) | ||
|
||
X = X.astype(float) | ||
X_train = X[:n_samples_train, :] | ||
X_test = X[n_samples_train:, :] | ||
y_train = y[:n_samples_train] | ||
y_test = y[n_samples_train:] | ||
|
||
if novelty_detection: | ||
X_train = X_train[y_train == 0] | ||
y_train = y_train[y_train == 0] | ||
|
||
print('LocalOutlierFactor processing...') | ||
model = LocalOutlierFactor(n_neighbors=20) | ||
tstart = time() | ||
model.fit(X_train) | ||
model.fit(X) | ||
fit_time = time() - tstart | ||
tstart = time() | ||
|
||
scoring = -model.decision_function(X_test) # the lower, the more normal | ||
predict_time = time() - tstart | ||
fpr, tpr, thresholds = roc_curve(y_test, scoring) | ||
scoring = -model.negative_outlier_factor_ # the lower, the more normal | ||
fpr, tpr, thresholds = roc_curve(y, scoring) | ||
AUC = auc(fpr, tpr) | ||
plt.plot(fpr, tpr, lw=1, | ||
label=('ROC for %s (area = %0.3f, train-time: %0.2fs,' | ||
'test-time: %0.2fs)' % (dataset_name, AUC, fit_time, | ||
predict_time))) | ||
label=('ROC for %s (area = %0.3f, train-time: %0.2fs)' | ||
% (dataset_name, AUC, fit_time))) | ||
|
||
plt.xlim([-0.05, 1.05]) | ||
plt.ylim([-0.05, 1.05]) | ||
|
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.
Now that the shuttle dataset is run by default, we can remove this comment.