-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm_autoencoder7.py
75 lines (64 loc) · 2.57 KB
/
lstm_autoencoder7.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : lstm_autoencoder7.py
@Time : 2018/12/27 15:27:41
@Author : 靳卫华
@Version : 1.0
@Contact : [email protected]
@Desc : None
'''
# here put the import lib
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from library.plot_utils import visualize_reconstruction_error
from library.auto_encoder import LstmAutoEncoder7
DO_TRAINING = True
def main():
data_dir_path = 'data'
model_dir_path = 'model'
dateparser = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
satellite_data1 = pd.read_csv(
data_dir_path + '/data_std.csv',
sep=',',
index_col=0,
encoding='utf-8',
parse_dates=True,
date_parser=dateparser)
column = ['INA1_PCU输出母线电流','INA4_A电池组充电电流','INA2_A电池组放电电流','TNZ1PCU分流模块温度1','INZ6_-Y太阳电池阵电流','VNA2_A蓄电池整组电压','VNC1_蓄电池A单体1电压','VNZ2MEA电压(S3R)','VNZ4A组蓄电池BEA信号']
satellite_data = satellite_data1.loc[:,column].iloc[0:80]#96720
print(satellite_data.head())
satellite_np_data = satellite_data.as_matrix()
scaler = MinMaxScaler()
satellite_np_data = scaler.fit_transform(satellite_np_data)
print(satellite_np_data.shape)
index = satellite_data.index
columns = satellite_data.columns
time_window_size = 1
# data_std = pd.DataFrame(satellite_np_data, index=index, columns=columns)
# data_std.to_csv('data/data_scaler.csv', encoding='utf-8')
input_dataset = np.reshape(
satellite_np_data,
((int)(satellite_np_data.shape[0] / time_window_size),
time_window_size, satellite_np_data.shape[1]))
ae = LstmAutoEncoder7(index, columns)
# fit the data and save model into model_dir_path
if DO_TRAINING:
ae.fit(
input_dataset,
model_dir_path=model_dir_path,
time_window_size=time_window_size,
estimated_negative_sample_ratio=0.9)
# load back the model saved in model_dir_path detect anomaly
ae.load_model(model_dir_path)
anomaly_information = ae.anomaly(input_dataset)
reconstruction_error = []
for idx, (is_anomaly, dist) in enumerate(anomaly_information):
print('# ' + str(idx) + ' is ' +
('abnormal' if is_anomaly else 'normal') + ' (dist: ' +
str(dist) + ')')
reconstruction_error.append(dist)
visualize_reconstruction_error(reconstruction_error, ae.threshold)
if __name__ == '__main__':
main()