{ "cells": [ { "cell_type": "code", "execution_count": 98, "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\")" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datehvac_Nhvac_Sair_temp_set_1solar_radiation_set_1
02018-01-01 00:00:00NaNNaN11.6486.7
12018-01-01 00:01:00NaNNaN11.6486.7
22018-01-01 00:02:00NaNNaN11.6486.7
32018-01-01 00:03:00NaNNaN11.6486.7
42018-01-01 00:04:00NaNNaN11.6486.7
\n", "
" ], "text/plain": [ " date hvac_N hvac_S air_temp_set_1 solar_radiation_set_1\n", "0 2018-01-01 00:00:00 NaN NaN 11.64 86.7\n", "1 2018-01-01 00:01:00 NaN NaN 11.64 86.7\n", "2 2018-01-01 00:02:00 NaN NaN 11.64 86.7\n", "3 2018-01-01 00:03:00 NaN NaN 11.64 86.7\n", "4 2018-01-01 00:04:00 NaN NaN 11.64 86.7" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "feature_list = ['date', 'hvac_N', 'hvac_S', 'air_temp_set_1', 'solar_radiation_set_1']\n", "extended_energy_data = all_data[feature_list]\n", "extended_energy_data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "energy_data = pd.read_csv(dataPATH + r\"\\hvac_data_1h.csv\")\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": null, "metadata": {}, "outputs": [], "source": [ "testdataset_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": null, "metadata": {}, "outputs": [], "source": [ "traindataset = traindataset.astype('float32')\n", "testdataset = testdataset.astype('float32')\n", "\n", "mintest = np.min(testdataset)\n", "maxtest = np.max(testdataset)\n", "\n", "scaler = MinMaxScaler(feature_range=(0, 1))\n", "traindataset = scaler.fit_transform(traindataset)\n", "testdataset = scaler.transform(testdataset)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/5\n", "57/58 [============================>.] - ETA: 0s - loss: 0.0238\n", "Epoch 1: val_loss improved from inf to 0.01717, saving model to lstm_energy_01.keras\n", "58/58 [==============================] - 11s 62ms/step - loss: 0.0238 - val_loss: 0.0172\n", "Epoch 2/5\n", "56/58 [===========================>..] - ETA: 0s - loss: 0.0139\n", "Epoch 2: val_loss improved from 0.01717 to 0.01117, saving model to lstm_energy_01.keras\n", "58/58 [==============================] - 2s 42ms/step - loss: 0.0139 - val_loss: 0.0112\n", "Epoch 3/5\n", "57/58 [============================>.] - ETA: 0s - loss: 0.0116\n", "Epoch 3: val_loss improved from 0.01117 to 0.00990, saving model to lstm_energy_01.keras\n", "58/58 [==============================] - 2s 36ms/step - loss: 0.0116 - val_loss: 0.0099\n", "Epoch 4/5\n", "57/58 [============================>.] - ETA: 0s - loss: 0.0084\n", "Epoch 4: val_loss improved from 0.00990 to 0.00889, saving model to lstm_energy_01.keras\n", "58/58 [==============================] - 2s 40ms/step - loss: 0.0084 - val_loss: 0.0089\n", "Epoch 5/5\n", "57/58 [============================>.] - ETA: 0s - loss: 0.0066\n", "Epoch 5: val_loss did not improve from 0.00889\n", "58/58 [==============================] - 2s 37ms/step - loss: 0.0066 - val_loss: 0.0103\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train,test = traindataset,testdataset\n", "days_in_past = 20\n", "time_step = 1\n", "def create_dataset(dataset,time_step):\n", " x = [[] for _ in range(3)] \n", " Y = [[] for _ in range(2)]\n", " 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", " for j in range(3):\n", " x[j].append(dataset[(i-time_step*days_in_past):i, j])\n", " for j in range(2):\n", " Y[j].append([dataset[x + i, j] for x in range(0,time_step)]) \n", " x = [np.array(feature_list) for feature_list in x]\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*2))\n", " return np.stack(x,axis=2), 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", "\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=30))\n", "model.add(Dense(units=time_step*2))\n", "\n", "model.compile(optimizer='adam', loss='mean_squared_error')\n", "\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": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "22/22 [==============================] - 0s 8ms/step - loss: 0.0126\n", "22/22 [==============================] - 1s 7ms/step\n", "Loss: 0.01257658563554287\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": 96, "metadata": {}, "outputs": [], "source": [ "%matplotlib qt\n", "\n", "# Create a 3x3 grid of subplots\n", "fig, axes = plt.subplots(3, 3, figsize=(10, 10))\n", "\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_test1[i,0:time_step], label='Original Testing Data', color='blue')\n", " ax.plot(test_predict2[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": [ "%matplotlib qt\n", "index = 100\n", "\n", "\n", "plt.plot(y_test[index,0:24], label='Original Testing Data', color='blue')\n", "plt.plot(test_predict1[index,0:24], label='Predicted Testing Data', color='red',alpha=0.8)\n", "\n", "\n", "plt.title('Testing Data - Predicted vs Actual')\n", "plt.xlabel('Time [hours]')\n", "plt.ylabel('Energy [kW]')\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y_test[1, 0:24]" ] }, { "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 }