基于人工神经网络的时间序列预测

2024-05-20 15:02:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用ANN使用不同的输入和参数预测未来10分钟的功耗。数据库每10分钟记录26000多个样本。下图是数据库的一部分

    Date_time              Consumption
0   2016-10-01 00:00:00    2781
1   2016-10-01 00:10:00    3729
2   2016-10-01 00:20:00    3357
3   2016-10-01 00:30:00    2609
4   2016-10-01 00:40:00    3025

enter image description here

为了预测“t”,我将其用作输入:

  1. 最后6个周期(t-1至t-6)
  2. 同时最后4天(t-144、t-288、t-432和t-576)
  3. 最后3个周期,同时4天
  4. 最后3个周期,4天,4周(t-1008,t-2016,t-3024,t-4032)

我使用pandas shift函数创建带有滞后特征的新列,然后删除NaN值。我将20%的数据设置为测试集,然后应用MinMaxScaler转换特性

对于每个输入配置,我都进行了网格搜索,以找到最佳参数。下面的代码显示了用于安装模型的功能

def model_fit(train_x, train_y, model_config):
    
    # reset keras
    
    clear_session()
    
    # model_config contains the parameters of the model
    
    n_layers, n_nodes, activation, n_batch = model_config
    
    model = Sequential()
    
    # the shape of the input layer is given by the number of features
    
    model.add(InputLayer(input_shape=(train_x.shape[1], )))
    
    # model.add(Dropout(0.2))
    
    for layers in range(n_layers):
        
        model.add(Dense(n_nodes, 
                        activation=activation
                       )
                 )
        
    model.add(Dense(1))

    model.compile(loss='mse', optimizer=Adam(0.01), metrics=['mse', 'mae'])
    
    model.summary()

    earlystop = EarlyStopping(monitor='val_mse')
    
    # 15% of the training set will be used to validate the model

    model.fit(train_x, 
              train_y, 
              validation_split=0.15, 
              callbacks = [earlystop], 
              epochs=1500, 
              batch_size=n_batch, 
              shuffle=False, 
              verbose=1
             )
    
    return model

在尝试了几个参数组合后,我发现配置3的效果最好

MODEL CONFIGURATION: (n_layers=1, n_nodes=48, activation='relu', n_batch=50)

Training RMSE: 429.3599853515625
Validation RMSE: 223.30999755859375 

Training MAE: 271.6600036621094
Validation MAE: 159.44000244140625 

Test RMSE: 227.69
Test MAE: 167.88

enter image description here

上述结果表明,人工神经网络只是利用“t-1”来预测“t”。然而,通过查看第一个图中的模式,在我看来,该模型可以做得更好。我发现了一些类似的问题,我已经尝试了一些建议。使用辍学时,移位/延迟预测问题似乎消失了(下图)。我还尝试使用时间(用余弦和正弦转换为周期)作为输入。然而,结果更糟,现在我真的不知道还能做什么

enter image description here

===================================================================================================

在使用整个数据库之前,我尝试用一个较小的数据库开发一个模型,该数据库每小时都有测量数据,而且运行得非常好。输入为时间(用余弦和正弦转换)、最后一个周期(t-1)和最后一天的消耗量(t-24)。下图显示了结果

MODEL CONFIGURATION: (n_layers=1, n_nodes=48, activation='tanh', n_batch=50)
MODEL RMSE: 354.76
MODEL MAE: 283.97

enter image description here


Tags: ofthe模型add数据库参数modellayers