-
Notifications
You must be signed in to change notification settings - Fork 50
/
PCA_test.py
130 lines (119 loc) · 3.75 KB
/
PCA_test.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
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
import numpy as np
from sklearn.decomposition import PCA
import tensorflow as tf
import matplotlib.pyplot as plt
import DR_discriminator as dr
##########################################
##########################################
# -- SWaT data -- #
normal = np.load('./data/swat.npy')
anomaly = np.load('./data/swat_a.npy')
# # ALL SENSORS IDX
# # XS = [0, 1, 5, 6, 7, 8, 16, 17, 18, 25, 26, 27, 28, 33, 34, 35, 36, 37, 38, 39, 40, 41, 44, 45, 46, 47]
# # X_n = normal[21600:, XS]
# # X_a = anomaly[:, XS]
# # ALL VARIABLES
X_n = normal[21600:, 0:51]
X_a = anomaly[:, 0:51]
L_a = anomaly[:, 51]
############################################
## -- normalization -- ##
# for i in range(X_a.shape[1]):
# # print('i=', i)
# A = max(X_a[:, i])
# # print('A=', A)
# if A != 0:
# X_a[:, i] /= max(X_a[:, i])
# X_a[:, i] = 2*X_a[:, i] - 1
# else:
# X_a[:, i] = X_a[:, i]
# for i in range(X_n.shape[1]):
# # print('i=', i)
# B = max(X_n[:, i])
# # print('B=', B)
# if B != 0:
# X_n[:, i] /= max(X_n[:, i])
# X_n[:, i] = 2*X_n[:, i] - 1
# else:
# X_n[:, i] = X_n[:, i]
# print('normalization finished...')
# X = np.concatenate((X_n, X_a), axis=0)
# print(np.isnan(X))
#############################################
pca = PCA(n_components=10, svd_solver='full')
pca.fit(X_n)
ex_var = pca.explained_variance_ratio_
pc = pca.components_
# plt.plot(ex_var, 'b')
# plt.show()
pca_a = PCA(n_components=10, svd_solver='full')
pca_a.fit(X_a)
pc_a = pca_a.components_
# pca_x = PCA(n_components=10, svd_solver='full')
# pca_x.fit(X)
# ex_var_x = pca.explained_variance_ratio_
# pc_x = pca_x.components_
# # plt.plot(ex_var_x, 'b')
# # plt.show()
#############################################
# PC distribution
# A = 10
# pca = PCA(n_components=A, svd_solver='full')
# pca.fit(X_n)
# ex_var = pca.explained_variance_ratio_
# xpoints = range(1, A + 1)
#
# fig, ax = plt.subplots(figsize=(6, 3))
#
# ax.plot(xpoints, ex_var)
# ax.grid(linestyle='--', linewidth=0.5)
# # ax.set_ylim(0, 1)
# ax.set_title('wadi.pca.explained_variance_ratio')
# ax.xaxis.set_ticks(range(1, A + 1, int(A / 10)))
# fig.savefig("./Figs/PC_rate_wadi.png")
# plt.clf()
# plt.close()
# fist four componenta
# pc = pc[0:4, :]
#############################################
def SPE(X, pc):
a = X.shape[0]
b = X.shape[1]
spe = np.empty([a])
# Square Prediction Error (square of residual distance)
# spe = X'(I-PP')X
I = np.identity(b, float) - np.matmul(pc.transpose(1, 0), pc)
# I = np.matmul(I, I)
for i in range(a):
x = X[i, :].reshape([b, 1])
y = np.matmul(x.transpose(1, 0), I)
spe[i] = np.matmul(y, x)
return spe
spe_n = SPE(X_n, pc)
spe_a = SPE(X_a, pc)
# spe_x = SPE(X, pc_a)
Accu, Pre, Rec, F1, FPR = dr.CUSUM_det(spe_n, spe_a, L_a)
print('SPE_I:Accu: {:.4}; Pre: {:.4}; Rec: {:.4}; F1: {:.4}; FPR: {:.4}'.format(Accu, Pre, Rec, F1, FPR))
# f = open("./experiments/plots/Measures_baseline.txt", "a")
# f.write('PCA-SPE:Accu: {:.4}; Pre: {:.4}; Rec: {:.4}; F1: {:.4}; FPR: {:.4}\n'.format(Accu, Pre, Rec, F1, FPR))
# f.close()
#
# # projected values on the principal component
# # T = XP
# T_n = np.matmul(X_n, pc.transpose(1, 0))
# T_a = np.matmul(X_a, pc.transpose(1, 0))
# T_x = np.matmul(X, pc_x.transpose(1, 0))
#
# # projected values
# plt.plot(T_n[0:14400:100], 'b')
# plt.plot(T_a[0:14400:100], 'r')
# # plt.plot(T_x[496800:496800+14400:100], 'g')
# plt.plot(L_a[0:14400:100], 'k')
# plt.show()
#
# # SPE values
# plt.plot(spe_n[0:14400:100], 'b')
# plt.plot(spe_a[0:14400:100], 'r')
# # plt.plot(spe_x[496800:496800+14400:100], 'g')
# plt.plot(30*L_a[0:14400:100], 'k')
# plt.show()