forked from yuqiChen94/Swat_Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_process.py
70 lines (54 loc) · 2.15 KB
/
data_process.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
import numpy as np
import os
def normal_data_process(normal_path, interval):
normal_vector = []
normal_data_set = np.load(normal_path, allow_pickle=True).tolist()
for i in range(0, len(normal_data_set) - interval):
vector = normal_data_set[i] + normal_data_set[i + interval]
normal_vector.append(vector)
return normal_vector
def data_process(data_path, normal_path, interval):
normal_data_set = normal_data_process(normal_path, interval)
abnormal_vector = []
files = os.listdir(data_path)
for file in files:
path = data_path + "/" + file
if path != normal_path:
abnormal_data_set = np.load(path, allow_pickle=True).tolist()
for i in range(0, len(abnormal_data_set) - interval):
vector = abnormal_data_set[i] + abnormal_data_set[i + interval]
abnormal_vector.append(vector)
return normal_data_set, abnormal_vector
def undersample(normal_data_set, abnormal_data_set):
new_set = []
t = round(len(abnormal_data_set) / len(normal_data_set))
for i in range(0, len(abnormal_data_set)):
if i % t == 0:
new_set.append(abnormal_data_set[i])
return new_set
def label(normal_data_set, abnormal_data_set):
x_path = "training_data/x.npy"
y_path = "training_data/y.npy"
y = []
for i in range(0, len(normal_data_set)):
y.append(1)
for i in range(0, len(abnormal_data_set)):
y.append(0)
x = np.array(normal_data_set + abnormal_data_set)
y = np.array(y)
print(x.shape)
print(y.shape)
x.dump(x_path)
y.dump(y_path)
interval = 100
normal_path = "data/normal.npy"
data_path = "data"
normal_data_set_path = "training_data/normal.npy"
abnormal_data_set_path = "training_data/abnormal.npy"
normal_data_set, abnormal_data_set = data_process(data_path, normal_path, interval)
new_abnormal_data_set = undersample(normal_data_set, abnormal_data_set)
label(normal_data_set, new_abnormal_data_set)
normal_data_set = np.array(normal_data_set)
normal_data_set.dump(normal_data_set_path)
new_abnormal_data_set = np.array(new_abnormal_data_set)
new_abnormal_data_set.dump(abnormal_data_set_path)