-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_Keras.py
151 lines (137 loc) · 5.41 KB
/
Test_Keras.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
'''
import pandas
import matplotlib.pyplot as plt
dataset = pandas.read_csv('a.csv', usecols=[1], engine='python', skipfooter=0)
print(dataset)
plt.plot(dataset)
plt.show()
'''
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1, in_from=0, in_to=1, out_from=1, out_to=2):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
dataX.append(dataset[i:(i+look_back), in_from:in_to])
dataY.append(dataset[i + look_back, out_from:out_to])
return numpy.array(dataX), numpy.array(dataY)
import numpy
import matplotlib.pyplot as plt
import pandas
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
#dataframe = pandas.read_csv('a.csv', names=['x','y'], engine='python', skipfooter=0, skiprows=1)
#dataframe = pandas.read_excel('major_indice.xlsx', skipfooter=0)
dataframe = pandas.read_excel('major_indice.xlsx', sheetname='indice_week')
dataset = dataframe.values
dataset = dataset.astype('float32')
#print(dataset)
# normalize the dataset
#scaler = MinMaxScaler(feature_range=(0, 1))
#dataset = scaler.fit_transform(dataset)
#print(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.9)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 4
in_from = 0
in_to = 3
out_from = 3
out_to = 5
trainX, trainY = create_dataset(train, look_back, in_from, in_to, out_from, out_to)
#print(trainX.shape, trainY.shape)
#print(trainX)
#print(trainY)
testX, testY = create_dataset(test, look_back, in_from, in_to, out_from, out_to)
#print(trainX)
#print(trainY)
# reshape input to be [samples, time steps, features]
#print(trainX.shape)
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], trainX.shape[2]))
trainY = numpy.reshape(trainY, (trainY.shape[0], trainY.shape[1]))
print(trainX.shape, trainY.shape)
print("trainX:\n", trainX)
print("trainY:\n", trainY)
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], testX.shape[2]))
testY = numpy.reshape(testY, (testY.shape[0], testY.shape[1]))
print(testX.shape, testY.shape)
print("testX:\n", testX)
print("testY:\n", testY)
# create and fit the LSTM network
model = Sequential()
#model.add(LSTM(4, input_dim=look_back))
model.add(LSTM(output_dim=6, input_dim=trainX.shape[2], input_length=trainX.shape[1]))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')
for loop in range(10):
print("--------------- Loop # %d ---------------" % loop)
#del model
#model = load_model('my_model.h5')
#model.fit(trainX, trainY, nb_epoch=1000, batch_size=1, verbose=2)
model.fit(trainX, trainY, nb_epoch=1000, batch_size=1, verbose=2)
#model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'
#del model # deletes the existing model
# make predictions
trainPredict = model.predict(trainX)
print("trainPredict:\n")
success = 0
fail = 0
for idx in range(len(trainX)):
err_0 = abs(trainY[idx][0] - trainPredict[idx][0])
err_1 = abs(trainY[idx][1] - trainPredict[idx][1])
if (trainY[idx][0] == 1 and trainPredict[idx][0] > trainPredict[idx][1]) or (trainY[idx][1] == 1 and trainPredict[idx][0] < trainPredict[idx][1]):
success += 1
print(trainY[idx][0], '\t', trainPredict[idx][0], '\t', trainY[idx][1], '\t', trainPredict[idx][1], '\t', err_0, '\t', err_1, True)
else:
fail += 1
print(trainY[idx][0], '\t', trainPredict[idx][0], '\t', trainY[idx][1], '\t', trainPredict[idx][1], '\t', err_0, '\t', err_1, False)
print(success/float(success+fail))
testPredict = model.predict(testX)
print("testPredict:\n")
success = 0
fail = 0
for idx in range(len(testX)):
err_0 = abs(testY[idx][0] - testPredict[idx][0])
err_1 = abs(testY[idx][1] - testPredict[idx][1])
if (testY[idx][0] == 1 and testPredict[idx][0] > testPredict[idx][1]) or (testY[idx][1] == 1 and testPredict[idx][0] < testPredict[idx][1]):
success += 1
print(testY[idx][0], '\t', testPredict[idx][0], '\t', testY[idx][1], '\t', testPredict[idx][1], '\t', err_0, '\t', err_1, True)
else:
fail += 1
print(testY[idx][0], '\t', testPredict[idx][0], '\t', testY[idx][1], '\t', testPredict[idx][1], '\t', err_0, '\t', err_1, False)
print(success/float(success+fail))
'''
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
'''
'''
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
#plt.plot(scaler.inverse_transform(dataset))
#plt.plot(trainPredictPlot)
#plt.plot(testPredictPlot)
#plt.show()
'''