You are on page 1of 1

In 

[1]:
import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

import xlrd

import datetime

from datetime import timedelta

In [2]:
data = pd.read_excel('Microgrid Data.xlsx')

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/1931149158.py:1: FutureWarning: Your version of xlrd is 1.2.0. In xlrd >= 2.0, only the xls format is supported. As a result, the
openpyxl engine will be used if it is installed and the engine argument is not specified. Install openpyxl instead.

data = pd.read_excel('Microgrid Data.xlsx')

In [3]:
entries = "Total rows: {0}".format(len(data))

print (entries)

Total rows: 8760

In [4]:
data.dtypes

No int64

Out[4]:
Date datetime64[ns]

Hour int64

Load float64

Wind Speed (m/s) float64

PV Production (kW) float64

Total Wind Production (kW) float64

dtype: object

In [5]:
data.describe()

Out[5]: No Hour Load Wind Speed (m/s) PV Production (kW) Total Wind Production (kW)

count 8760.000000 8760.000000 8760.000000 8760.000000 8760.000000 8760.000000

mean 4380.500000 11.500000 1206.993747 5.955160 465.103723 509.818825

std 2528.938512 6.922582 273.366649 0.895772 772.804194 298.686848

min 1.000000 0.000000 829.756000 3.266542 0.000000 11.059908

25% 2190.750000 5.750000 1005.100000 5.271204 0.000000 298.617511

50% 4380.500000 11.500000 1142.628000 5.942433 12.236907 442.396313

75% 6570.250000 17.250000 1317.428000 6.665088 617.963779 713.364055

max 8760.000000 23.000000 2643.228000 8.646830 3505.873715 1609.216590

In [6]:
data.dtypes

No int64

Out[6]:
Date datetime64[ns]

Hour int64

Load float64

Wind Speed (m/s) float64

PV Production (kW) float64

Total Wind Production (kW) float64

dtype: object

In [7]:
plt.figure(figsize = (18,9))

plt.plot(range(data.shape[0]),(data['Load']))

plt.xticks(range(0,data.shape[0],500),data['Date'].loc[::500],rotation=45)

plt.xlabel('Date',fontsize=18)

plt.ylabel('Load',fontsize=18)

plt.show()

In [8]:
data = pd.read_excel('Microgrid Data.xlsx')

from datetime import date

data["DAY_TYPE"] = data.Date.apply(lambda x: 1 if x.dayofweek > 5 else 0 )

data["Day_Name"] = data['Date'].dt.weekday

from pandas.tseries.holiday import Holiday, AbstractHolidayCalendar

class BelgianHolidaysCalendar(AbstractHolidayCalendar):

