-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforecast.py
153 lines (103 loc) · 3.8 KB
/
forecast.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras import layers
import matplotlib.pyplot as plt
import numpy as np
import csv
print("Loading data")
FILENAME = "Data/CADCHF1440.csv"
exportFileName = "Data/AUDCAD30_input.csv"
opearaList = []
with open(FILENAME, 'r') as f_read:
data = csv.reader(f_read)
time = 0
index = 0
for line in data:
temp = []
for element in line[2:6]:
temp.append(element)
opearaList.append(temp)
print("Start proccess")
trainningList = []
#print(opearaList[0])
totalSize = len(opearaList)
print("Total size:", totalSize)
# for line in opearaList:
# tempElement = []
# index = opearaList.index(line)
# # print(index)
# if(index+9<=totalSize):
# for shift in range(9):
# tempElement = tempElement + opearaList[index+shift]
# trainningList.append(tempElement)
inputList = []
# inputData = np.zero((totalSize, 36))
for index in range(totalSize):
if(index+9<totalSize):
tempElement = []
for shift in range(9):
for element in opearaList[index+shift]:
tempElement.append(float(element))
inputList.append(tempElement)
outputList = []
for line in opearaList[9:]:
tempLine = []
for element in line:
tempLine.append(float(element))
outputList.append(tempLine)
print("TrainList:", len(inputList))
print("OutputList:", len(outputList))
predictList =[]
tempPredict = []
for line in opearaList[totalSize-9:]:
for element in line:
tempPredict.append(float(element))
predictList.append(tempPredict)
# # translate data
# for line in inputList:
# for element in line:
# element = float(element)
# for line in outputList:
# for element in line:
# element = float(element)
# for line in predictList:
# for element in line:
# element = float(element)
# print(np.random.random((100, 36)))
inputData = np.array(inputList)
outputData = np.array(outputList)
predictData = np.array(predictList)
print("Start modeling")
model = tf.keras.Sequential()
# for input layer
model.add(layers.Dense(36, activation="relu"))
# for hidden layer
model.add(layers.Dense(18, activation="relu", bias_initializer=tf.keras.initializers.constant(1.0)))
# model.add(layers.Dense(18, activation="relu", bias_initializer=tf.keras.initializers.constant(1.0)))
# model.add(layers.Dense(18, activation="relu", bias_initializer=tf.keras.initializers.constant(1.0)))
# model.add(layers.Dense(18, activation="relu", bias_initializer=tf.keras.initializers.constant(1.0)))
# model.add(layers.Dense(18, activation="relu", bias_initializer=tf.keras.initializers.constant(1.0)))
# for output layer
model.add(layers.Dense(4, activation="relu", bias_initializer=tf.keras.initializers.constant(1.0)))
# compile the model
#model.compile(optimizer=keras.optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True), loss='mse', metrics=['mae'])
model.compile(optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False), loss='mse', metrics=['mae'])
#model.compile(optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True), loss='categorical_crossentropy', metrics=['accuracy'])
# fit the model
#
model.fit(inputData, outputData, epochs=10, batch_size=18)
# evaluate the result
print("Priting the evaluate result")
model.evaluate(inputData, outputData, batch_size=18)
# result predict
result = model.predict(predictData, batch_size=18)
print(result.shape)
print(result)
# Save entire model to a HDF5 file
model.save('audcad_model.h5')
# Recreate the exact same model, including weights and optimizer.
# model = tf.keras.models.load_model('my_model.h5')
# export the result
with open("result.csv", "w") as fw:
writer = csv.writer(fw)
writer.writerow(result)