Skip to content

Commit

Permalink
Merge fc76a49 into c2839e0
Browse files Browse the repository at this point in the history
  • Loading branch information
mbongaerts committed Jul 18, 2022
2 parents c2839e0 + fc76a49 commit e26e196
Show file tree
Hide file tree
Showing 4 changed files with 829 additions and 39 deletions.
257 changes: 257 additions & 0 deletions examples/rgraph_example.ipynb

Large diffs are not rendered by default.

34 changes: 16 additions & 18 deletions examples/anogan_example.py → examples/rgraph_example.py
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
"""Example for Anomaly Detection with Generative Adversarial Networks (AnoGAN)
Paper: https://arxiv.org/pdf/1703.05921.pdf
Note, that this is another implementation of AnoGAN as the one from https://github.com/fuchami/ANOGAN
"""Example for R-graph
"""
# Author: Michiel Bongaerts (but not author of the AnoGAN method)
# Author: Michiel Bongaerts (but not author of the R-graph method)
# License: BSD 2 clause


Expand All @@ -18,39 +16,42 @@
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))

from pyod.models.anogan import AnoGAN
from pyod.models.rgraph import RGraph
from pyod.utils.data import generate_data
from pyod.utils.data import evaluate_print
from pyod.utils.example import visualize


if __name__ == "__main__":

contamination = 0.1 # percentage of outliers
n_train = 200 # number of training points
n_train = 100 # number of training points
n_test = 100 # number of testing points

# Generate sample data
X_train, X_test, y_train, y_test = generate_data(
n_train=n_train,
n_test=n_test,
n_features=2,
n_features=70,
contamination=contamination,
behaviour="new",
random_state=42,
)

# train AutoEncoder detector
clf_name = 'AnoGAN'
clf = AnoGAN(G_layers=[10, 20], D_layers=[20, 2],
preprocessing=True, index_D_layer_for_recon_error=1,
epochs=200, contamination=contamination, verbose=1)

# train R-graph detector
clf_name = 'R-graph'
clf = RGraph(n_nonzero = 100, transition_steps = 20 , gamma = 50, blocksize_test_data = 20,
tau = 1, preprocessing=True, active_support = False, gamma_nz = False,
algorithm= 'lasso_lars', maxiter= 100, verbose =1 )

clf.fit(X_train)


# 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
# # 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

Expand All @@ -60,8 +61,5 @@
print("\nOn Test Data:")
evaluate_print(clf_name, y_test, y_test_scores)

# visualize the results
visualize(clf_name, X_train, y_train, X_test, y_test, y_train_pred,
y_test_pred, show_figure=True, save_figure=False)

clf.plot_learning_curves(window_smoothening=30)

0 comments on commit e26e196

Please sign in to comment.