rules = [Holiday('NY', month=1, day=1),

Holiday('Easter', month=4, day=17),

Holiday('Labour day', month=5, day=1),

Holiday('Ascension day', month=5, day=25),

Holiday('White Monday day', month=6, day=5),

Holiday('Belgian National day', month=7, day=21),

Holiday('Assumption day', month=8, day=15),

Holiday('All Saints day', month=11, day=1),

Holiday('Armistice day', month=11, day=11),

Holiday('Xmas day', month=12, day=25)

cal = BelgianHolidaysCalendar()

holidays = cal.holidays(start = data.Date.min(), end = data.Date.max())

data["IS_HOLIDAY"] = data.Date.isin(holidays)

data.IS_HOLIDAY = data.IS_HOLIDAY.astype("int")

data.head()

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/2505659249.py:1: FutureWarning: Your version of xlrd is 1.2.0. In xlrd >= 2.0, only the xls format is supported. As a result, the
openpyxl engine will be used if it is installed and the engine argument is not specified. Install openpyxl instead.

data = pd.read_excel('Microgrid Data.xlsx')

Out[8]: No Date Hour Load Wind Speed (m/s) PV Production (kW) Total Wind Production (kW) DAY_TYPE Day_Name IS_HOLIDAY

0 1 2017-01-01 0 1133.028 5.633630 0.0 1105.990783 1 6 1

1 2 2017-01-01 1 1150.828 6.577402 0.0 658.064516 1 6 1

2 3 2017-01-01 2 1175.556 6.798660 0.0 1498.617512 1 6 1

3 4 2017-01-01 3 1183.288 7.709222 0.0 1200.000000 1 6 1

4 5 2017-01-01 4 1184.544 7.050374 0.0 658.064516 1 6 1

In [9]:
data = data[['Date','Load','DAY_TYPE', 'IS_HOLIDAY']]

data.head()

Out[9]: Date Load DAY_TYPE IS_HOLIDAY

0 2017-01-01 1133.028 1 1

1 2017-01-01 1150.828 1 1

2 2017-01-01 1175.556 1 1

3 2017-01-01 1183.288 1 1

4 2017-01-01 1184.544 1 1

In [10]:
import numpy

import matplotlib.pyplot as plt

import pandas

import math

from keras.models import Sequential

from keras.layers import Dense

from keras.layers import LSTM, RNN, GRU, SimpleRNN,Bidirectional

from keras.layers import Dropout

from sklearn.preprocessing import MinMaxScaler


from sklearn.metrics import mean_squared_error

In [11]:
minVal = min(data['Load']);

maxVal = max(data['Load']);

v = data['Load'].tolist()

norm=[]

for x in v:

x= (x- minVal) / ( maxVal - minVal )

norm.append(x)

data['Load'] = norm

data.head()

Out[11]: Date Load DAY_TYPE IS_HOLIDAY

0 2017-01-01 0.167233 1 1

1 2017-01-01 0.177048 1 1

2 2017-01-01 0.190684 1 1

3 2017-01-01 0.194948 1 1

4 2017-01-01 0.195640 1 1

In [12]:
data.tail()

Out[12]: Date Load DAY_TYPE IS_HOLIDAY

8755 2017-12-31 0.220404 1 0

8756 2017-12-31 0.216632 1 0

8757 2017-12-31 0.208632 1 0

8758 2017-12-31 0.184506 1 0

8759 2017-12-31 0.170680 1 0

In [13]:
for obs in range(1,49):

data["T_" + str(obs)] = data.Load.shift(obs)

data.fillna(0.00,inplace=True)

In [14]:
data.head()

Out[14]: Date Load DAY_TYPE IS_HOLIDAY T_1 T_2 T_3 T_4 T_5 T_6 ... T_39 T_40 T_41 T_42 T_43 T_44 T_45 T_46 T_47 T_48

0 2017-01-01 0.167233 1 1 0.000000 0.000000 0.000000 0.000000 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

1 2017-01-01 0.177048 1 1 0.167233 0.000000 0.000000 0.000000 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

2 2017-01-01 0.190684 1 1 0.177048 0.167233 0.000000 0.000000 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

3 2017-01-01 0.194948 1 1 0.190684 0.177048 0.167233 0.000000 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

4 2017-01-01 0.195640 1 1 0.194948 0.190684 0.177048 0.167233 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

5 rows × 52 columns

In [15]:
training_data = data[data.Date < pd.to_datetime("10/01/2017")]

val_mask = (data.Date >= pd.to_datetime("10/01/2017")) & (data.Date < pd.to_datetime("11/01/2017"))

val_data = data.loc[val_mask]

test_data = data[data.Date >= pd.to_datetime("11/01/2017")]

training_data = training_data.drop(["Date"],axis=1)

val_data = val_data.drop(["Date"],axis=1)

test_data = test_data.drop(["Date"],axis=1)

training_data.tail(3)

Out[15]: Load DAY_TYPE IS_HOLIDAY T_1 T_2 T_3 T_4 T_5 T_6 T_7 ... T_39 T_40 T_41 T_42 T_43 T_44 T_45 T_46 T_47 T_48

6549 0.072429 0 0 0.083649 0.106037 0.150090 0.176164 0.185995 0.213987 0.234313 ... 0.105638 0.101260 0.098265 0.081519 0.076430 0.075956 0.073594 0.071781 0.072109 0.083039

6550 0.064930 0 0 0.072429 0.083649 0.106037 0.150090 0.176164 0.185995 0.213987 ... 0.110497 0.105638 0.101260 0.098265 0.081519 0.076430 0.075956 0.073594 0.071781 0.072109

6551 0.061553 0 0 0.064930 0.072429 0.083649 0.106037 0.150090 0.176164 0.185995 ... 0.093606 0.110497 0.105638 0.101260 0.098265 0.081519 0.076430 0.075956 0.073594 0.071781

3 rows × 51 columns

In [16]:
training_data.head(2)

Out[16]: Load DAY_TYPE IS_HOLIDAY T_1 T_2 T_3 T_4 T_5 T_6 T_7 ... T_39 T_40 T_41 T_42 T_43 T_44 T_45 T_46 T_47 T_48

0 0.167233 1 1 0.000000 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

1 0.177048 1 1 0.167233 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

2 rows × 51 columns

In [17]:
x_train = training_data.drop(["Load"],axis=1)

y_train = training_data.Load

x_val = val_data.drop(["Load"],axis=1)

y_val = val_data.Load

x_test = test_data.drop(["Load"],axis=1)

y_test = test_data.Load

In [18]:
#APPLYING ANN(Artificial Neural Networks)

model=Sequential()

In [19]:
model.add(Dense(units=1,kernel_initializer='uniform',activation='relu',input_dim=50))

In [20]:
model.add(Dense(units=1,kernel_initializer='uniform',activation='relu'))

model.add(Dense(units=1,kernel_initializer='uniform',activation='sigmoid'))

"""model_k = Sequential()

model_k.add(LSTM(14, return_sequences=False, input_shape=(1,50)))

model_k.add(Dropout(0.1))

#model_k.add(LSTM(6, return_sequences=False))

#model_k.add(Dropout(0.1))

model_k.add(Dense(1))
model_k.compile(loss='mean_squared_error', optimizer='adam')"""

"model_k = Sequential()\nmodel_k.add(LSTM(14, return_sequences=False, input_shape=(1,50)))\nmodel_k.add(Dropout(0.1)) \n\n#model_k.add(LSTM(6, return_sequences=False)) \n#model_


Out[20]:
k.add(Dropout(0.1)) \n\nmodel_k.add(Dense(1))\nmodel_k.compile(loss='mean_squared_error', optimizer='adam')"

In [21]:
model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])

