Time series forecasting - LSTM implements multi-variable multi-step load forecasting (Tensorflow)

Table of contents

1 Data processing

1.1 Introduction to data sets

1.2 Data set processing

2 Model training and prediction

2.1 Model training

2.2 Model multi-step prediction

2.3 Results visualization


Number of calculations

1.1 Introduction to data sets

The experimental data set uses Data Set 6: Australian electricity load and price forecast data (Download link), including the data set including date and hour , dry bulb temperature, dew point temperature, wet bulb temperature, humidity, electricity price, power load characteristics, time interval 30min.

Looking at the partial load data separately, we found that there are strong regularities.

1.2 Data set processing

First, check the missing values ​​of the data. From the statistical data, you can see that the data is relatively complete and there are no missing values. Other outliers and data handling can be handled on your own.

# 缺失值统计
data.isnull().sum()

It is planned to predict 48 data for the next day. The data to be predicted will be retained (that is, the unknown data in the future), the previously trained data (that is, historical data) will be extracted separately, and the data set will be divided on a rolling basis. It is different from the division method in the previous article. Because it is multi-variable, features and labels are divided separately. Otherwise, there will be many problems in subsequent processing.

# 训练数据,也就是历史数据
dataf = data.values[0:-48]
#构造数据集
def create_dataset(datasetx,datasety,timesteps=36,predict_size=6):
    datax=[]#构造x
    datay=[]#构造y
    for each in range(len(datasetx)-timesteps - predict_steps):
        x = datasetx[each:each+timesteps,0:6]
        y = datasety[each+timesteps:each+timesteps+predict_steps,0]
        datax.append(x)
        datay.append(y)
    return datax, datay#np.array(datax),np.array(datay)

Then set the time step of prediction, the step size of each prediction, and the final total prediction step size. The parameters can be changed as needed. Different from the previous article, there is no rolling prediction here because there is no continuous feature input. Rolling prediction can be made when features are input in actual applications.

#构造train and predict
train = dataf.copy()
timesteps = 48 #构造x,为48个数据,表示每次用前48个数据作为一段
predict_steps = 48 #构造y,为48个数据,表示用后12个数据作为一段
length = 48 #预测多步,预测48个数据,每次预测48个

Then the data is normalized. Different from the previous article, the features and labels are divided separately and normalized separately.

# 特征和标签分开划分
datafx = dataf[:,0:5]
datafy = dataf[:,5].reshape(25872,1)

# 分开进行归一化处理
scaler1 = MinMaxScaler(feature_range=(0,1))
scaler2 = MinMaxScaler(feature_range=(0,1))
datafx = scaler1.fit_transform(datafx)
datafy = scaler2.fit_transform(datafy)

Finally, the row data set is divided and the data is transformed into data that meets the model format requirements.

trainx, trainy = create_dataset(datafx,datafy,timesteps, predict_steps)
trainx = np.array(trainx)
trainy = np.array(trainy)

Model introductionYoshogi

2.1 Model training

First, build the general operation of the model, and then use the training data trainx and trainy for training, and perform training for 20 epochs, with each batch containing 200 samples. At this time, input_shape is the shape of each x when dividing the data set.

#lstm training
model = Sequential()
model.add(LSTM(128,input_shape=(timesteps,5),return_sequences= True))
model.add(Dropout(0.5))
model.add(LSTM(128,return_sequences=True))
#model.add(Dropout(0.3))
model.add(LSTM(64,return_sequences=False))
#model.add(Dropout(0.2))
model.add(Dense(predict_steps))
model.compile(loss="mean_squared_error",optimizer="adam")
model.fit(trainx,trainy, epochs= 20, batch_size=200)

2.2 Model multi-step prediction

The following introduces the most important method in the article, which is also the method to predict future labels without real future features. The overall idea is to train the next 48 future data through the first 48 data, and when predicting, take the first 48 data to predict the next 48 future data. This is different from single-variable prediction. There is no rolling prediction because the results of single-variable prediction can be rolled as historical data. Multi-variable here only generates predicted values ​​and has no prediction labels. Rolling prediction cannot be performed. In fact, there is a continuous flow of data. Rolling forecast can be used. (The data inside can be changed according to needs)

First, extract the data that needs to be brought into the model, that is, the timesteps row features before prediction.

predict_xlist = []
predict_xlist.extend(dataf[dataf.shape[0]-timesteps:dataf.shape[0],0:5].tolist())
predictx = np.array(predict_xlist[-timesteps:])
predictx = np.reshape(predictx,(1,timesteps,5))#变换格式,适应LSTM模型

After preparing the data, make predictions and denormalize the prediction results.

# 预测
lstm_predict = model.predict(predictx)
# 反归一化
lstm_predict = scaler2.inverse_transform(lstm_predict)
# 提取预测值,方便对比
predict_y = []
predict_y.extend(lstm_predict[0])

2.3 Results visualization

Calculate the error, save the prediction results, and visualize them.

#error
y_ture = np.array(data.values[-48:,5])
train_score = np.sqrt(mean_squared_error(y_ture,predict_y))
print("train score RMSE: %.2f"% train_score)
y_predict = pd.DataFrame(predict_y,columns=["predict"])
y_predict.to_csv("y_predict_LSTM.csv",index=False)
# 可视化
from itertools import cycle
cycol = cycle('bgrcmk')

plt.figure(dpi=100,figsize=(14,5))
plt.plot(y_ture,c=next(cycol),markevery=5)
plt.plot(y_predict,c=next(cycol),markevery=5)
plt.legend(['y_ture','y_predict']) 
# 坐标描述
plt.xlabel('时间')
plt.ylabel('功率(kW)')
plt.show() 

Finally, we visualized the running results and found that the prediction effect roughly captured the trend, and there was a certain degree of fluctuation in the prediction value.

 

Guess you like

Origin blog.csdn.net/qq_41921826/article/details/134631612