alibi_detectt.py
2.64 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.metrics import accuracy_score, confusion_matrix, f1_score, recall_score
from alibi_detect.od import SpectralResidual
from alibi_detect.utils.perturbation import inject_outlier_ts
from alibi_detect.utils.saving import save_detector, load_detector
from alibi_detect.utils.visualize import plot_instance_score, plot_feature_outlier_ts
import timesynth as ts
n_points = 10000
time_sampler = ts.TimeSampler(stop_time=n_points)
time_samples = time_sampler.sample_regular_time(num_points=n_points)
X = np.loadtxt("x_test.csv", delimiter=",", dtype=np.float32, encoding='UTF8', skiprows=1)
Y = np.loadtxt("Y_test.csv", delimiter=",", dtype=np.float32, encoding='UTF8', skiprows=1)
X = np.expand_dims(X, axis=1)
data = inject_outlier_ts(X, perc_outlier=10, perc_window=10, n_std=2, min_std=1.)
X_outlier, y_outlier, labels = data.data, data.target.astype(int), data.target_names
od = SpectralResidual(
threshold=None, # threshold for outlier score
window_amp=20, # window for the average log amplitude
window_local=20, # window for the average saliency map
n_est_points=20 # nb of estimated points padded to the end of the sequence
)
X_threshold = X_outlier[:10000, :]
od.infer_threshold(X_threshold, time_samples[:10000], threshold_perc=80)
print('New threshold: {:.4f}'.format(od.threshold))
od_preds = od.predict(X_outlier, time_samples, return_instance_score=True)
a,TP,FP,FN = 0,0,0,0
for i in range(10000):
if od_preds['data']['is_outlier'][i] == 0 and Y[i] == 0:
a +=1
if od_preds['data']['is_outlier'][i] == 1 and Y[i] == 1:
TP +=1
if od_preds['data']['is_outlier'][i] == 1 and Y[i] == 0:
FP +=1
if od_preds['data']['is_outlier'][i] == 0 and Y[i] == 1:
FN +=1
print(a, TP, FP, FN)
if TP == 0:
print("wrong model")
else:
Precision = TP / (TP + FP)
Recall = TP / (TP + FN)
F1 = 2 * ((Precision * Recall) / (Precision + Recall))
print(Precision, Recall, F1)
# y_pred = od_preds['data']['is_outlier']
# f1 = f1_score(y_outlier, y_pred)
# acc = accuracy_score(y_outlier, y_pred)
# rec = recall_score(y_outlier, y_pred)
# print('F1 score: {} -- Accuracy: {} -- Recall: {}'.format(f1, acc, rec))
# cm = confusion_matrix(y_outlier, y_pred)
# df_cm = pd.DataFrame(cm, index=labels, columns=labels)
# sns.heatmap(df_cm, annot=True, cbar=True, linewidths=.5)
# plt.show()
# plot_feature_outlier_ts(od_preds,
# X_outlier,
# od.threshold,
# window=(0, 200),
# t=time_samples,
# X_orig=X)