#model.compile(optimizer='adam',loss='mean_squared_error'""""metrics=['accuracy']""")

In [22]:
history=model.fit(x_train,y_train,batch_size=64,epochs=20)

Epoch 1/20
103/103 [==============================] - 0s 2ms/step - loss: 0.1047 - accuracy: 1.5263e-04

Epoch 2/20
103/103 [==============================] - 0s 1ms/step - loss: 0.0716 - accuracy: 1.5263e-04

Epoch 3/20
103/103 [==============================] - 0s 2ms/step - loss: 0.0421 - accuracy: 1.5263e-04

Epoch 4/20
103/103 [==============================] - 0s 2ms/step - loss: 0.0360 - accuracy: 1.5263e-04

Epoch 5/20
103/103 [==============================] - 0s 1ms/step - loss: 0.0335 - accuracy: 1.5263e-04

Epoch 6/20
103/103 [==============================] - 0s 1ms/step - loss: 0.0311 - accuracy: 1.5263e-04

Epoch 7/20
103/103 [==============================] - 0s 1ms/step - loss: 0.0284 - accuracy: 1.5263e-04

Epoch 8/20
103/103 [==============================] - 0s 2ms/step - loss: 0.0250 - accuracy: 1.5263e-04

Epoch 9/20
103/103 [==============================] - 0s 2ms/step - loss: 0.0211 - accuracy: 1.5263e-04

Epoch 10/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0188 - accuracy: 1.5263e-04

Epoch 11/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0176 - accuracy: 1.5263e-04

Epoch 12/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0168 - accuracy: 1.5263e-04

Epoch 13/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0162 - accuracy: 1.5263e-04

Epoch 14/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0156 - accuracy: 1.5263e-04

Epoch 15/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0149 - accuracy: 1.5263e-04

Epoch 16/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0142 - accuracy: 1.5263e-04

Epoch 17/20

103/103 [==============================] - 0s 1ms/step - loss: 0.0135 - accuracy: 1.5263e-04

Epoch 18/20

103/103 [==============================] - 0s 1ms/step - loss: 0.0127 - accuracy: 1.5263e-04

Epoch 19/20

103/103 [==============================] - 0s 2ms/step - loss: 0.0118 - accuracy: 1.5263e-04

Epoch 20/20

103/103 [==============================] - 0s 1ms/step - loss: 0.0109 - accuracy: 1.5263e-04

In [23]:
y_predicted=model.predict(x_test)

In [24]:
y_predicted=(y_predicted>0.5)

y_predicted

array([[False],

Out[24]:
[False],

[False],

...,

[False],

[False],

[False]])

In [25]:
#x_train_reshaped = x_train.values.reshape((x_train.shape[0], 1, x_train.shape[1]))

#x_val_reshaped = x_val.values.reshape((x_val.shape[0], 1, x_val.shape[1]))

