File size: 24,114 Bytes
c27effd
 
 
 
3beb62e
c27effd
3beb62e
c27effd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3beb62e
 
c27effd
 
 
 
3beb62e
c27effd
c38f36d
 
3beb62e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c38f36d
 
c27effd
d40a125
c27effd
 
d40a125
 
 
 
6a833f6
59b2cc9
 
6a833f6
59b2cc9
6a833f6
59b2cc9
6a833f6
 
 
d40a125
59b2cc9
 
 
 
 
6a833f6
c27effd
 
 
 
3beb62e
c27effd
 
 
3beb62e
6a833f6
 
c27effd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3beb62e
c27effd
c38f36d
 
 
 
 
 
 
3beb62e
c38f36d
 
 
 
c27effd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3beb62e
c27effd
 
 
 
 
 
d40a125
 
c27effd
 
 
 
 
 
59b2cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c27effd
 
3beb62e
c27effd
c38f36d
 
 
 
 
 
3beb62e
 
 
c38f36d
3beb62e
 
 
c38f36d
3beb62e
 
 
c38f36d
3beb62e
 
 
c38f36d
3beb62e
 
 
c38f36d
 
 
 
 
3beb62e
c38f36d
 
3beb62e
c38f36d
 
 
 
c27effd
 
c38f36d
3beb62e
c38f36d
 
c27effd
d40a125
 
 
 
 
 
3beb62e
c27effd
3beb62e
c27effd
3beb62e
d40a125
 
c27effd
 
 
 
 
59b2cc9
c27effd
 
 
 
 
 
 
59b2cc9
d40a125
c38f36d
 
 
 
 
59b2cc9
 
 
c38f36d
 
 
c27effd
 
 
 
 
 
 
 
 
 
 
 
59b2cc9
c27effd
59b2cc9
 
 
 
 
 
 
 
 
 
 
 
 
c27effd
 
 
 
 
 
3beb62e
c27effd
 
 
3beb62e
 
