-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_1_hammerstein.py
114 lines (103 loc) · 3.75 KB
/
type_1_hammerstein.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import GoogleTrend
import RoadSection
import bpnn
import dynamic_bpnn
import time
import csv
import math
from multiprocessing import Pool
import neural
from sklearn.metrics import mean_squared_error
# Load Data-----------------------------------------------------
gt = GoogleTrend.GoogleTrend('multiTimeline.csv')
gt.load()
gt.normalize()
rs = RoadSection.RoadSection('03F2614S-03F2709S.csv')
rs.load()
rs.normalize()
print(len(rs.flow_all))
print(len(gt.trend_percentage))
assert rs.really_start_day_index == gt.trend_start_day_index # 416
# Store to Pandas DataFrame Type-------------------------------------------
# dates = pd.date_range('20150101', periods=639)
# Normalize data 1 to -1
def normalize_all(series):
series_max = series.max()
series_min = series.min()
return pd.Series([(lambda x: 2 / (series_max - series_min) * (x - series_min) - 1)(x) for x in series])
df = pd.DataFrame({
'flow': normalize_all(np.array(rs.flow_all[rs.really_start_day_index:])),
'trend': normalize_all(np.array(gt.trend_percentage[rs.really_start_day_index:]))
},
# index=dates
)
for day in [2]:
# Type 1 Neural Network---------------------------------------------------------------
point = 10 # 先跳過有問題那幾筆
cases = []
labels = []
# day = 1
for i in range(0, 100):
one_day = list(df.iloc[point:point + day, 0]) + list(df.iloc[point:point + day, 1])
cases.append(one_day)
labels.append([df.iloc[point + day, 0]])
point += 1
cases_test = []
labels_test = []
for j in range(0, 100):
one_day = list(df.iloc[point:point + day, 0]) + list(df.iloc[point:point + day, 1])
cases_test.append(one_day)
labels_test.append([df.iloc[point + day, 0]])
point += 1
# ---------------------------------------------------------------------------------------
mse_all = []
total_start_time = time.time()
for r in range(10):
start = time.time()
neutest = neural.Neu('03F2614S-03F2709S' + ' #' + str(r))
trainfinished = False
while trainfinished != True:
try:
for i in range(neutest.epoch):
# print(i + 1)
# count = 0
for c, l in zip(cases, labels):
inputlist = c
expect = l
# print('input: ', inputlist)
# print('expect: ', expect)
result = neutest.forward(inputlist)
# print('result: ', result)
neutest.backward(expect)
# print(count)
# count = count + 1
# if count == 288:
# count = 0
neutest.cleartemporalepoch()
except OverflowError:
neutest = neural.Neu('03F2614S-03F2709S' + ' #' + str(r))
print('math error')
else:
trainfinished = True
end = time.time()
elapsed = end - start
print("Time taken: ", elapsed, "seconds.")
# test
predict_all = []
for test1 in cases_test:
result = neutest.forward(test1)
predict_all.append(result)
mse = mean_squared_error(labels_test[:], predict_all[:])
print('MSE :', mse)
mse_all.append(mse)
total_end_time = time.time()
print("Total time taken: ", total_end_time - total_start_time, "seconds.")
print(mse_all)
print(np.array(mse_all).min())
# plt.plot(labels_test, 'b')
# plt.plot(predict_all, 'r')
# plt.show()
# print("---------------------------------------------------------")