#x_test_reshaped = x_test.values.reshape((x_test.shape[0], 1, x_test.shape[1]))

In [26]:
plt.plot(history.history['loss'], label='train', color = 'r')

plt.plot(history.history['accuracy'], label='val',color='b')

plt.ylabel('Loss',fontsize=12)

plt.xlabel('Epoch',fontsize=12)

plt.legend()

<matplotlib.legend.Legend at 0x295a6bb26a0>
Out[26]:

In [27]:
res = model.predict(x_test)

In [28]:
test_data["DL_PRED"] = res

In [29]:
from sklearn.metrics import mean_squared_error

from math import sqrt

testLoadList = test_data.Load.values.tolist()

testPredLoadList = test_data.DL_PRED.values.tolist()

rmse = [0]*len(testLoadList)

for i in range (0,len(testLoadList)):

rmse[i] = (testLoadList[i]-testPredLoadList[i])/testLoadList[i]

test_data['rmse']=rmse

from sklearn import datasets

from sklearn.model_selection import cross_val_predict

from sklearn import linear_model

import matplotlib.pyplot as plt

lr = linear_model.LinearRegression()

x = test_data['Load']

y = test_data["DL_PRED"]

fig,ax = plt.subplots()

ax.scatter(y, x)

ax.plot([y.min(), y.max()], [y.min(), y.max()], 'r', lw=2)

ax.set_xlabel('Measured')

ax.set_ylabel('Predicted')

fig.show()

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/3962006822.py:26: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backen


d, so cannot show the figure.

fig.show()

In [30]:
from numpy import sqrt

sqrt(mean_squared_error(test_data.Load,test_data.DL_PRED))

0.0997395563298242
Out[30]:

In [31]:
#APPLYING LSTM(Long Short Term Memory)

model_k = Sequential()

model_k.add(LSTM(14, return_sequences=False, input_shape=(1,50)))

model_k.add(Dropout(0.1))

#model_k.add(LSTM(6, return_sequences=False))

#model_k.add(Dropout(0.1))

model_k.add(Dense(1))

model_k.compile(loss='mean_squared_error', optimizer='adam')

In [32]:
x_train_reshaped = x_train.values.reshape((x_train.shape[0], 1, x_train.shape[1]))

x_val_reshaped = x_val.values.reshape((x_val.shape[0], 1, x_val.shape[1]))

x_test_reshaped = x_test.values.reshape((x_test.shape[0], 1, x_test.shape[1]))

In [33]:
history = model_k.fit(x_train_reshaped, y_train, validation_data=(x_val_reshaped, y_val), epochs=20, batch_size=64, verbose=2)

Epoch 1/20
103/103 - 1s - loss: 0.0214 - val_loss: 0.0035

Epoch 2/20
103/103 - 0s - loss: 0.0063 - val_loss: 0.0023

Epoch 3/20
103/103 - 0s - loss: 0.0044 - val_loss: 0.0022

Epoch 4/20
103/103 - 0s - loss: 0.0035 - val_loss: 0.0015

Epoch 5/20
103/103 - 0s - loss: 0.0029 - val_loss: 0.0014

Epoch 6/20
103/103 - 0s - loss: 0.0025 - val_loss: 0.0011

Epoch 7/20
103/103 - 0s - loss: 0.0022 - val_loss: 0.0010

Epoch 8/20
103/103 - 0s - loss: 0.0021 - val_loss: 8.7407e-04

Epoch 9/20
103/103 - 0s - loss: 0.0019 - val_loss: 7.5844e-04

Epoch 10/20

103/103 - 0s - loss: 0.0017 - val_loss: 8.4327e-04

Epoch 11/20

103/103 - 0s - loss: 0.0017 - val_loss: 6.4647e-04

Epoch 12/20

103/103 - 0s - loss: 0.0015 - val_loss: 6.1281e-04

Epoch 13/20

103/103 - 0s - loss: 0.0015 - val_loss: 5.6904e-04

Epoch 14/20

103/103 - 0s - loss: 0.0014 - val_loss: 5.4281e-04

Epoch 15/20

103/103 - 0s - loss: 0.0013 - val_loss: 4.9893e-04

Epoch 16/20

103/103 - 0s - loss: 0.0013 - val_loss: 5.3048e-04

Epoch 17/20

103/103 - 0s - loss: 0.0013 - val_loss: 4.5825e-04

Epoch 18/20

103/103 - 0s - loss: 0.0012 - val_loss: 4.1746e-04

