Repository navigation
Add Deep SAD (Deep Semi-Supervised Anomaly Detection) detector #716
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
JayeshSuryavanshi
wants to merge
9
commits into
yzhao062:development
Choose a base branch
from
JayeshSuryavanshi:feat/deep-sad
base: development
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
11f0649
Add Deep SAD (Deep Semi-Supervised Anomaly Detection) detector
JayeshSuryavanshi feb561a
Address review: allow wide encoder layers and validate the center
JayeshSuryavanshi 148f05f
Address Codex review: restore the best epoch, expand scalar centers, …
JayeshSuryavanshi 3b1c56c
Address Codex review: use validation_size for best-epoch selection, a…
JayeshSuryavanshi 61e36be
Address Codex review: stratify the validation split, fit the scaler o…
JayeshSuryavanshi ab8c233
Update the author email in the DeepSAD header comments
JayeshSuryavanshi 2e362c3
Check fitted state in DeepSAD.decision_function and seed at fit time
JayeshSuryavanshi ff1216a
Merge remote-tracking branch 'origin/development' into feat/deep-sad
JayeshSuryavanshi aa6da4e
Register DeepSAD in the knowledge base as experimental so ADEngine do…
JayeshSuryavanshi 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
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,64 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Example of using Deep SAD for semi-supervised outlier detection | ||
| """ | ||
| # Author: Jayesh Suryavanshi <jayeshksuryavanshi@gmail.com> | ||
| # License: BSD 2 clause | ||
|
|
||
| from __future__ import division | ||
| from __future__ import print_function | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| import numpy as np | ||
|
|
||
| # temporary solution for relative imports in case pyod is not installed | ||
| # if pyod is installed, no need to use the following line | ||
| sys.path.append( | ||
| os.path.abspath(os.path.join(os.path.dirname("__file__"), '..'))) | ||
|
|
||
| from pyod.models.deep_sad import DeepSAD | ||
| from pyod.utils.data import generate_data | ||
| from pyod.utils.data import evaluate_print | ||
|
|
||
| if __name__ == "__main__": | ||
| contamination = 0.1 # percentage of outliers | ||
| n_train = 20000 # number of training points | ||
| n_test = 2000 # number of testing points | ||
| n_features = 300 # number of features | ||
| random_state = 10 # if c is set to None use random_state | ||
|
|
||
| # Generate sample data | ||
| X_train, X_test, y_train, y_test = \ | ||
| generate_data(n_train=n_train, | ||
| n_test=n_test, | ||
| n_features=n_features, | ||
| contamination=contamination, | ||
| random_state=42) | ||
|
|
||
| # Deep SAD is semi-supervised: reveal a fraction of the training | ||
| # anomalies as labeled (1) and keep the rest unlabeled (0). | ||
| semi_y = np.zeros(n_train, dtype=int) | ||
| anomaly_idx = np.where(y_train == 1)[0] | ||
| revealed = anomaly_idx[:len(anomaly_idx) // 2] | ||
| semi_y[revealed] = 1 | ||
|
|
||
| # train Deep SAD detector | ||
| clf_name = 'DeepSAD' | ||
| clf = DeepSAD(n_features=n_features, epochs=5, | ||
| contamination=contamination, random_state=random_state) | ||
| clf.fit(X_train, semi_y) | ||
|
|
||
| # get the prediction labels and outlier scores of the training data | ||
| y_train_pred = clf.labels_ # binary labels (0: inliers, 1: outliers) | ||
| y_train_scores = clf.decision_scores_ # raw outlier scores | ||
|
|
||
| # get the prediction on the test data | ||
| y_test_pred = clf.predict(X_test) # outlier labels (0 or 1) | ||
| y_test_scores = clf.decision_function(X_test) # outlier scores | ||
|
|
||
| # evaluate and print the results | ||
| print("\nOn Training Data:") | ||
| evaluate_print(clf_name, y_train, y_train_scores) | ||
| print("\nOn Test Data:") | ||
| evaluate_print(clf_name, y_test, y_test_scores) |
Oops, something went wrong.
Oops, something went wrong.
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.