-
Notifications
You must be signed in to change notification settings - Fork 2
/
MTTS_DFFN.py
151 lines (113 loc) · 4.35 KB
/
MTTS_DFFN.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
# https://machinelearningmastery.com/backtest-machine-learning-models-time-series-forecasting/
import os
import pandas
from sklearn import metrics
from keras import Sequential
from keras.layers import BatchNormalization, Dense, Activation, Dropout
from sklearn.model_selection import TimeSeriesSplit
from Settings import *
from matplotlib import pyplot
import numpy as np
from keras.models import model_from_json
import matplotlib.dates as mdates
from baseline import str_to_datetime
def multiple_train_test_splits(file_name):
# Load data using Pandas
cols = list(pandas.read_csv(file_name, nrows=1))
series = pandas.read_csv(file_name, header=0, usecols=[i for i in cols])
# Convert to numpy array
data = series.to_numpy()
dates = data[:,0]
data = data[:,1:]
splits = TimeSeriesSplit(n_splits=Settings.n_splits)
f1 = pyplot.figure(1)
f1.suptitle("Windpark 7")
x_test = []
y_test = []
# Plotting lists
val_y = []
val_pred = []
val_score = []
validation_hist = []
index = 1
for train_index, test_index in splits.split(data):
training = data[train_index]
test = data[test_index]
# Getting features and targets
x_features = training[:, 0:11]
x_targets = training[:, 11]
y_features = test[:, 0:11]
y_targets = test[:, 11]
print('Observations: %d' % (len(training) + len(test)))
print('Training Observations: %d' % (len(training)))
print('Testing Observations: %d' % (len(test)))
# The first number must reflect number of splits
ax1 = f1.add_subplot(Settings.n_splits, 1, 0 + index)
ax1.plot([x[11] for x in training])
ax1.plot([None for i in training] + [x[11] for x in test])
# Does training
model = create_model()
# train on the rest of the training set
val_history = model.fit(x=x_features, y=x_targets, validation_data=(y_features, y_targets),
verbose=Settings.verbose,
epochs=Settings.epochs)
validation_hist.append(val_history.history['loss'])
# Prediction and plotting
pred = model.predict(y_features)
val_y.append(y_targets)
val_pred.append(pred)
val_score.append(metrics.mean_squared_error(pred, y_targets))
print("Train test score (MSE): {}".format(val_score[index - 1]))
x_test = y_features
y_test = y_targets
index += 1
pyplot.show()
print("Mean value score for all train tests: ", np.mean(val_score))
# Plotting loss
plot_loss(validation_hist)
# Predicting and plotting result
pred = model.predict(x_test)
score = metrics.mean_squared_error(pred, y_test)
print("Train test score (MSE): {}".format(score))
dates = dates[-len(y_test):]
frame = pandas.DataFrame({"Dates" : dates})
frame["Dates"] = frame["Dates"].apply(str_to_datetime)
pyplot.plot(frame["Dates"], pred)
pyplot.plot(frame["Dates"], y_test)
pyplot.title('Windpark 7')
pyplot.legend(['Prediction', 'Real'], loc='upper left')
pyplot.show()
def plot_loss(validation_hist):
pyplot.figure(1)
for x in validation_hist:
pyplot.plot(x)
pyplot.title('Model loss for windpark 7')
pyplot.ylabel('Mean squared error')
pyplot.xlabel('epoch')
pyplot.legend(['Train 1', 'Train 2', 'Train 3', 'Train 4', 'Train 5'], loc='upper right')
pyplot.show()
def create_model():
model = Sequential()
# Input layer
model.add(
Dense(Settings.input_layer, input_dim=Settings.number_of_features, kernel_initializer='normal', use_bias=False))
model.add(Activation('relu'))
# model.add(BatchNormalization())
# Hidden Layer 1
model.add(Dense(Settings.h_layer1, kernel_initializer='normal'))
model.add(Activation('relu'))
# model.add(BatchNormalization())
# Hidden Layer 2
model.add(Dense(Settings.h_layer2, kernel_initializer='normal'))
model.add(Activation('relu'))
# model.add(BatchNormalization())
# Output layer
model.add(Dense(Settings.output_layer))
# model.add(BatchNormalization())
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mse', 'mae'])
return model
def main():
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
multiple_train_test_splits("Processed_data/wp7.csv")
if __name__ == '__main__':
main()