Epoch 19/20

103/103 - 0s - loss: 0.0012 - val_loss: 4.0413e-04

Epoch 20/20

103/103 - 0s - loss: 0.0012 - val_loss: 4.0251e-04

In [34]:
plt.plot(history.history['loss'], label='train', color = 'r')

plt.plot(history.history['val_loss'], label='val',color='b')

plt.ylabel('Loss',fontsize=12)

plt.xlabel('Epoch',fontsize=12)

plt.legend()

<matplotlib.legend.Legend at 0x295ac4f52b0>
Out[34]:

In [35]:
res = model_k.predict(x_test_reshaped)

In [36]:
test_data["DL_PRED"] = res

In [37]:
from sklearn.metrics import mean_squared_error

from math import sqrt

testLoadList = test_data.Load.values.tolist()

testPredLoadList = test_data.DL_PRED.values.tolist()

rmse = [0]*len(testLoadList)

for i in range (0,len(testLoadList)):

rmse[i] = (testLoadList[i]-testPredLoadList[i])/testLoadList[i]

test_data['rmse']=rmse

from sklearn import datasets

from sklearn.model_selection import cross_val_predict

from sklearn import linear_model

import matplotlib.pyplot as plt

lr = linear_model.LinearRegression()

x = test_data['Load']

y = test_data["DL_PRED"]

fig,ax = plt.subplots()

ax.scatter(y, x)

ax.plot([y.min(), y.max()], [y.min(), y.max()], 'r', lw=2)

ax.set_xlabel('Measured')

ax.set_ylabel('Predicted')

fig.show()

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/3962006822.py:26: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backen


d, so cannot show the figure.

fig.show()

In [38]:
from scipy import interpolate

plt.figure(figsize=(20,10))

#test_data_withDates=data[(data['Date']>=datetime.date(2017,11,1))]

#test_data_xaxis = test_data_withDates['Date']

plt.tick_params(

axis='x', # changes apply to the x-axis

which='both', # both major and minor ticks are affected

bottom=False, # ticks along the bottom edge are off

top=False, # ticks along the top edge are off

labelbottom=False) # labels along the bottom edge are off

plt.plot(test_data['DL_PRED'],'r', label='Prediction')

plt.plot(test_data['Load'],'g')

plt.ylabel('Energy (Normalized)',fontsize=12)

plt.xlabel('Date: November & December 2017',fontsize=12)


plt.legend()

<matplotlib.legend.Legend at 0x295ac8d4f70>
Out[38]:

In [39]:
from numpy import sqrt

sqrt(mean_squared_error(test_data.Load,test_data.DL_PRED))

0.03140237174889139
Out[39]:

In [40]:
# Applying Bi LSTM

model_b=Sequential()

model_b.add(Bidirectional(LSTM(14,return_sequences=True,input_shape=(1,50))))

model_b.add(Bidirectional(LSTM(14)))

model_b.add(Dropout(0.1))

model_b.add(Dense(1))

model_b.compile(loss='mean_squared_error',optimizer='adam')

In [41]:
x_train_reshaped = x_train.values.reshape((x_train.shape[0], 1, x_train.shape[1]))

x_val_reshaped = x_val.values.reshape((x_val.shape[0], 1, x_val.shape[1]))

x_test_reshaped = x_test.values.reshape((x_test.shape[0], 1, x_test.shape[1]))

In [42]:
history_b = model_b.fit(x_train_reshaped, y_train, validation_data=(x_val_reshaped, y_val), epochs=20, batch_size=64, verbose=2)

Epoch 1/20
103/103 - 3s - loss: 0.0118 - val_loss: 0.0024

Epoch 2/20
103/103 - 0s - loss: 0.0039 - val_loss: 0.0017

Epoch 3/20
103/103 - 0s - loss: 0.0025 - val_loss: 0.0011

Epoch 4/20
103/103 - 0s - loss: 0.0019 - val_loss: 8.8361e-04

Epoch 5/20
103/103 - 1s - loss: 0.0016 - val_loss: 7.6346e-04

Epoch 6/20
103/103 - 1s - loss: 0.0013 - val_loss: 7.1679e-04

Epoch 7/20
103/103 - 1s - loss: 0.0012 - val_loss: 5.8352e-04

Epoch 8/20
103/103 - 1s - loss: 0.0011 - val_loss: 5.3146e-04

Epoch 9/20
103/103 - 1s - loss: 9.7443e-04 - val_loss: 4.6234e-04

