levimohle commited on
Commit
c27effd
1 Parent(s): 30c3c9d

Added energy prediction LSTM

Browse files
Files changed (2) hide show
  1. lstm_energy.ipynb +384 -0
  2. lstm_energy_01.keras +0 -0
lstm_energy.ipynb ADDED
@@ -0,0 +1,384 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 98,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import pandas as pd \n",
10
+ "from datetime import datetime \n",
11
+ "from datetime import date\n",
12
+ "import matplotlib.pyplot as plt\n",
13
+ "# import seaborn as sns\n",
14
+ "import numpy as np\n",
15
+ "import pandas as pd\n",
16
+ "from keras.models import Sequential\n",
17
+ "from keras.layers import LSTM, Dense\n",
18
+ "from sklearn.model_selection import train_test_split\n",
19
+ "from sklearn.preprocessing import MinMaxScaler,StandardScaler\n",
20
+ "from keras.callbacks import ModelCheckpoint\n",
21
+ "\n",
22
+ "dataPATH = r\"C:\\Users\\levim\\OneDrive\\Documents\\MastersAI_ES\\TeamProject-5ARIP10\\smart-buildings\\Data\"\n",
23
+ "all_data = pd.read_csv(dataPATH + r\"\\long_merge.csv\")"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 102,
29
+ "metadata": {},
30
+ "outputs": [
31
+ {
32
+ "data": {
33
+ "text/html": [
34
+ "<div>\n",
35
+ "<style scoped>\n",
36
+ " .dataframe tbody tr th:only-of-type {\n",
37
+ " vertical-align: middle;\n",
38
+ " }\n",
39
+ "\n",
40
+ " .dataframe tbody tr th {\n",
41
+ " vertical-align: top;\n",
42
+ " }\n",
43
+ "\n",
44
+ " .dataframe thead th {\n",
45
+ " text-align: right;\n",
46
+ " }\n",
47
+ "</style>\n",
48
+ "<table border=\"1\" class=\"dataframe\">\n",
49
+ " <thead>\n",
50
+ " <tr style=\"text-align: right;\">\n",
51
+ " <th></th>\n",
52
+ " <th>date</th>\n",
53
+ " <th>hvac_N</th>\n",
54
+ " <th>hvac_S</th>\n",
55
+ " <th>air_temp_set_1</th>\n",
56
+ " <th>solar_radiation_set_1</th>\n",
57
+ " </tr>\n",
58
+ " </thead>\n",
59
+ " <tbody>\n",
60
+ " <tr>\n",
61
+ " <th>0</th>\n",
62
+ " <td>2018-01-01 00:00:00</td>\n",
63
+ " <td>NaN</td>\n",
64
+ " <td>NaN</td>\n",
65
+ " <td>11.64</td>\n",
66
+ " <td>86.7</td>\n",
67
+ " </tr>\n",
68
+ " <tr>\n",
69
+ " <th>1</th>\n",
70
+ " <td>2018-01-01 00:01:00</td>\n",
71
+ " <td>NaN</td>\n",
72
+ " <td>NaN</td>\n",
73
+ " <td>11.64</td>\n",
74
+ " <td>86.7</td>\n",
75
+ " </tr>\n",
76
+ " <tr>\n",
77
+ " <th>2</th>\n",
78
+ " <td>2018-01-01 00:02:00</td>\n",
79
+ " <td>NaN</td>\n",
80
+ " <td>NaN</td>\n",
81
+ " <td>11.64</td>\n",
82
+ " <td>86.7</td>\n",
83
+ " </tr>\n",
84
+ " <tr>\n",
85
+ " <th>3</th>\n",
86
+ " <td>2018-01-01 00:03:00</td>\n",
87
+ " <td>NaN</td>\n",
88
+ " <td>NaN</td>\n",
89
+ " <td>11.64</td>\n",
90
+ " <td>86.7</td>\n",
91
+ " </tr>\n",
92
+ " <tr>\n",
93
+ " <th>4</th>\n",
94
+ " <td>2018-01-01 00:04:00</td>\n",
95
+ " <td>NaN</td>\n",
96
+ " <td>NaN</td>\n",
97
+ " <td>11.64</td>\n",
98
+ " <td>86.7</td>\n",
99
+ " </tr>\n",
100
+ " </tbody>\n",
101
+ "</table>\n",
102
+ "</div>"
103
+ ],
104
+ "text/plain": [
105
+ " date hvac_N hvac_S air_temp_set_1 solar_radiation_set_1\n",
106
+ "0 2018-01-01 00:00:00 NaN NaN 11.64 86.7\n",
107
+ "1 2018-01-01 00:01:00 NaN NaN 11.64 86.7\n",
108
+ "2 2018-01-01 00:02:00 NaN NaN 11.64 86.7\n",
109
+ "3 2018-01-01 00:03:00 NaN NaN 11.64 86.7\n",
110
+ "4 2018-01-01 00:04:00 NaN NaN 11.64 86.7"
111
+ ]
112
+ },
113
+ "execution_count": 102,
114
+ "metadata": {},
115
+ "output_type": "execute_result"
116
+ }
117
+ ],
118
+ "source": [
119
+ "feature_list = ['date', 'hvac_N', 'hvac_S', 'air_temp_set_1', 'solar_radiation_set_1']\n",
120
+ "extended_energy_data = all_data[feature_list]\n",
121
+ "extended_energy_data.head()"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": null,
127
+ "metadata": {},
128
+ "outputs": [],
129
+ "source": [
130
+ "energy_data = pd.read_csv(dataPATH + r\"\\hvac_data_1h.csv\")\n",
131
+ "\n",
132
+ "# Convert the date column to datetime\n",
133
+ "energy_data['date'] = pd.to_datetime(energy_data['date'], format = \"%Y-%m-%d %H:%M:%S\")\n",
134
+ "\n",
135
+ "energy_data['day_of_week'] = energy_data['date'].dt.weekday\n",
136
+ "# Filter the data for the year 2019\n",
137
+ "df_filtered = energy_data[ (energy_data.date.dt.date >date(2019, 1, 20)) & (energy_data.date.dt.date< date(2019, 7, 26))]\n",
138
+ "\n",
139
+ "# Check for NA values in the DataFrame\n",
140
+ "if df_filtered.isna().any().any():\n",
141
+ " print(\"There are NA values in the DataFrame columns.\")"
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": null,
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "testdataset_df = df_filtered[(df_filtered.date.dt.date <date(2019, 2, 20))]\n",
151
+ "\n",
152
+ "traindataset_df = df_filtered[ (df_filtered.date.dt.date >date(2019, 2, 21))]\n",
153
+ "\n",
154
+ "testdataset = testdataset_df.drop(columns=[\"date\"]).values\n",
155
+ "\n",
156
+ "traindataset = traindataset_df.drop(columns=[\"date\"]).values\n",
157
+ "\n",
158
+ "columns_with_na = traindataset_df.columns[traindataset_df.isna().any()].tolist()\n",
159
+ "columns_with_na"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": null,
165
+ "metadata": {},
166
+ "outputs": [],
167
+ "source": [
168
+ "traindataset = traindataset.astype('float32')\n",
169
+ "testdataset = testdataset.astype('float32')\n",
170
+ "\n",
171
+ "mintest = np.min(testdataset)\n",
172
+ "maxtest = np.max(testdataset)\n",
173
+ "\n",
174
+ "scaler = MinMaxScaler(feature_range=(0, 1))\n",
175
+ "traindataset = scaler.fit_transform(traindataset)\n",
176
+ "testdataset = scaler.transform(testdataset)"
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "execution_count": 101,
182
+ "metadata": {},
183
+ "outputs": [
184
+ {
185
+ "name": "stdout",
186
+ "output_type": "stream",
187
+ "text": [
188
+ "Epoch 1/5\n",
189
+ "57/58 [============================>.] - ETA: 0s - loss: 0.0238\n",
190
+ "Epoch 1: val_loss improved from inf to 0.01717, saving model to lstm_energy_01.keras\n",
191
+ "58/58 [==============================] - 11s 62ms/step - loss: 0.0238 - val_loss: 0.0172\n",
192
+ "Epoch 2/5\n",
193
+ "56/58 [===========================>..] - ETA: 0s - loss: 0.0139\n",
194
+ "Epoch 2: val_loss improved from 0.01717 to 0.01117, saving model to lstm_energy_01.keras\n",
195
+ "58/58 [==============================] - 2s 42ms/step - loss: 0.0139 - val_loss: 0.0112\n",
196
+ "Epoch 3/5\n",
197
+ "57/58 [============================>.] - ETA: 0s - loss: 0.0116\n",
198
+ "Epoch 3: val_loss improved from 0.01117 to 0.00990, saving model to lstm_energy_01.keras\n",
199
+ "58/58 [==============================] - 2s 36ms/step - loss: 0.0116 - val_loss: 0.0099\n",
200
+ "Epoch 4/5\n",
201
+ "57/58 [============================>.] - ETA: 0s - loss: 0.0084\n",
202
+ "Epoch 4: val_loss improved from 0.00990 to 0.00889, saving model to lstm_energy_01.keras\n",
203
+ "58/58 [==============================] - 2s 40ms/step - loss: 0.0084 - val_loss: 0.0089\n",
204
+ "Epoch 5/5\n",
205
+ "57/58 [============================>.] - ETA: 0s - loss: 0.0066\n",
206
+ "Epoch 5: val_loss did not improve from 0.00889\n",
207
+ "58/58 [==============================] - 2s 37ms/step - loss: 0.0066 - val_loss: 0.0103\n"
208
+ ]
209
+ },
210
+ {
211
+ "data": {
212
+ "text/plain": [
213
+ "<keras.callbacks.History at 0x1f4931dc790>"
214
+ ]
215
+ },
216
+ "execution_count": 101,
217
+ "metadata": {},
218
+ "output_type": "execute_result"
219
+ }
220
+ ],
221
+ "source": [
222
+ "train,test = traindataset,testdataset\n",
223
+ "days_in_past = 20\n",
224
+ "time_step = 1\n",
225
+ "def create_dataset(dataset,time_step):\n",
226
+ " x = [[] for _ in range(3)] \n",
227
+ " Y = [[] for _ in range(2)]\n",
228
+ " for i in range(time_step * days_in_past, len(dataset) - time_step * days_in_past): # -time_step is to ensure that the Y value has enough values\n",
229
+ " for j in range(3):\n",
230
+ " x[j].append(dataset[(i-time_step*days_in_past):i, j])\n",
231
+ " for j in range(2):\n",
232
+ " Y[j].append([dataset[x + i, j] for x in range(0,time_step)]) \n",
233
+ " x = [np.array(feature_list) for feature_list in x]\n",
234
+ " Y = [np.array(feature_list) for feature_list in Y] \n",
235
+ " Y = np.stack(Y,axis=1)\n",
236
+ " Y = np.reshape(Y, (Y.shape[0], time_step*2))\n",
237
+ " return np.stack(x,axis=2), Y\n",
238
+ "\n",
239
+ "\n",
240
+ "X_train, y_train = create_dataset(train, time_step)\n",
241
+ "X_test, y_test = create_dataset(test, time_step)\n",
242
+ "\n",
243
+ "\n",
244
+ "model = Sequential()\n",
245
+ "model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))\n",
246
+ "model.add(LSTM(units=50, return_sequences=True))\n",
247
+ "model.add(LSTM(units=30))\n",
248
+ "model.add(Dense(units=time_step*2))\n",
249
+ "\n",
250
+ "model.compile(optimizer='adam', loss='mean_squared_error')\n",
251
+ "\n",
252
+ "checkpoint_path = \"lstm_energy_01.keras\"\n",
253
+ "checkpoint_callback = ModelCheckpoint(filepath=checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')\n",
254
+ "model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5, batch_size=64, verbose=1, callbacks=[checkpoint_callback])"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "metadata": {},
261
+ "outputs": [],
262
+ "source": []
263
+ },
264
+ {
265
+ "cell_type": "code",
266
+ "execution_count": 95,
267
+ "metadata": {},
268
+ "outputs": [
269
+ {
270
+ "name": "stdout",
271
+ "output_type": "stream",
272
+ "text": [
273
+ "22/22 [==============================] - 0s 8ms/step - loss: 0.0126\n",
274
+ "22/22 [==============================] - 1s 7ms/step\n",
275
+ "Loss: 0.01257658563554287\n"
276
+ ]
277
+ }
278
+ ],
279
+ "source": [
280
+ "loss = model.evaluate(X_test, y_test)\n",
281
+ "test_predict1 = model.predict(X_test)\n",
282
+ "print(\"Loss: \", loss)\n",
283
+ "# Converting values back to the original scale\n",
284
+ "scalerBack = MinMaxScaler(feature_range=(mintest, maxtest))\n",
285
+ "test_predict2 = scalerBack.fit_transform(test_predict1)\n",
286
+ "y_test1 = scalerBack.fit_transform(y_test)\n"
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": 96,
292
+ "metadata": {},
293
+ "outputs": [],
294
+ "source": [
295
+ "%matplotlib qt\n",
296
+ "\n",
297
+ "# Create a 3x3 grid of subplots\n",
298
+ "fig, axes = plt.subplots(3, 3, figsize=(10, 10))\n",
299
+ "\n",
300
+ "# Loop over the value index\n",
301
+ "for i, ax in enumerate(axes.flat):\n",
302
+ " # Plot your data or perform any other operations\n",
303
+ " ax.plot(y_test1[i,0:time_step], label='Original Testing Data', color='blue')\n",
304
+ " ax.plot(test_predict2[i,0:time_step], label='Predicted Testing Data', color='red',alpha=0.8)\n",
305
+ " # ax.set_title(f'Plot {i+1}')\n",
306
+ " ax.set_title('Testing Data - Predicted vs Actual')\n",
307
+ " ax.set_xlabel('Time [hours]')\n",
308
+ " ax.set_ylabel('Energy Consumption [kW]') \n",
309
+ " ax.legend()\n",
310
+ "\n",
311
+ "# Adjust the spacing between subplots\n",
312
+ "plt.tight_layout()\n",
313
+ "\n",
314
+ "# Show the plot\n",
315
+ "plt.show()"
316
+ ]
317
+ },
318
+ {
319
+ "cell_type": "code",
320
+ "execution_count": null,
321
+ "metadata": {},
322
+ "outputs": [],
323
+ "source": [
324
+ "%matplotlib qt\n",
325
+ "index = 100\n",
326
+ "\n",
327
+ "\n",
328
+ "plt.plot(y_test[index,0:24], label='Original Testing Data', color='blue')\n",
329
+ "plt.plot(test_predict1[index,0:24], label='Predicted Testing Data', color='red',alpha=0.8)\n",
330
+ "\n",
331
+ "\n",
332
+ "plt.title('Testing Data - Predicted vs Actual')\n",
333
+ "plt.xlabel('Time [hours]')\n",
334
+ "plt.ylabel('Energy [kW]')\n",
335
+ "plt.legend()\n",
336
+ "plt.show()"
337
+ ]
338
+ },
339
+ {
340
+ "cell_type": "code",
341
+ "execution_count": null,
342
+ "metadata": {},
343
+ "outputs": [],
344
+ "source": [
345
+ "y_test[1, 0:24]"
346
+ ]
347
+ },
348
+ {
349
+ "cell_type": "code",
350
+ "execution_count": null,
351
+ "metadata": {},
352
+ "outputs": [],
353
+ "source": []
354
+ },
355
+ {
356
+ "cell_type": "code",
357
+ "execution_count": null,
358
+ "metadata": {},
359
+ "outputs": [],
360
+ "source": []
361
+ }
362
+ ],
363
+ "metadata": {
364
+ "kernelspec": {
365
+ "display_name": "experiments",
366
+ "language": "python",
367
+ "name": "python3"
368
+ },
369
+ "language_info": {
370
+ "codemirror_mode": {
371
+ "name": "ipython",
372
+ "version": 3
373
+ },
374
+ "file_extension": ".py",
375
+ "mimetype": "text/x-python",
376
+ "name": "python",
377
+ "nbconvert_exporter": "python",
378
+ "pygments_lexer": "ipython3",
379
+ "version": "3.8.15"
380
+ }
381
+ },
382
+ "nbformat": 4,
383
+ "nbformat_minor": 2
384
+ }
lstm_energy_01.keras ADDED
Binary file (545 kB). View file