c27effd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3beb62e
 
 
 
 
c27effd
 
 
 
 
 
 
d40a125
3beb62e
 
 
 
 
d40a125
c27effd
59b2cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c27effd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "from datetime import datetime \n",
    "from datetime import date\n",
    "import matplotlib.pyplot as plt\n",
    "# import seaborn as sns\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from keras.models import Sequential\n",
    "from keras.layers import LSTM, Dense\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import MinMaxScaler,StandardScaler\n",
    "from keras.callbacks import ModelCheckpoint\n",
    "\n",
    "dataPATH = r\"C:\\Users\\levim\\OneDrive\\Documents\\MastersAI_ES\\TeamProject-5ARIP10\\smart-buildings\\Data\"\n",
    "# all_data = pd.read_csv(dataPATH + r\"\\long_merge.csv\")\n",
    "all_data = pd.read_csv(dataPATH + r\"\\extended_energy_data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>hvac_N</th>\n",
       "      <th>hvac_S</th>\n",
       "      <th>air_temp_set_1</th>\n",
       "      <th>solar_radiation_set_1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-01 00:00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.64</td>\n",
       "      <td>86.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-01 00:15:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.49</td>\n",
       "      <td>45.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-01 00:30:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.59</td>\n",
       "      <td>51.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-01 00:45:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.44</td>\n",
       "      <td>21.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-01 01:00:00</td>\n",
       "      <td>37.400002</td>\n",
       "      <td>19.5</td>\n",
       "      <td>11.12</td>\n",
       "      <td>6.45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 date     hvac_N  hvac_S  air_temp_set_1  \\\n",
       "0 2018-01-01 00:00:00        NaN     NaN           11.64   \n",
       "1 2018-01-01 00:15:00        NaN     NaN           11.49   \n",
       "2 2018-01-01 00:30:00        NaN     NaN           11.59   \n",
       "3 2018-01-01 00:45:00        NaN     NaN           11.44   \n",
       "4 2018-01-01 01:00:00  37.400002    19.5           11.12   \n",
       "\n",
       "   solar_radiation_set_1  \n",
       "0                  86.70  \n",
       "1                  45.88  \n",
       "2                  51.62  \n",
       "3                  21.43  \n",
       "4                   6.45  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Prepar energy data set with extended features\n",
    "feature_list = ['date', 'hvac_N', 'hvac_S', 'air_temp_set_1', 'solar_radiation_set_1']\n",
    "extended_energy_data = all_data[feature_list]\n",
    "\n",
    "extended_energy_data['date'] = pd.to_datetime(extended_energy_data['date'])\n",
    "extended_energy_data.set_index('date', inplace=True)\n",
    "\n",
    "eed_15m = extended_energy_data.resample('15T').mean()\n",
    "eed_1h = extended_energy_data.resample('60T').mean()\n",
    "\n",
    "eed_15m = eed_15m.reset_index(drop=False)\n",
    "eed_1h = eed_1h.reset_index(drop=False)\n",
    "\n",
    "window_size = 4*4 # 4 hours\n",
    "eed_15m_avg = eed_15m.copy()\n",
    "eed_15m_avg['hvac_N'] = eed_15m['hvac_N'].rolling(window=window_size).mean()\n",
    "eed_15m_avg['hvac_S'] = eed_15m['hvac_S'].rolling(window=window_size).mean()\n",
    "\n",
    "window_size = 4 # 4 hours\n",
    "eed_1h_avg = eed_1h.copy()\n",
    "eed_1h_avg['hvac_N'] = eed_1h['hvac_N'].rolling(window=window_size).mean()\n",
    "eed_1h_avg['hvac_S'] = eed_1h['hvac_S'].rolling(window=window_size).mean()\n",
    "\n",
    "eed_15m.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# energy_data = pd.read_csv(dataPATH + r\"\\extended_energy_data.csv\")\n",
    "# energy_data = eed_15m\n",
    "energy_data = eed_15m_avg\n",
    "\n",
    "# Convert the date column to datetime\n",
    "energy_data['date'] = pd.to_datetime(energy_data['date'], format = \"%Y-%m-%d %H:%M:%S\")\n",
    "\n",
    "energy_data['day_of_week'] = energy_data['date'].dt.weekday\n",
    "# Filter the data for the year 2019\n",
    "df_filtered = energy_data[ (energy_data.date.dt.date >date(2019, 1, 20)) & (energy_data.date.dt.date< date(2019, 7, 26))]\n",
    "\n",
    "# Check for NA values in the DataFrame\n",
    "if df_filtered.isna().any().any():\n",
    "    print(\"There are NA values in the DataFrame columns.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testdataset_df = df_filtered[(df_filtered.date.dt.date <date(2019, 2, 20))]\n",
    "\n",
    "traindataset_df = df_filtered[ (df_filtered.date.dt.date >date(2019, 2, 21))]\n",
    "\n",
    "testdataset = testdataset_df.drop(columns=[\"date\"]).values\n",
    "\n",
    "traindataset = traindataset_df.drop(columns=[\"date\"]).values\n",
    "\n",
    "columns_with_na = traindataset_df.columns[traindataset_df.isna().any()].tolist()\n",
    "columns_with_na"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "traindataset = traindataset.astype('float32')\n",
    "testdataset = testdataset.astype('float32')\n",
    "\n",
    "mintest = np.min(testdataset[:,0:2])\n",
    "maxtest = np.max(testdataset[:,0:2])\n",
    "\n",
    "scaler = MinMaxScaler(feature_range=(0, 1))\n",
    "traindataset = scaler.fit_transform(traindataset)\n",
    "testdataset = scaler.transform(testdataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_model(X_train, time_step, no_outputs):\n",
    "    model = Sequential()\n",
    "    model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))\n",
    "    model.add(LSTM(units=50, return_sequences=True))\n",
    "    model.add(LSTM(units=time_step*no_outputs))\n",
    "    model.add(Dense(units=time_step*no_outputs))\n",
    "\n",
    "    model.compile(optimizer='adam', loss='mean_squared_error')\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Model 1 (continous predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "225/229 [============================>.] - ETA: 0s - loss: 0.0130\n",
      "Epoch 1: val_loss improved from inf to 0.01885, saving model to lstm_energy_01.keras\n",
      "229/229 [==============================] - 8s 16ms/step - loss: 0.0129 - val_loss: 0.0189\n",
      "Epoch 2/5\n",
      "229/229 [==============================] - ETA: 0s - loss: 0.0058\n",
      "Epoch 2: val_loss did not improve from 0.01885\n",
      "229/229 [==============================] - 3s 11ms/step - loss: 0.0058 - val_loss: 0.0192\n",
      "Epoch 3/5\n",
      "225/229 [============================>.] - ETA: 0s - loss: 0.0052\n",
      "Epoch 3: val_loss improved from 0.01885 to 0.01818, saving model to lstm_energy_01.keras\n",
      "229/229 [==============================] - 3s 11ms/step - loss: 0.0052 - val_loss: 0.0182\n",
      "Epoch 4/5\n",
      "225/229 [============================>.] - ETA: 0s - loss: 0.0045\n",
      "Epoch 4: val_loss did not improve from 0.01818\n",
      "229/229 [==============================] - 3s 11ms/step - loss: 0.0045 - val_loss: 0.0190\n",
      "Epoch 5/5\n",
      "226/229 [============================>.] - ETA: 0s - loss: 0.0041\n",
      "Epoch 5: val_loss did not improve from 0.01818\n",
      "229/229 [==============================] - 3s 11ms/step - loss: 0.0041 - val_loss: 0.0186\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x25e69ac0370>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train,test = traindataset,testdataset\n",
    "steps_in_past   = 3   \n",
    "time_step       = 4*6\n",
    "no_inputs       = 5\n",
    "no_outputs      = 2\n",
    "def create_dataset(dataset,time_step):\n",
    "    x = [[] for _ in range(no_inputs)] \n",
    "    Y = [[] for _ in range(no_outputs)]\n",
    "    for i in range(time_step * steps_in_past, len(dataset) - time_step * steps_in_past): # -time_step is to ensure that the Y value has enough values\n",
    "        for j in range(no_inputs):\n",
    "            x[j].append(dataset[(i-time_step*steps_in_past):i, j])\n",
    "        for j in range(no_outputs):\n",
    "            Y[j].append(dataset[i:i+time_step, j])    \n",
    "    x = [np.array(feature_list) for feature_list in x]\n",
    "    x = np.stack(x,axis=1)\n",
    "    Y = [np.array(feature_list) for feature_list in Y] \n",
    "    Y = np.stack(Y,axis=1)\n",
    "    Y = np.reshape(Y, (Y.shape[0], time_step*no_outputs))\n",
    "    return x, Y\n",
    "\n",
    "\n",
    "X_train, y_train = create_dataset(train, time_step)\n",
    "X_test, y_test = create_dataset(test, time_step)\n",
    "\n",
    "model = create_model(X_train, time_step, no_outputs)\n",
    "checkpoint_path = \"lstm_energy_01.keras\"\n",
    "checkpoint_callback = ModelCheckpoint(filepath=checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')\n",
    "model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5, batch_size=64, verbose=1, callbacks=[checkpoint_callback])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4/4 [==============================] - 0s 4ms/step - loss: 0.0153\n",
      "4/4 [==============================] - 1s 4ms/step\n",
      "Loss:  0.01531214825809002\n"
     ]
    }
   ],
   "source": [
    "loss            = model.evaluate(X_test, y_test)\n",
    "test_predict1   = model.predict(X_test)\n",
    "print(\"Loss: \", loss)\n",
    "# Converting values back to the original scale\n",
    "scalerBack      = MinMaxScaler(feature_range=(mintest, maxtest))\n",
    "test_predict2   = scalerBack.fit_transform(test_predict1)\n",
    "y_test1         = scalerBack.fit_transform(y_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 106 is out of bounds for axis 0 with size 106",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[52], line 10\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# Loop over the value index\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, ax \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(axes\u001b[38;5;241m.\u001b[39mflat):\n\u001b[0;32m      9\u001b[0m     \u001b[38;5;66;03m# Plot your data or perform any other operations\u001b[39;00m\n\u001b[1;32m---> 10\u001b[0m     ax\u001b[38;5;241m.\u001b[39mplot(\u001b[43my_test\u001b[49m\u001b[43m[\u001b[49m\u001b[43mvar\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43mi\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43mtime_step\u001b[49m\u001b[43m]\u001b[49m, label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mOriginal Testing Data\u001b[39m\u001b[38;5;124m'\u001b[39m, color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mblue\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     11\u001b[0m     ax\u001b[38;5;241m.\u001b[39mplot(test_predict1[var\u001b[38;5;241m+\u001b[39mi,\u001b[38;5;241m0\u001b[39m:time_step], label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPredicted Testing Data\u001b[39m\u001b[38;5;124m'\u001b[39m, color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mred\u001b[39m\u001b[38;5;124m'\u001b[39m,alpha\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0.8\u001b[39m)\n\u001b[0;32m     12\u001b[0m     \u001b[38;5;66;03m# ax.set_title(f'Plot {i+1}')\u001b[39;00m\n",
      "\u001b[1;31mIndexError\u001b[0m: index 106 is out of bounds for axis 0 with size 106"
     ]
    }
   ],
   "source": [
    "%matplotlib qt\n",
    "\n",
    "# Create a 3x3 grid of subplots\n",
    "fig, axes = plt.subplots(3, 3, figsize=(10, 10))\n",
    "\n",
    "var = 100\n",
    "# Loop over the value index\n",
    "for i, ax in enumerate(axes.flat):\n",
    "    # Plot your data or perform any other operations\n",
    "    ax.plot(y_test[var+i,0:time_step], label='Original Testing Data', color='blue')\n",
    "    ax.plot(test_predict1[var+i,0:time_step], label='Predicted Testing Data', color='red',alpha=0.8)\n",
    "    # ax.set_title(f'Plot {i+1}')\n",
    "    ax.set_title('Testing Data - Predicted vs Actual')\n",
    "    ax.set_xlabel('Time [hours]')\n",
    "    ax.set_ylabel('Energy Consumption [kW]')     \n",
    "    ax.legend()\n",
    "\n",
    "# Adjust the spacing between subplots\n",
    "plt.tight_layout()\n",
    "\n",
    "# Show the plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Autoregressive prediction\n",
    "X_pred = testdataset.copy()\n",
    "for i in range(steps_in_past,steps_in_past*2):\n",
    "    xin = X_pred[i-steps_in_past:i].reshape((1, steps_in_past, no_outputs)) \n",
    "    X_pred[i] = model.predict(xin, verbose = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot prediction vs actual for test data\n",
    "plt.figure()\n",
    "plt.plot(X_pred[steps_in_past:steps_in_past*2,0],':',label='LSTM')\n",
    "plt.plot(testdataset[steps_in_past:steps_in_past*2,0],'--',label='Actual')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Model 2 (Predicting once per day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0850 \n",
      "Epoch 1: val_loss improved from inf to 0.07467, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 7s 131ms/step - loss: 0.0791 - val_loss: 0.0747\n",
      "Epoch 2/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0487\n",
      "Epoch 2: val_loss improved from 0.07467 to 0.03484, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 20ms/step - loss: 0.0419 - val_loss: 0.0348\n",
      "Epoch 3/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0262\n",
      "Epoch 3: val_loss improved from 0.03484 to 0.02388, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 17ms/step - loss: 0.0241 - val_loss: 0.0239\n",
      "Epoch 4/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0180\n",
      "Epoch 4: val_loss improved from 0.02388 to 0.02059, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 18ms/step - loss: 0.0174 - val_loss: 0.0206\n",
      "Epoch 5/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0134\n",
      "Epoch 5: val_loss improved from 0.02059 to 0.01839, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 18ms/step - loss: 0.0130 - val_loss: 0.0184\n",
      "Epoch 6/20\n",
      " 8/10 [=======================>......] - ETA: 0s - loss: 0.0107\n",
      "Epoch 6: val_loss did not improve from 0.01839\n",
      "10/10 [==============================] - 0s 21ms/step - loss: 0.0106 - val_loss: 0.0255\n",
      "Epoch 7/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0090\n",
      "Epoch 7: val_loss did not improve from 0.01839\n",
      "10/10 [==============================] - 0s 14ms/step - loss: 0.0090 - val_loss: 0.0261\n",
      "Epoch 8/20\n",
      "10/10 [==============================] - ETA: 0s - loss: 0.0085\n",
      "Epoch 8: val_loss did not improve from 0.01839\n",
      "10/10 [==============================] - 0s 18ms/step - loss: 0.0085 - val_loss: 0.0197\n",
      "Epoch 9/20\n",
      " 9/10 [==========================>...] - ETA: 0s - loss: 0.0074\n",
      "Epoch 9: val_loss improved from 0.01839 to 0.01687, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 22ms/step - loss: 0.0074 - val_loss: 0.0169\n",
      "Epoch 10/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0066\n",
      "Epoch 10: val_loss did not improve from 0.01687\n",
      "10/10 [==============================] - 0s 14ms/step - loss: 0.0068 - val_loss: 0.0171\n",
      "Epoch 11/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0063\n",
      "Epoch 11: val_loss did not improve from 0.01687\n",
      "10/10 [==============================] - 0s 14ms/step - loss: 0.0061 - val_loss: 0.0191\n",
      "Epoch 12/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0057\n",
      "Epoch 12: val_loss improved from 0.01687 to 0.01678, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 18ms/step - loss: 0.0057 - val_loss: 0.0168\n",
      "Epoch 13/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0052\n",
      "Epoch 13: val_loss did not improve from 0.01678\n",
      "10/10 [==============================] - 0s 13ms/step - loss: 0.0058 - val_loss: 0.0206\n",
      "Epoch 14/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0058\n",
      "Epoch 14: val_loss improved from 0.01678 to 0.01612, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 20ms/step - loss: 0.0062 - val_loss: 0.0161\n",
      "Epoch 15/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0061\n",
      "Epoch 15: val_loss did not improve from 0.01612\n",
      "10/10 [==============================] - 0s 14ms/step - loss: 0.0059 - val_loss: 0.0184\n",
      "Epoch 16/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0054\n",
      "Epoch 16: val_loss improved from 0.01612 to 0.01561, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 17ms/step - loss: 0.0053 - val_loss: 0.0156\n",
      "Epoch 17/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0046\n",
      "Epoch 17: val_loss did not improve from 0.01561\n",
      "10/10 [==============================] - 0s 13ms/step - loss: 0.0048 - val_loss: 0.0166\n",
      "Epoch 18/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0054\n",
      "Epoch 18: val_loss improved from 0.01561 to 0.01503, saving model to lstm_energy_01.keras\n",
      "10/10 [==============================] - 0s 18ms/step - loss: 0.0052 - val_loss: 0.0150\n",
      "Epoch 19/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0050\n",
      "Epoch 19: val_loss did not improve from 0.01503\n",
      "10/10 [==============================] - 0s 13ms/step - loss: 0.0046 - val_loss: 0.0156\n",
      "Epoch 20/20\n",
      " 6/10 [=================>............] - ETA: 0s - loss: 0.0045\n",
      "Epoch 20: val_loss did not improve from 0.01503\n",
      "10/10 [==============================] - 0s 14ms/step - loss: 0.0045 - val_loss: 0.0153\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x25e3a8cf640>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train,test = traindataset,testdataset\n",
    "steps_in_past   = 7   \n",
    "time_step       = 24\n",
    "no_inputs       = 5\n",
    "no_outputs      = 2\n",
    "def create_dataset(dataset,time_step):\n",
    "    x = [[] for _ in range(no_inputs)] \n",
    "    Y = [[] for _ in range(no_outputs)]\n",
    "    for i in range(steps_in_past, round(len(dataset)/24) - steps_in_past): # -time_step is to ensure that the Y value has enough values\n",
    "        for j in range(no_inputs):\n",
    "            x[j].append(dataset[(i-steps_in_past)*time_step:i*time_step, j])\n",
    "        for j in range(no_outputs):\n",
    "            Y[j].append(dataset[i*time_step:(i+1)*time_step, j])    \n",
    "    x = [np.array(feature_list) for feature_list in x]\n",
    "    x = np.stack(x,axis=1)\n",
    "    Y = [np.array(feature_list) for feature_list in Y] \n",
    "    Y = np.stack(Y,axis=1)\n",
    "    Y = np.reshape(Y, (Y.shape[0], time_step*no_outputs))\n",
    "    return x, Y\n",
    "\n",
    "\n",
    "X_train, y_train = create_dataset(train, time_step)\n",
    "X_test, y_test = create_dataset(test, time_step)\n",
    "\n",
    "model = create_model(X_train, time_step, no_outputs)\n",
    "checkpoint_path = \"lstm_energy_01.keras\"\n",
    "checkpoint_callback = ModelCheckpoint(filepath=checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')\n",
    "model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=20, batch_size=64, verbose=1, callbacks=[checkpoint_callback])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "experiments",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}