[PYTHON] Power demand forecast by GRU

1.First of all

Hello. It's been about three months since I started learning machine learning, and it seems that my understanding has gradually deepened. This time, I would like to use what I have learned so far to forecast electricity demand. Specifically, I would like to make predictions using GRU, which is a type of deep learning model that can handle time-series data.

2. Development environment

OS : Windows 10 Python execution environment: Google colaboratory

If you use Google colaboratory, you can develop without building the environment. Also, GPU is provided by Google (with time limit)

3. About GRU

With a normal RNN, there is a problem that the gradient disappears / explodes when handling long-term time series data, and learning cannot be performed well. Therefore, in LSRM, the concept of cells and three gates (input gate, output gate, forgetting gate) is adopted to maintain the state, and it is a model that enables long-term dependent learning. Roughly speaking, GRU is a simple model of LSTM, and it is generally said that the amount of calculation is smaller than that of LSTM.

4. Module used

This time, I would like to make a prediction using the GRU mentioned earlier.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Activation
from keras.layers import GRU
from keras.layers import Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

5. Data used and its preprocessing

● Data used

This time I used Kaggle's open data (https://www.kaggle.com/manualrg/spanish-electricity-market-demand-gen-price). The daily demand power data is read in the data frame format. And since the original format includes time information, it is changed to display up to the date. The data from 2014 to 2017 is divided for training and the data for 2018 is divided for testing.

data = pd.read_csv("/content/drive/My Drive/data/spain_energy_market.csv", sep=",", parse_dates=["datetime"])
data = data[data["name"]=="Demanda programada PBF total"]#.set_index("datetime")
data["date"] = data["datetime"].dt.date
data.set_index("date", inplace=True)
data = data[["value"]]
data = data.asfreq("D")
data = data.rename(columns={"value": "energy"})
train_series = data.loc["2014":"2017"].values
test_series  = data.loc["2018"].values

If you plot the data, you will get a waveform like this. energy_raw.PNG

● Data processing

Next, we will normalize each dataset based on the distribution in the training data.


scaler = MinMaxScaler(feature_range=(0, 1))
scaler_train = scaler.fit(train_series)
train = scaler_train.transform(train_series)
test = scaler_train.transform(test_series)

Next, this time, we will use 20 points of data to predict the data one period ahead, so for that purpose, the latest 20 points (20 points behind from the point of prediction) and their label data ( We will divide it into (data one period ahead).

def create_dataset(dataset, look_back):
    data_X, data_Y = [], []
    for i in range(look_back, len(dataset)):
        data_X.append(dataset[i-look_back:i, 0])
        data_Y.append(dataset[i, 0])
    return np.array(data_X), np.array(data_Y)

Finally, format the data to add to the GRU model.

train_X = train_X.reshape(train_X.shape[0], train_X.shape[1], 1)
test_X = test_X.reshape(test_X.shape[0], test_X.shape[1], 1)

6. Model definition

This time, we will build a GRU model using the machine learning library "Keras". The model construction is as follows.

#Creating a GRU model
model = Sequential()
model.add(GRU(40, return_sequences=False, batch_input_shape=(None, look_back, 1)))
model.add(Dropout(0.25))
model.add(Dense(1))
model.add(Activation("linear"))
model.compile(loss='mean_squared_error', optimizer='adam')

7. Data learning and prediction results

● Learning and prediction

You can train the model with model.fit (). Since it is an epoch here, it is iteratively processed 20 times and learning is performed.

history = model.fit(train_X, train_Y, epochs=20, batch_size=1, verbose=1)

And the prediction in the model can be done with model.predict (). By passing the test data as input to this, the prediction of one period ahead is made.

train_predict = model.predict(train_X)
test_predict = model.predict(test_X)

● Forecast results

The learning loss results are as follows. It seems that the value of the loss function could be kept small. loss.PNG Next, the results of the one-term forecast using the test data are as follows. forecast_testdata.PNG There seems to be a rough flow, but the result was a big deviation in some places. You may get good results by resetting the hyperparameters etc. and learning. There seemed to be various points to be improved in the pretreatment process.

7. Summary

This time, we used the GRU model, which is a variant of the RNN model, to forecast the power demand. As a result, good results were not obtained. In the future, I will review the data preprocessing method and remodel it so that good accuracy can be obtained.

Recommended Posts

Power demand forecast by GRU
Try to forecast power demand by machine learning
Story of power approximation by Python