Professional Documents
Culture Documents
[1]:
import numpy as np
import pandas as pd
import xlrd
import datetime
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.
In [3]:
entries = "Total rows: {0}".format(len(data))
print (entries)
In [4]:
data.dtypes
No int64
Out[4]:
Date datetime64[ns]
Hour int64
Load float64
dtype: object
In [5]:
data.describe()
Out[5]: No Hour Load Wind Speed (m/s) PV Production (kW) Total Wind Production (kW)
In [6]:
data.dtypes
No int64
Out[6]:
Date datetime64[ns]
Hour int64
Load 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')
data["Day_Name"] = data['Date'].dt.weekday
class BelgianHolidaysCalendar(AbstractHolidayCalendar):
cal = BelgianHolidaysCalendar()
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.
Out[8]: No Date Hour Load Wind Speed (m/s) PV Production (kW) Total Wind Production (kW) DAY_TYPE Day_Name IS_HOLIDAY
In [9]:
data = data[['Date','Load','DAY_TYPE', 'IS_HOLIDAY']]
data.head()
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 pandas
import math
In [11]:
minVal = min(data['Load']);
maxVal = max(data['Load']);
v = data['Load'].tolist()
norm=[]
for x in v:
norm.append(x)
data['Load'] = norm
data.head()
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()
In [13]:
for obs in range(1,49):
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_data = data.loc[val_mask]
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(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 [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
Epoch 11/20
Epoch 12/20
Epoch 13/20
Epoch 14/20
Epoch 15/20
Epoch 16/20
Epoch 17/20
Epoch 18/20
Epoch 19/20
Epoch 20/20
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]))
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
testLoadList = test_data.Load.values.tolist()
testPredLoadList = test_data.DL_PRED.values.tolist()
rmse = [0]*len(testLoadList)
rmse[i] = (testLoadList[i]-testPredLoadList[i])/testLoadList[i]
test_data['rmse']=rmse
lr = linear_model.LinearRegression()
x = test_data['Load']
y = test_data["DL_PRED"]
fig,ax = plt.subplots()
ax.scatter(y, x)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()
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(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]))
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
Epoch 11/20
Epoch 12/20
Epoch 13/20
Epoch 14/20
Epoch 15/20
Epoch 16/20
Epoch 17/20
Epoch 18/20
Epoch 19/20
Epoch 20/20
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
testLoadList = test_data.Load.values.tolist()
testPredLoadList = test_data.DL_PRED.values.tolist()
rmse = [0]*len(testLoadList)
rmse[i] = (testLoadList[i]-testPredLoadList[i])/testLoadList[i]
test_data['rmse']=rmse
lr = linear_model.LinearRegression()
x = test_data['Load']
y = test_data["DL_PRED"]
fig,ax = plt.subplots()
ax.scatter(y, x)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()
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(
plt.plot(test_data['DL_PRED'],'r', label='Prediction')
plt.plot(test_data['Load'],'g')
plt.ylabel('Energy (Normalized)',fontsize=12)
<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]))
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
Epoch 11/20
Epoch 12/20
Epoch 13/20
Epoch 14/20
Epoch 15/20
Epoch 16/20
Epoch 17/20
Epoch 18/20
Epoch 19/20
Epoch 20/20
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.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()
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(
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)
<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(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
Epoch 11/20
Epoch 12/20
Epoch 13/20
Epoch 14/20
Epoch 15/20
Epoch 16/20
Epoch 17/20
Epoch 18/20
Epoch 19/20
Epoch 20/20
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.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()
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(
plt.plot(test_data['DL_PRED_GRU'],'m')
plt.plot(test_data['Load'],'g')
plt.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(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
Epoch 11/20
Epoch 12/20
Epoch 13/20
Epoch 14/20
Epoch 15/20
Epoch 16/20
Epoch 17/20
Epoch 18/20
Epoch 19/20
Epoch 20/20
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.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()
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.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(
plt.plot(test_data['DL_PRED_GRU'],'m')
#plt.plot(test_data['PV'],'g')
plt.legend()
<matplotlib.legend.Legend at 0x14fd6767760>
Out[58]:
In [55]:
test_data['Load'].mean()
0.27030567660062255
Out[55]:
In [ ]: