【问题标题】:Time-series prediction with keras使用 keras 进行时间序列预测
【发布时间】:2017-11-27 14:28:16
【问题描述】:

我正在尝试进行概念验证,以预测停车场的满员情况。我正在尝试使用 Keras 创建一个 LSTM 神经网络,以预测一个区域在给定时间的填充程度。

这是我的数据框的头部:

Time_Stamp      Weekday Area    Sub_Area    Free_Spots  Used_Spots Full%                        
2014-04-10 08:00:00 Yes Ballard Locks   NW 54TH SR ST BETWEEN 32ND AVE NW AND NW 54TH ST    68.0    1.0  1.0
2014-04-10 09:00:00 Yes Ballard Locks   NW 54TH SR ST BETWEEN 32ND AVE NW AND NW 54TH ST    68.0    2.0  3.0
2014-04-10 10:00:00 Yes Ballard Locks   NW 54TH SR ST BETWEEN 32ND AVE NW AND NW 54TH ST    12.0    0.0  0.0
2014-04-10 11:00:00 Yes Ballard Locks   NW 54TH SR ST BETWEEN 32ND AVE NW AND NW 54TH ST    12.0    0.0  0.0
2014-04-10 12:00:00 Yes Ballard Locks   NW 54TH SR ST BETWEEN 32ND AVE NW AND NW 54TH ST    12.0    0.0  0.0

我运行以下代码:

from sklearn.model_selection import train_test_split
TRAIN,TEST,notused,notused = train_test_split(df['data']['Full%'],
                                                    df['data']['Full%'], 
                                                    test_size=0.25)
TRAIN.sort_index(inplace=True)
TEST.sort_index(inplace=True)

.

# create train lists
x_train = []
y_train = []

# create test lists
x_test = []
y_test = []

# fill the train lists
for i in range(len(TRAIN)-1):
    x_train.append(TRAIN[i])
    y_train.append(TRAIN[i+1])

# fill the test lists
for i in range(len(TEST)-1):
    x_test.append(TEST[i])
    y_test.append(TEST[i+1])

# change the lists to numpy arrays
x_train, y_train = np.array(x_train), np.array(y_train)
x_test, y_test = np.array(x_test), np.array(y_test)

下一部分是我无法让它工作的地方。

x_train = x_train.reshape(1,56,1)
y_train = x_train.reshape(1,56,1)

model = Sequential()
model.add(LSTM(56, input_dim=56,return_sequences=True))
model.add(Dense(56))
model.compile(loss='mean_absolute_error', optimizer='adam',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10000, batch_size=1, verbose=2,validation_data=(x_test, y_test))

我一直在玩,但错误一直是某种价值错误:

ValueError: Error when checking input: expected lstm_24_input to have shape (None, None, 56) but got array with shape (1, 56, 1)

现在我有几个问题,除了我的代码有什么问题:

我的训练数据和测试数据的大小不同似乎是一个问题,因为输入的维度不会相同。我该如何处理?

日期时间戳不是我的训练/测试数据的一部分,而且因为这个数据集是一个实际的数据集(数据取自这个数据集:https://github.com/bok11/IS-Data-Analasys/blob/master/Data/Annual_Parking_Study_Data.csv),所以每次观察之间的时间会有所不同。这样可以吗?

可以在这里看到我的笔记本的完整视图:https://github.com/bok11/IS-Data-Analasys/blob/master/Data%20Exploration%20(Part%202).ipynb

编辑:我的任务目标是证明收集这些数据来预测停车区是否可行。

【问题讨论】:

  • 您确定这是正确的代码吗?您的第一层是 LSTM,而不是消息所说的 Dense 层。
  • 你是对的,我复制了错误的错误消息(我一直在玩并重新运行单元格)我已经用正确的错误更新了我的问题

标签: python time-series keras lstm


【解决方案1】:

消息说您的输入数据(numpy 数组)的形状为 (1,56,1),而您的模型的形状为 (any, any, 56)

在循环网络中,输入形状应类似于(batch size, time steps, input features)

因此,您需要确定是同一特征有 56 个时间步,还是只有 56 个不同特征的一个时间步。然后选择两个形状之一进行调整。

你有序列似乎是合乎逻辑的(如果你使用的是 LSTM),所以我假设你有 56 个时间步长。

那么,你在 LSTM 层中的输入形状应该是:

LSTM(doesntMatter, input_shape=(56,1), return_sequences=True)

或者(如果您想要可变数量的步骤):

LSTM(doesntMatter, input_shape=(None,1), return_sequences=True)

假设您需要多个信息,例如日期和工作日。那么你有两个功能。您的形状将是 input_shape(None,2)

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多