-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadfa_stide_plotly.py
More file actions
145 lines (106 loc) · 3.48 KB
/
adfa_stide_plotly.py
File metadata and controls
145 lines (106 loc) · 3.48 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 18 19:52:11 2018
=========utility functions in aomaly detection using deep learining=======
scipy_distance:
compute distances between two set of vectors using
scipy.spatial.distance
@author: Shariful
"""
import plotly.offline as py
import plotly.graph_objs as go
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import cufflinks as cf
print(__version__)
init_notebook_mode(connected=True)
cf.go_offline()
from scipy.spatial import distance
import pandas as pd
import numpy as np
from sklearn import metrics
from matplotlib import pyplot as plt
def plot_ROC(test_labels, test_predictions):
fpr, tpr, thresholds = metrics.roc_curve(
test_labels, test_predictions, pos_label=1)
auc = "%.2f" % metrics.auc(fpr, tpr)
title = 'ROC Curve, AUC = '+str(auc)
with plt.style.context(('ggplot')):
fig, ax = plt.subplots()
ax.plot(fpr, tpr, "#000099", label='ROC curve')
ax.plot([0, 1], [0, 1], 'k--', label='Baseline')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend(loc='lower right')
plt.title(title)
return fig
#====Loading data
trn_nml = pd.read_csv('/Users/Shariful/Documents/DataCamp/ADFA-LD(tf-idf)/train_normal.csv')
test_atk = pd.read_csv('/Users/Shariful/Documents/DataCamp/ADFA-LD(tf-idf)/test_attack.csv')
test_nml = pd.read_csv('/Users/Shariful/Documents/DataCamp/ADFA-LD(tf-idf)/test_normal.csv')
# ======set the threshold
val_test_nml = test_nml.head(1312) #30% test normal
dist_val_to_nrml = distance.cdist(val_test_nml, \
trn_nml, metric='euclidean')
dist_val_to_nrml = dist_val_to_nrml.mean(axis=1)
th0 = dist_val_to_nrml.mean()
dist_val_to_nrml = dist_val_to_nrml[dist_val_to_nrml <= 15]
th1 = dist_val_to_nrml.mean()
test_set = test_atk.append(test_nml.tail(len(test_nml)-1312))
test_labels = np.hstack((np.ones(len(test_atk), dtype = int), \
np.zeros(len(test_nml)-1312, dtype = int)))
dist_test = distance.cdist(test_set, trn_nml, metric='euclidean')
test_predictions = dist_test.mean(axis=1)
plot_ROC(test_labels, test_predictions)
#response0 = dist_test >= th0
#response1 = dist_test >= th1
#th0 = dist_val_to_nrml.mean()
##plotting mean of nomal dist on val set
#x_val = np.linspace(0, 1, len(dist_val_to_nrml))
## Create a trace
#trace = go.Scatter(
# x = x_val,
# y = dist_val_to_nrml
#)
#data = [trace]
#py.plot(data, 'scatter')
#==============================
#trn_nml_sub = trn_nml.iloc[1:5, 1:5]
#def linalg_norm(data):
# a, b = data
# return numpy.linalg.norm(a-b, axis=1)
#
#
#def sqrt_sum(data):
# a, b = data
# return numpy.sqrt(numpy.sum((a-b)**2, axis=1))
#def scipy_distance(data):
# a, b = data
# return list(map(distance.euclidean, a, b))
#
#
#a = (1, 2, 3)
#b = (4, 5, 6)
#dst = distance.euclidean(trn_nml, trn_nml)
#def mpl_dist(data):
# a, b = data
# return list(map(matplotlib.mlab.dist, a, b))
#
#
#def sqrt_einsum(data):
# a, b = data
# a_min_b = a - b
# return numpy.sqrt(numpy.einsum('ij,ij->i', a_min_b, a_min_b))
#
#
#perfplot.show(
# setup=lambda n: numpy.random.rand(2, n, 3),
# n_range=[2**k for k in range(20)],
# kernels=[linalg_norm, scipy_distance, mpl_dist, sqrt_sum, sqrt_einsum],
# logx=True,
# logy=True,
# xlabel='len(x), len(y)'
# )