Epoch 10/20

103/103 - 1s - loss: 9.8663e-04 - val_loss: 4.3623e-04

Epoch 11/20

103/103 - 1s - loss: 9.5133e-04 - val_loss: 4.3790e-04

Epoch 12/20

103/103 - 1s - loss: 8.9420e-04 - val_loss: 3.8131e-04

Epoch 13/20

103/103 - 1s - loss: 8.2696e-04 - val_loss: 3.9230e-04

Epoch 14/20

103/103 - 1s - loss: 7.8759e-04 - val_loss: 3.4957e-04

Epoch 15/20

103/103 - 1s - loss: 8.1772e-04 - val_loss: 3.3825e-04

Epoch 16/20

103/103 - 1s - loss: 7.6970e-04 - val_loss: 3.2239e-04

Epoch 17/20

103/103 - 1s - loss: 7.1228e-04 - val_loss: 3.3087e-04

Epoch 18/20

103/103 - 0s - loss: 7.3522e-04 - val_loss: 3.6715e-04

Epoch 19/20

103/103 - 1s - loss: 7.4365e-04 - val_loss: 3.0145e-04

Epoch 20/20

103/103 - 1s - loss: 7.1445e-04 - val_loss: 3.0103e-04

In [43]:
plt.plot(history_b.history['loss'], label='train', color = 'r')

plt.plot(history_b.history['val_loss'], label='val')

plt.ylabel('Loss',fontsize=12)

plt.xlabel('Epoch',fontsize=12)

plt.legend()

<matplotlib.legend.Legend at 0x295bc6e2940>
Out[43]:

In [44]:
res_b = model_b.predict(x_test_reshaped)

In [45]:
test_data["DL_PRED_blstm"] = res_b

x = test_data['Load']

y = test_data["DL_PRED_blstm"]

fig,ax = plt.subplots()

ax.scatter(y, x)

ax.plot([y.min(), y.max()], [y.min(), y.max()], 'r', lw=2)

ax.set_xlabel('Measured')

ax.set_ylabel('Predicted')

fig.show()

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/1514339170.py:10: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backen


d, so cannot show the figure.

fig.show()

In [46]:
plt.figure(figsize=(20,10))

#test_data_withDates=data[(data['Date']>=datetime.date(2017,11,1))]

#test_data_xaxis = test_data_withDates['Date']

plt.tick_params(

axis='x', # changes apply to the x-axis

which='both', # both major and minor ticks are affected

bottom=False, # ticks along the bottom edge are off

top=False, # ticks along the top edge are off

labelbottom=False) # labels along the bottom edge are off

plt.plot(test_data['DL_PRED_blstm'],'m',label='Predicted data')

plt.plot(test_data['Load'],'y',label='Test data')

plt.ylabel('Energy (Normalized)',fontsize=12)

plt.xlabel('Date: November & December 2017',fontsize=12)


plt.legend()

<matplotlib.legend.Legend at 0x295be35ff70>
Out[46]:

In [47]:
sqrt(mean_squared_error(test_data.Load,test_data.DL_PRED_blstm))

0.027553974603213235
Out[47]:

In [48]:
( maxVal - minVal ) * 0.02822239779954743 + minVal

880.9365281823409
Out[48]:

In [49]:
model_g = Sequential()

model_g.add(GRU(14, return_sequences=False, input_shape=(1,50)))

model_g.add(Dropout(0.1))

#model_k.add(LSTM(6, return_sequences=False))

#model_k.add(Dropout(0.1))

model_g.add(Dense(1))

model_g.compile(loss='mean_squared_error', optimizer='adam')

In [50]:
history_gru = model_g.fit(x_train_reshaped, y_train, validation_data=(x_val_reshaped, y_val), epochs=20, batch_size=64, verbose=2)

Epoch 1/20
103/103 - 1s - loss: 0.0085 - val_loss: 0.0018

Epoch 2/20
103/103 - 0s - loss: 0.0038 - val_loss: 0.0012

Epoch 3/20
103/103 - 0s - loss: 0.0029 - val_loss: 9.3192e-04

Epoch 4/20
103/103 - 0s - loss: 0.0025 - val_loss: 8.2663e-04

Epoch 5/20
103/103 - 0s - loss: 0.0022 - val_loss: 7.5406e-04

Epoch 6/20
103/103 - 0s - loss: 0.0020 - val_loss: 5.9814e-04

