-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSTM.py
220 lines (184 loc) · 6.01 KB
/
LSTM.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
#%%
import os
import pandas as pd
import numpy as np
import tensorflow as ts
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from sklearn.metrics import mean_absolute_error , mean_squared_error
from tensorflow.keras.layers import LSTM, Dense, Dropout, BatchNormalization
import matplotlib.pyplot as plt
# %%
df = pd.read_csv("C:/Users/utilisateur/OneDrive - Simplonformations.co/Bureau/Programmations/LSTM_polution/lstm_polution/LSTM-Multivariate_pollution.csv")
# %%
# Encode Wind direction
df.wnd_dir.unique()
# %%
def wind_encode(s):
if s == "SE":
return 1
elif s == "NE":
return 2
elif s == "NW":
return 3
else:
return 4
df["wind_dir"] = df["wnd_dir"].apply(wind_encode)
del df["wnd_dir"]
df.head()
# %%
# Can't scale Date, i drop it
df.drop(["date"], inplace = True, axis = 1)
df.head()
# %%
#Scaled data
scaler = MinMaxScaler()
scaled = scaler.fit_transform(df)
scaled = pd.DataFrame(scaled)
scaled.head()
# %%
def series_to_supervised(data, n_in = 1, n_out = 1, dropnan = True):
n_vars = 1 if type(data) is list else data.shape[1]
df = pd.DataFrame(data)
cols, names = list(), list()
#input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [("var%d(t-%d)" % (j+1, i)) for j in range(n_vars)]
# Forecast sequence (t, t+1,...,t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [("var%d(t)" % (j+1)) for j in range(n_vars)]
else:
names += [("var%d(t+%d)" % (j+1, i)) for j in range(n_vars)]
# Put it all together
agg = pd.concat(cols, axis=1)
agg.columns = names
#drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
# %%
reframed = series_to_supervised(scaled, 1, 1)
print(reframed.shape)
# %%
# droping columns we don't want to predict
reframed.drop(reframed.columns[[9, 10, 11, 12, 13, 14, 15]], axis = 1, inplace = True)
print(reframed.head())
# %%
# split train and validation data
#Eache rows is 1 hour, each year = 24 hours * 365 days
year = 24 * 365
values = reframed.values
#train on forst 3 years and validate w/ rst
train = values[:(3 * year), :]
val = values[(3 * year):, :]
#split training and validation inte feature and target
X_train, y_train = train[:, :-1], train[:, -1]
X_val, y_val = val[:, :-1], val[:, -1]
#reshape input 3 dim
X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))
X_val = X_val.reshape((X_val.shape[0], 1, X_val.shape[1]))
print(X_train.shape, y_train.shape, X_val.shape, y_val.shape)
# %%
def create_model():
opti = Adam(learning_rate=0.01)
model = Sequential()
model.add(LSTM(256, input_shape = (X_train.shape[1], X_train.shape[2]), activation = "relu", return_sequences=True))
model.add(LSTM(150, activation = "relu", return_sequences=True))
model.add(LSTM(75, activation = "relu", return_sequences=True))
model.add(Dense(200))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Dense(75))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Dense(1))
model.compile(loss = "mse",
optimizer = opti,
metrics = ["accuracy"])
return model
#%%
#Create ModelChekPoint
checkpoint_path = "training_1/cp-{epoch:04d}.cpkt"
checkpoint_dir = os.path.dirname(checkpoint_path)
batch_size = 170
cp_callback = ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose = 0,
save_freq= 5 * batch_size)
# %%
model = create_model()
history = model.fit(X_train, y_train, epochs = 100,
batch_size = 170, validation_split = 0.2,
verbose = 2, shuffle = False, validation_data=(X_val, y_val),
)
# %%
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# %%
poll = np.array(df["pollution"])
poll_mean = poll.mean()
poll_std = poll.std()
y_val_true = val[:,8]
val_pred = model.predict(X_val).ravel()
y_val_true = y_val_true*poll_std + poll_mean
val_pred = val_pred*poll_std + poll_mean
from matplotlib import pyplot as plt
plt.figure(figsize=(15,6))
plt.xlim([1000,1250])
plt.ylabel("ppm")
plt.xlabel("hrs")
plt.plot(val_pred, c = "r", alpha = 0.90, linewidth = 2.5)
plt.plot(y_val_true, c = "b", alpha = 0.75)
plt.title("Validation Data Prediction", fontsize=20)
plt.legend(['Predicted Values', 'True Values'], fontsize='xx-large')
plt.show()
#%%
#Save model
model.save("LSTM.h5")
# %%
# Import Test data
test_df = df.copy()
#test_df["wind_dir"] = test_df["wind_dir"].apply(wind_encode)
#del test_df["win_dir"]
values_test = test_df.values
values_test = values_test.astype("float32")
scaler1 = MinMaxScaler()
scaled_test = scaler1.fit_transform(values_test)
reframed_test = series_to_supervised(scaled_test, 1, 1)
reframed_test.drop(reframed_test.columns[[9,10,11,12,13,14,15]], axis = 1, inplace = True)
values_test1 = reframed_test.values
test_x, test_y = values_test1[:, :-1], values_test1[:, -1]
test_x = test_x.reshape((test_x.shape[0], 1, test_x.shape[1]))
val_pred4 = model.predict(test_x).ravel()
#inverse scaling the ouptu, for better visuel interpretation
test_y = test_y * poll_std + poll_mean
val_pred4 = val_pred4 * poll_std + poll_mean
# %%
plt.figure(figsize=(18,5.5))
plt.ylabel("ppm")
plt.xlabel("hrs")
plt.plot(test_y, c = "r", alpha = 0.90)
plt.plot(val_pred4, c = "darkblue", alpha = 0.75)
plt.title("Testing Data Prediction")
plt.legend(['Predicted Values', 'True Values'], fontsize='x-large')
plt.show()
# %%