-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_weather.py
278 lines (248 loc) · 10.5 KB
/
lstm_weather.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import print_function
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Activation
from tensorflow import keras
from sklearn.metrics import mean_squared_error, r2_score
import os
from tensorflow.python.keras.callbacks import EarlyStopping
import warnings
from keras.models import load_model
warnings.filterwarnings("ignore", category=DeprecationWarning)
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
start_time = time.time()
# Reading from the database
dataset = pd.read_csv('assets/hourly_load&weather_data.csv')
x_temperature = dataset['Temperature']
x_dewpoint = dataset['Dew Point']
x_humidity = dataset['Humidity']
x_windspeed = dataset['Wind Speed']
x_pressure = dataset['Pressure']
y = dataset['Load']
# Converting the values in the usable format
x_temperature = x_temperature.values
x_dewpoint = x_dewpoint.values
x_humidity = x_humidity.values
x_windspeed = x_windspeed.values
x_pressure = (x_pressure.values) / 29.53 # Hg to bar
y = (y.values) / 1000 # MW to GW
# Visualising the independent variables
dataset_graph = plt.figure(figsize=(60, 8))
plt.plot(x_temperature[:364], label='Temperature (C)')
plt.plot(x_dewpoint[:364], label='Dew Point (C)')
plt.plot(x_humidity[:364], label='Humidity (%)')
plt.plot(x_windspeed[:364], label='Wind Speed (mph)')
plt.plot(x_pressure[:364], label='Pressure (bar)')
plt.plot(y[:364], label='Load (GW)')
plt.legend(loc='upper right')
plt.title("Dataset", fontsize=14)
plt.xlabel('Day', fontsize=14)
plt.ylabel('Values', fontsize=14)
plt.legend()
plt.show()
dataset_graph.savefig('results/LSTM_weather/dataset_graph.jpg', bbox_inches='tight')
# Converting to a usable format in a 2D array
x_temperature = x_temperature.reshape((len(x_temperature), 1))
x_dewpoint = x_dewpoint.reshape((len(x_dewpoint), 1))
x_humidity = x_humidity.reshape((len(x_humidity), 1))
x_windspeed = x_windspeed.reshape((len(x_windspeed), 1))
x_pressure = x_pressure.reshape((len(x_pressure), 1))
y = y.reshape((len(y), 1))
print("\nIndependent & Dependent variables arrays:")
print("x_temperature:", x_temperature.shape)
print("x_dewpoint:", x_dewpoint.shape)
print("x_humidity:", x_humidity.shape)
print("x_windspeed:", x_windspeed.shape)
print("x_pressure:", x_pressure.shape)
print("y:", y.shape)
# Normalising the data using the MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
x_temperature_scaled = scaler.fit_transform(x_temperature)
x_dewpoint_scaled = scaler.fit_transform(x_dewpoint)
x_humidity_scaled = scaler.fit_transform(x_humidity)
x_windspeed_scaled = scaler.fit_transform(x_windspeed)
x_pressure_scaled = scaler.fit_transform(x_pressure)
y_scaled = scaler.fit_transform(y)
# Stacking the data columns horizontally
stacked_dataset = np.hstack(
(x_temperature_scaled, x_dewpoint_scaled, x_humidity_scaled, x_windspeed_scaled, x_pressure_scaled, y_scaled))
print("\nStacked Dataset", stacked_dataset.shape, ":\n", stacked_dataset)
#Split a multivariate sequence into samples
def split_sequences(dataset, n_steps_in, n_steps_out):
X, y = list(), list()
for i in range(len(dataset)):
start = i + n_steps_in
end = start + n_steps_out-1
if end > len(dataset):
break
x_sequence, y_sequence = dataset[i:start, :-1], dataset[start-1:end, -1]
X.append(x_sequence)
y.append(y_sequence)
return np.array(X), np.array(y)
n_steps_in, n_steps_out, n_features = 30 , 15, 5
X, y = split_sequences(stacked_dataset, n_steps_in, n_steps_out)
print ("\nX.shape " , X.shape)
print ("y.shape" , y.shape)
#Splitting into training & test sets
train_X, train_y = X[:318, :], y[:318, :]
test_X, test_y = X[318:, :], y[318:, :]
print ("\ntrain_X" , train_X.shape)
print ("train_y" , train_y.shape)
print ("test_X" , test_X.shape)
print ("test_y" , test_y.shape)
#Learning Rate
opt = keras.optimizers.Adam(learning_rate=0.0001)
#LSTM Model
model = Sequential()
model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(50, activation='relu'))
model.add(Dense(n_steps_out))
model.add(Activation('linear'))
model.compile(loss='mse' , optimizer=opt , metrics=['mse'])
es = EarlyStopping(monitor="val_loss", min_delta=0, patience=5, verbose=1, mode="auto", baseline=None,
restore_best_weights=True) # Stops the training when the values don't improve
history = model.fit(train_X, train_y, epochs=100, batch_size=30, verbose=1, validation_data=(test_X, test_y), callbacks=[es], shuffle=False)
print(model.summary())
# Evaluating the result
test_mse = model.evaluate(test_X, test_y, verbose=1)
# print('\nThe Mean-squared-error (MSE) on the test data set is %.6f over %d test samples.' % (test_mse, len(test_y)))
print("Test MSE:", test_mse)
model.save('assets/multivariate/lstm.tf', overwrite=True, include_optimizer=True)
# loaded_model = load_model('path')
# print("X", test_X.shape, test_X[65])
# Getting the predicted values
predicted_values = model.predict(test_X[0].reshape((1,30,5)))
scaler1 = MinMaxScaler(feature_range=(0, 1))
scaler1.fit(y)
y_pred = scaler1.inverse_transform(predicted_values)
test_scaled = scaler1.inverse_transform([test_y[0]])
y_pred = y_pred.reshape((15,))
test_scaled = test_scaled.reshape((15,))
print("P", y_pred)
print("y", test_scaled)
print("p", predicted_values, predicted_values.reshape((15,)))
print("y", test_y[0], test_y[0].shape)
# # read test data
# x_temperature = dataset['Temperature'].values
# x_dewpoint = dataset['Dew Point'].values
# x_humidity = dataset['Humidity'].values
# x_windspeed = dataset['Wind Speed'].values
# x_pressure = dataset['Pressure'].values
# y_test = dataset['Load'].values
#
# x_temperature = x_temperature[255:]
# x_dewpoint = x_dewpoint[255:]
# x_humidity = x_humidity[255:]
# x_windspeed = x_windspeed[255:]
# x_pressure = x_pressure[255:]
# y_test = y_test[255:]
#
#
# # convert to [rows, columns] structure
# x_temperature = x_temperature.reshape((len(x_temperature), 1))
# x_dewpoint = x_dewpoint.reshape((len(x_dewpoint), 1))
# x_humidity = x_humidity.reshape((len(x_humidity), 1))
# x_windspeed = x_windspeed.reshape((len(x_windspeed), 1))
# x_pressure = x_pressure.reshape((len(x_pressure), 1))
# y_test = y_test.reshape((len(y_test), 1))
#
# x_temperature_scaled = scaler.fit_transform(x_temperature)
# x_dewpoint_scaled = scaler.fit_transform(x_dewpoint)
# x_humidity_scaled = scaler.fit_transform(x_humidity)
# x_windspeed_scaled = scaler.fit_transform(x_windspeed)
# x_pressure_scaled = scaler.fit_transform(x_pressure)
#
#
# def prep_data(x_temperature_scaled, x_dewpoint_scaled, x_humidity_scaled, x_windspeed_scaled, x_pressure_scaled, y_test, start, end, last):
# dataset_test = np.hstack((x_temperature_scaled, x_dewpoint_scaled, x_humidity_scaled, x_windspeed_scaled, x_pressure_scaled))
# dataset_test_X = dataset_test[start:end, :]
# test_X_new = dataset_test_X.reshape(1, dataset_test_X.shape[0], dataset_test_X.shape[1])
#
# # prepare past and groundtruth
# past_data = y_test[:end, :]
# dataset_test_y = y_test[end:last, :]
# scaler1 = MinMaxScaler(feature_range=(0, 1))
# scaler1.fit(dataset_test_y)
#
# # predictions
# y_pred = model.predict(test_X_new)
# y_pred_inv = scaler1.inverse_transform(y_pred)
# y_pred_inv = y_pred_inv.reshape(n_steps_out, 1)
# y_pred_inv = y_pred_inv[:, 0]
#
# return y_pred_inv, dataset_test_y, past_data
#
#
# def evaluate_prediction(predictions, actual, model_name , start , end):
# errors = predictions - actual
# mse = np.square(errors).mean()
# rmse = np.sqrt(mse)
# mae = np.abs(errors).mean()
#
# print("Test Data from {} to {}".format(start, end))
# print('Mean Absolute Error: {:.2f}'.format(mae))
# print('Root Mean Square Error: {:.2f}'.format(rmse))
# print('')
# print('')
#
#
#
# # Plot history and future
# def plot_multistep(history, prediction1 , groundtruth , start , end):
# plt.figure(figsize=(20, 4))
# y_mean = np.mean(prediction1)
# range_history = len(history)
# range_future = list(range(range_history, range_history + len(prediction1)))
# plt.plot(np.arange(range_history), np.array(history), label='History')
# plt.plot(range_future, np.array(prediction1),label='Forecasted with LSTM')
# plt.plot(range_future, np.array(groundtruth),label='GroundTruth')
# plt.legend(loc='upper right')
# plt.title("Test Data from {} to {} , Mean = {:.2f}".format(start, end, y_mean) , fontsize=18)
# plt.xlabel('Time step' , fontsize=18)
# plt.ylabel('y-value' , fontsize=18)
#
#
# for i in range(30, 60, 90):
# start = i
# end = start + n_steps_in
# last = end + n_steps_out
# y_pred_inv , dataset_test_y , past_data = prep_data(x_temperature_scaled, x_dewpoint_scaled, x_humidity_scaled, x_windspeed_scaled, x_pressure_scaled, y_test, start, end, last)
# evaluate_prediction(y_pred_inv , dataset_test_y, 'LSTM' , start , end)
# plot_multistep(past_data , y_pred_inv , dataset_test_y , start , end)
# Plotting the results
fig = plt.figure()
plt.plot((test_y[0]))
plt.plot(predicted_values.reshape((15,)))
plt.title('LSTM')
plt.xlabel('Days')
plt.ylabel('Electricity load (*1e3)')
plt.legend(('Actual', 'Predicted'), fontsize='15')
plt.show()
fig.savefig('results/LSTM_weather/final_output.jpg', bbox_inches='tight')
# Plot of the loss
loss_fig = plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()
loss_fig.savefig('results/LSTM_weather/final_loss.jpg', bbox_inches='tight')
# Storing the result in a file: 'load_forecasting_result.txt'
np.savetxt('results/LSTM_weather/predicted_values.txt', predicted_values)
np.savetxt('results/LSTM_weather/test_values.txt', test_y)
end_time = time.time()
# print("MSE:", mean_squared_error(test_y[65].reshape((1,15))*1000, predicted_values*1000))
# print("RMSE:", mean_squared_error(test_y[65].reshape((1,15))*1000, predicted_values*1000, squared=False))
# print("MSE:", mean_squared_error(test_y[0]*1000, predicted_values.reshape((15,))*1000))
# print("RMSE:", mean_squared_error(test_y[0]*1000, predicted_values.reshape((15,))*1000, squared=False))
print("RMSE:", mean_squared_error(test_scaled*1000, y_pred*1000, squared=False))
# print("R-squared:", r2_score(test_y[65].reshape((1,15))*1000, predicted_values*1000))
print("R-squared:", r2_score(test_scaled, y_pred))
print('MAPE:', np.mean(np.abs((test_scaled*1000 - y_pred*1000) / (test_scaled*1000))),'\n')
print("Total time: ", end_time - start_time)