Epoch 7/20
103/103 - 0s - loss: 0.0017 - val_loss: 5.6195e-04

Epoch 8/20
103/103 - 0s - loss: 0.0016 - val_loss: 5.7181e-04

Epoch 9/20
103/103 - 0s - loss: 0.0015 - val_loss: 4.6241e-04

Epoch 10/20

103/103 - 0s - loss: 0.0014 - val_loss: 5.4730e-04

Epoch 11/20

103/103 - 0s - loss: 0.0014 - val_loss: 4.3736e-04

Epoch 12/20

103/103 - 0s - loss: 0.0013 - val_loss: 4.2272e-04

Epoch 13/20

103/103 - 0s - loss: 0.0013 - val_loss: 3.7549e-04

Epoch 14/20

103/103 - 0s - loss: 0.0012 - val_loss: 3.6645e-04

Epoch 15/20

103/103 - 0s - loss: 0.0011 - val_loss: 4.3364e-04

Epoch 16/20

103/103 - 0s - loss: 0.0011 - val_loss: 4.1554e-04

Epoch 17/20

103/103 - 0s - loss: 0.0011 - val_loss: 3.5896e-04

Epoch 18/20

103/103 - 0s - loss: 0.0010 - val_loss: 3.4745e-04

Epoch 19/20

103/103 - 0s - loss: 0.0010 - val_loss: 3.3236e-04

Epoch 20/20

103/103 - 0s - loss: 9.8582e-04 - val_loss: 2.9121e-04

In [51]:
plt.plot(history_gru.history['loss'], label='train', color = 'r')

plt.plot(history_gru.history['val_loss'], label='val')

plt.ylabel('Loss',fontsize=12)

plt.xlabel('Epoch',fontsize=12)

plt.legend()

<matplotlib.legend.Legend at 0x295c292b340>
Out[51]:

In [52]:
res_gru = model_g.predict(x_test_reshaped)

In [53]:
test_data["DL_PRED_GRU"] = res_gru

x = test_data['Load']

y = test_data["DL_PRED_GRU"]

fig,ax = plt.subplots()

ax.scatter(y, x)

ax.plot([y.min(), y.max()], [y.min(), y.max()], 'r', lw=2)

ax.set_xlabel('Measured')

ax.set_ylabel('Predicted')

fig.show()

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/3668570819.py:10: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backen


d, so cannot show the figure.

fig.show()

In [54]:
plt.figure(figsize=(20,10))

#test_data_withDates=data[(data['Date']>=datetime.date(2017,11,1))]

#test_data_xaxis = test_data_withDates['Date']

plt.tick_params(

axis='x', # changes apply to the x-axis

which='both', # both major and minor ticks are affected

bottom=False, # ticks along the bottom edge are off

top=False, # ticks along the top edge are off

labelbottom=False) # labels along the bottom edge are off

plt.plot(test_data['DL_PRED_GRU'],'m')

plt.plot(test_data['Load'],'g')

plt.legend()

No handles with labels found to put in legend.

<matplotlib.legend.Legend at 0x295c2cdcf70>
Out[54]:

In [55]:
sqrt(mean_squared_error(test_data.Load,test_data.DL_PRED_GRU))

0.029380574699640623
Out[55]:

In [56]:
( maxVal - minVal ) * 0.028088220676590434 + minVal

880.6932017268177
Out[56]:

In [57]:
model_rnn = Sequential()

model_rnn.add(SimpleRNN(14, return_sequences=False, input_shape=(1,50)))

model_rnn.add(Dropout(0.1))

#model_k.add(LSTM(6, return_sequences=False))

#model_k.add(Dropout(0.1))

model_rnn.add(Dense(1))

model_rnn.compile(loss='mean_squared_error', optimizer='adam')

In [58]:
history_rnn = model_rnn.fit(x_train_reshaped, y_train, validation_data=(x_val_reshaped, y_val), epochs=20, batch_size=64, verbose=2)

Epoch 1/20
103/103 - 1s - loss: 0.0433 - val_loss: 0.0043

Epoch 2/20
103/103 - 0s - loss: 0.0135 - val_loss: 0.0021

Epoch 3/20
103/103 - 0s - loss: 0.0083 - val_loss: 0.0014

Epoch 4/20
103/103 - 0s - loss: 0.0071 - val_loss: 0.0013

Epoch 5/20
103/103 - 0s - loss: 0.0055 - val_loss: 9.5713e-04

