【问题标题】:How to shape large DataFrame for python's keras LSTM?如何为 python 的 keras LSTM 塑造大型 DataFrame?
【发布时间】:2019-06-18 15:36:30
【问题描述】:

我几乎在accepted answer here 中找到了我需要的东西。但是有内存问题,因为提供的测试 df 只有 11 行。

我要预测的是使用 LSTM 在 回归 模型(不是分类器!)。我的 dataframe X 有大约 1500 行和 2000 个特征,属于 shape (1500, 2000)真值 y 只是 1500 行,共 1 个功能(可以range any value between -1 and 1)。

由于 LSTM 需要 3D 向量作为输入,我真的苦苦思索如何重塑数据

再次,按照第一段的示例,它在填充值时因 MemoryError 而崩溃,更具体地说,在 df.cumulative_input_vectors.tolist()

我的测试(读取预测)是shape (10, 2000)数据框。

由于敏感数据,我实际上无法分享值/示例。我该如何帮助您解决这个问题?

所以,为了让 LSTM 能够从 y 的 1500 行中学习如何我应该重塑我的 x 的 1500 行和 2000 行功能另外如何我应该重塑我的forecast 10 行和 2000 个特征

他们会经历 - 起初因为我正在学习 LSTM - 一个简单的 LSTM 模型:

model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(train_X, train_y , epochs=50, batch_size=2, verbose=1)

我试过了,但是当预测出错时:

# A function to make a 3d data of what I understood needed done:
def preprocess_data(stock, seq_len):
    amount_of_features = len(stock.columns)
    data = stock.values

    sequence_length = seq_len #+ 1
    result = []
    for index in range(len(data) - sequence_length):
        result.append(data[index : index + sequence_length])

    X_train = np.array(result)  

    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], amount_of_features))

    return X_train

# creating the train as:
# X == the DF of 1500 rows and 2000 features
window = 10
train_X = preprocess_data(X[::-1], window)

【问题讨论】:

  • 请重新整理您的问题,并明确您的要求。现在我很困惑,因为您似乎在问一个重塑问题,然后不知何故您设法运行预测但在那里却出现错误?
  • 我看不出有什么困惑,问题的句子/名称是什么?提示:我应该如何重塑。正文只是对我在哪里的完整解释,以避免“可能重复......”的无益答案

标签: python keras lstm reshape


【解决方案1】:

一段时间后,我设法正确理解了尺寸在哪里。 Keras 期望一个 3d 数组 .shape (totalRows, sequences, totalColumns)sequences 最让我困惑。

那是因为当重塑 df df.reshape(len(df), 1, len(df.columns)) 意味着 keras 将学习 1 行矩阵时,它给了我不好的结果,因为我不知道 最好缩放数据 对我来说MinMaxScaler(-1,1) 效果最好,但可能是(0,1)

让我明白首先使用超过 1 行(或几天,因为我的数据集是时间序列)的序列。这意味着不是输入 1 行特征 X 会导致 1 个 y 值我使用了类似 5 行特征 X 会导致 1 个 y 值。如:

# after scaling the df, resulted in "scaled_dataset"
sequences = 5
result = []
# for loop will walk for each of the 1500 rows
for i in range(0,len(scaled_dataset)):
    # every group must have the same length, so if current loop position i + number 
    # of sequences is higher than df length, breaks
    if i+sequences <= len(scaled_dataset):
        # this will add into the list as [[R1a,R1b...R1t],[R2a,R2b...R2t],...[R5a,R5b...R5t]]
        result.append(scaled_dataset[i:i+sequences].values)
# Converting to array + keras takes float32 better than 64
train_x = np.array(result).astype('float32')
# making the y into same length as X
train_y = np.array(y.tail(train_x.shape[0]).values)

train_x.shape, train_y.shape

'>>> (1495, 5, 2400), (1495,)

用另一种方式写下 keras 的心态为我的问题塑造了:

考虑到它是一个时间序列,上面意味着 5 天(第 0 到 4 行)的数据导致第 5 行的值 y。

然后,减去第一天 + 后天 - 仍然是 5 天 - (第 1 到 5 行)的数据导致第 6 行的值 y。

然后,减去第二天 + 最后一天后的第二天 - 仍然是 5 天 - (第 2 到 6 行)的数据导致第 7 行的值 y。

对于 keras/LSTM 的初学者来说,这很令人困惑,但我希望我可以为可能登陆这里的人详细说明这一点。

【讨论】:

  • 我也在用这个方法。但是,循环遍历大型数据帧非常慢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 2022-01-14
  • 2018-02-20
  • 2018-06-05
  • 1970-01-01
  • 2017-07-30
相关资源
最近更新 更多