-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmo_gaal_example.py
60 lines (48 loc) · 2.08 KB
/
mo_gaal_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""Example of using Multiple-Objective Generative Adversarial Active
Learning (MO_GAAL) for outlier detection
"""
# Author: Winston Li <[email protected]>
# License: BSD 2 clause
from __future__ import division
from __future__ import print_function
import os
import sys
import torch
# 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.mo_gaal import MO_GAAL
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 = 30000 # number of training points
n_test = 3000 # number of testing points
n_features = 300 # number of features
# 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)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
X_train = torch.tensor(X_train, dtype=torch.float32).to(device).cpu().numpy()
X_test = torch.tensor(X_test, dtype=torch.float32).to(device).cpu().numpy()
# train MO_GAAL detector
clf_name = 'MO_GAAL'
clf = MO_GAAL(k=3, stop_epochs=2, contamination=contamination)
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
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)