Epoch 6/20
103/103 - 0s - loss: 0.0046 - val_loss: 7.5007e-04

Epoch 7/20
103/103 - 0s - loss: 0.0040 - val_loss: 7.1998e-04

Epoch 8/20
103/103 - 0s - loss: 0.0036 - val_loss: 6.4958e-04

Epoch 9/20
103/103 - 0s - loss: 0.0032 - val_loss: 7.1173e-04

Epoch 10/20

103/103 - 0s - loss: 0.0027 - val_loss: 6.2715e-04

Epoch 11/20

103/103 - 0s - loss: 0.0027 - val_loss: 5.5088e-04

Epoch 12/20

103/103 - 0s - loss: 0.0025 - val_loss: 5.0393e-04

Epoch 13/20

103/103 - 0s - loss: 0.0023 - val_loss: 4.1565e-04

Epoch 14/20

103/103 - 0s - loss: 0.0022 - val_loss: 4.1169e-04

Epoch 15/20

103/103 - 0s - loss: 0.0020 - val_loss: 4.4663e-04

Epoch 16/20

103/103 - 0s - loss: 0.0019 - val_loss: 3.6769e-04

Epoch 17/20

103/103 - 0s - loss: 0.0018 - val_loss: 3.7209e-04

Epoch 18/20

103/103 - 0s - loss: 0.0017 - val_loss: 3.5715e-04

Epoch 19/20

103/103 - 0s - loss: 0.0017 - val_loss: 3.8111e-04

Epoch 20/20

103/103 - 0s - loss: 0.0015 - val_loss: 3.3611e-04

In [59]:
plt.plot(history_rnn.history['loss'], label='train', color = 'r')

plt.plot(history_rnn.history['val_loss'], label='val')

plt.ylabel('Loss',fontsize=12)

plt.xlabel('Epoch',fontsize=12)

plt.legend()

<matplotlib.legend.Legend at 0x295c58f1100>
Out[59]:

In [60]:
res_rnn = model_rnn.predict(x_test_reshaped)

In [61]:
test_data["DL_PRED_RNN"] = res_rnn

x = test_data['Load']

y = test_data["DL_PRED_RNN"]

fig,ax = plt.subplots()

ax.scatter(y, x)

ax.plot([y.min(), y.max()], [y.min(), y.max()], 'r', lw=2)

ax.set_xlabel('Measured')

ax.set_ylabel('Predicted')

fig.show()

C:\Users\YASHWA~1\AppData\Local\Temp/ipykernel_704/2361390719.py:10: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backen


d, so cannot show the figure.

fig.show()

In [62]:
sqrt(mean_squared_error(test_data.Load,test_data.DL_PRED_RNN))

0.03049433025548257
Out[62]:

In [63]:
plt.plot(history_rnn.history['loss'], label='trainRNN', color = '#FF0000')

plt.plot(history_b.history['loss'], label='trainblstm', color = '#FF7000')

plt.plot(history_b.history['val_loss'], label='valblstm', color = '#FF7800')

plt.plot(history_rnn.history['val_loss'], label='valRNN', color = '#FF0000')

plt.plot(history.history['loss'], label='trainLSTM', color = '#09CF00')

plt.plot(history.history['val_loss'], label='valLSTM', color = '#09CF00')

plt.plot(history_gru.history['loss'], label='trainGRU', color = '#0064FF')

plt.plot(history_gru.history['val_loss'], label='valGRU', color = '#0064FF')

plt.ylabel('Loss',fontsize=18)

plt.xlabel('Epoch',fontsize=18)

plt.legend()

<matplotlib.legend.Legend at 0x295c5ac1820>
Out[63]:

In [58]:
plt.figure(figsize=(20,10))

#test_data_withDates=data[(data['Date']>=datetime.date(2017,11,1))]

#test_data_xaxis = test_data_withDates['Date']

plt.tick_params(

axis='x', # changes apply to the x-axis

which='both', # both major and minor ticks are affected

bottom=False, # ticks along the bottom edge are off

top=False, # ticks along the top edge are off

labelbottom=False) # labels along the bottom edge are off

plt.plot(test_data['DL_PRED_GRU'],'m')

#plt.plot(test_data['PV'],'g')

plt.legend()

No handles with labels found to put in legend.

<matplotlib.legend.Legend at 0x14fd6767760>
Out[58]:

In [55]:
test_data['Load'].mean()

0.27030567660062255
Out[55]:

In [ ]:

You might also like