如何为具有外部特征的时间序列多步范围构建 LSTM 的输入数据? [英] How to construct input data to LSTM for time series multi-step horizon with external features?

查看:24
本文介绍了如何为具有外部特征的时间序列多步范围构建 LSTM 的输入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 LSTM 进行商店销售预测.这是我的原始数据的样子:

I'm trying to use LSTM to do store sales forecast. Here is how my raw data look like:

|     Date   | StoreID | Sales | Temperature |  Open   | StoreType |
|------------|---------|-------|-------------|---------|-----------|
| 01/01/2016 |   1     |   0   |      36     |    0    |     1     |
| 01/02/2016 |   1     | 10100 |      42     |    1    |     1     |
| ...
| 12/31/2016 |   1     | 14300 |      39     |    1    |     1     |
| 01/01/2016 |   2     | 25000 |      46     |    1    |     3     |
| 01/02/2016 |   2     | 23700 |      43     |    1    |     3     |
| ...
| 12/31/2016 |   2     | 20600 |      37     |    1    |     3     |
| ...
| 12/31/2016 |   10    | 19800 |      52     |    1    |     2     |

我需要预测未来 10 天的销售额.在这个例子中,我需要预测从 01-01-2017 到 01-10-2017 的商店销售额.我知道如何使用其他时间序列模型或回归模型来解决这个问题,但我想知道 RNN-LSTM 是否适合它.

I need to forecast for the next 10 days' sales. In this example, I will need to forecast the store sales from 01-01-2017 to 01-10-2017. I know how to use other time series model or regression model to solve this problem, but I want to know if RNN-LSTM is a good candidate for it.

我首先使用仅 storeID=1 数据来测试 LSTM.如果我的数据只有日期和销售额.我将以这种方式构建我的 trainX 和 trainY(如果我错了,请纠正我):

I started by taking only storeID=1 data to test the LSTM. If my data only have Date and Sales. I will construct my trainX and trainY in this way (please correct me if I'm wrong):

Window = 20
Horizon = 10

|         trainX                  |          trainY              |
| [Yt-10, Yt-11, Yt-12,...,Yt-29] | [Yt, Yt-1, Yt-2,...,Yt-9]    |
| [Yt-11, Yt-12, Yt-13,...,Yt-30] | [Yt-2, Yt-3, Yt-4,...,Yt-10] |
| [Yt-12, Yt-13, Yt-14,...,Yt-31] | [Yt-3, Yt-4, Yt-5,...,Yt-11] |
...

两者重塑后

trainX.shape
(300, 1, 20)
trainY.shape
(300, 10)

问题1:在这种情况下,[样本、时间步长、特征] = [300, 1, 20].这是正确的吗?还是应该将样本构造为 [300, 20, 1] ?

Question1: In this case, [samples, time steps, features] = [300, 1, 20]. Is this right? Or should I construct the sample as [300, 20, 1] ?

问题 2: 我确实想在原始数据中使用其他信息,例如温度、存储类型等.我应该如何为 LSTM 构建输入数据?

Question2: I do want to use other information in the raw data like Temperature, StoreType, etc. How should I construct my input data for LSTM?

问题3:到目前为止,我们只讨论了1个店铺预测,如果我想对所有店铺进行预测,那么我应该如何构建我的输入数据呢?

Question3: So far we only discussed 1 store forecast, if I want to forecast for all the stores, how should I construct my input data then?

目前我正在从 here,但似乎不足以涵盖我所拥有的场景.非常感谢您的帮助!

Currently I'm flowing examples from here, but it seems not sufficient to cover the scenario that I have. I really appreciate for your help!

推荐答案

我最近在解决类似的问题.在你的情况下:

I was recently solving similiar problem. In your case:

  1. 输入的形状应为 (300, 20, 1) - 因为您有长度为 20 且具有 1 功能的时间序列.

  1. Input should have shape (300, 20, 1) - because you have time sequences of length 20 with 1 feature.

你可以这样做:

sequential_input = Input(shape=(20, 1))
feature_input = Input(shape=(feature_nb,))
lstm_layer = LSTM(lstm_units_1st_layer, return_sequences=True)(sequential_input)
lstm_layer = LSTM(lstm_units_2nd_layer, return_sequences=True)(lstm_layer)
...
lstm_layer = LSTM(lstm_units_nth_layer, return_sequences=False)(lstm_layer)
merged = merge([lstm_layer, feature_input], mode='concat')
blend = Dense(blending_units_1st_layer, activation='relu')(merged)
blend = Dense(blending_units_2nd_layer, activation='relu')(blend)
...
output = Dense(10)(blend)

  • 这是最难的部分.我建议您通过将多个商店作为一个特征向量输入网络来预测它们.您可能会简单地跳过这一部分并尝试使用一个模型或使用例如后处理输出来预测不同的商店.矩阵上的某种图形模型或 PCA,其中行是日销售额.

  • This is the hardest part. I do not advise you to predict multiple shops by feeding them to a network as one feature vector. You may under simply skip this part and try to predict different shops using one model or postprocess output using e.g. some kind of graphical models or PCA on matrix where rows are day sales.

    更新:

    为了处理多个顺序特征,您可以执行以下操作:

    In order to deal with multiple sequential features you could do the following thing:

        sequential_input = Input(shape=(20, nb_of_sequental_features))
        feature_input = Input(shape=(feature_nb,))
        lstm_layer = LSTM(lstm_units_1st_layer, return_sequences=True)(sequential_input)
        lstm_layer = LSTM(lstm_units_2nd_layer, return_sequences=True)(lstm_layer)
        ...
        lstm_layer = LSTM(lstm_units_nth_layer, return_sequences=False)(lstm_layer)
        merged = merge([lstm_layer, feature_input], mode='concat')
        blend = Dense(blending_units_1st_layer, activation='relu')(merged)
        blend = Dense(blending_units_2nd_layer, activation='relu')(blend)
        ...
        output = Dense(10)(blend)
        model = Model(input=[sequential_input, feature_input], output=output])
    

    在这种情况下,您的输入应包含两个表的列表:[sequential_data, features] 其中 sequential_data.shape = (nb_of_examples, timesteps,sequential_features)features.shape = (nb_of_examples, feature_nb).所以 salestemperature 应该存储在 sequential_featuresstore_typefeatures.

    In this case your input should consist of list of two tables: [sequential_data, features] where sequential_data.shape = (nb_of_examples, timesteps, sequential_features) and features.shape = (nb_of_examples, feature_nb). So sales or temperature should be stored in sequential_features and store_type in features.

    这篇关于如何为具有外部特征的时间序列多步范围构建 LSTM 的输入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