如何为 LSTM 重塑数据 - 时间序列 multi class classification

How to reshape data for LSTM - Time series multi class classification

我正在使用 ASHRAE RP-1043 冷水机多传感器数据集处理时间序列 classification,每个冷水机故障和正常状态有 65 列和 3000 多行。而且我使用了 LSTM,我不确定我在这里使用的数据结构是否适合时间序列 classification。下面是我的数据框的图像,该数据框是根据收集的数据集创建的,其中包含多个冷水机条件的记录(7 个故障和正常)。每条记录都标有相关的 class(条件)。并从不同文件构建数据集,用于故障条件和正常条件。

火车数据形状如下 X_train.shape,y_train.shape

((81600, 65), (81600, 8))

但是对于 LSTM 输入需要是 3D 的。所以重塑如下。 (只有 1 个时间步长) # 使其成为 3d 输入 X_train = X_train.reshape(-1,1,65) X_train.shape,y_train.shape`

((81600, 1, 65), (81600, 8))

def create_nn_model():
  model = Sequential()
  model.add(LSTM(100, dropout=0.2, input_shape=(X_train.shape[1],
  X_train.shape[2]),return_sequences=True))
  model.add(Dense(100, activation='relu'))
  model.add(Dense(8,activation='softmax'))
  model.compile(loss='categorical_crossentropy',
                optimizer='adam', metrics=['accuracy'])
  return model

这适用于我的模型,我可以毫无错误地适应。

但是我怎样才能增加 X_train 的时间步数,如(100 个时间步)

scaled_x_train.reshape(-1,100,65) X_train.shape,y_train.shape

((816, 100, 65), (81600, 8))

现在 X_train 已经重塑。但由于 X_train 和 y_train 的大小不同,我不能适应这个。我曾尝试以与 X_train 相同的方式重塑 y_train 但随后我将不得不 return 序列,这不是我的要求。我的数据集结构(102000 行和 65 列)有什么问题吗?我可以直接拆分上图中显示的数据进行训练和测试,还是需要做更多操作。感谢任何帮助

P.S 与 Priya 的回答相关

即使在返回序列后,您也可以采用最后一个时间步,就像您有

num_categories=10
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True))
tf.keras.layers.Dense(num_categories)
tf.keras.layers.Dense(1)
ypred = model.predict(x_test)

# Final layer shape after predict would be
# ypred.shape = (Num_samples, 100, 10)
# taking last time step
ypred = ypred[:,-1,:]  # will be of shape (Num_samples, 10)

# Prediction classes
ypred_classes = np.argmax(ypred,axis = -1)

你不能直接重塑成这样:

scaled_x_train.reshape(-1,100,65) X_train.shape,y_train.shape

当 timesteps=1 时不会报错,因为 x_train.shape = (num_samples,time_steps,num_features) 中的 num_samples 不会改变。由于 dim=1 可以在任何轴上创建。

但是当time_steps>1,num_samples=len(dataset)-time_steps.

我包含了一段代码,它为 Lstm 模型创建输入数据,假设最后一列是您的目标变量。我认为您其余的模型代码都很好。

import numpy as np 

# FUNCTION TO CREATE 1D DATA INTO TIME SERIES DATASET
def new_dataset(dataset, time_steps):
    data_X, data_Y = [], []
    for i in range(len(dataset)-time_steps):
        a = dataset[i:(i+time_steps), :-1]
        data_X.append(a)
        data_Y.append(dataset[i + time_steps, -1])
    return np.array(data_X), np.array(data_Y)

我认为您错过了 RNN 或 LSTM 的基本工作原理。这不是数据重复......这是 RNN 的工作方式。

这里我举个简单的例子,假设我们的数据由三个特征和一个目标组成。

Temperature humidity pressure   target(binary, whether it will rain or not)
    
    63        89       29.8       1
    88        21.2     40.5       0
    72        90.3     48.7       1
    45        23.2     67.2       0
    90        10.8     32.6       0

然后假设我们希望我们的模型回顾 3 timesteps 以了解数据中的模式。因此,我们构造输入数据的方式是将 3 个时间步组合在一起。

# 0,1,2 index
[63, 89, 29.8,       Target data:
 88, 21.2, 40.5,     # datapoint at 3rd index in target
 72, 90.3, 48.7]     [0]

# 1,2,3 index
[88, 21.2, 40.5,     # datapoint at 4th index in target
 72, 90.3, 48.7       [0]
 45, 23.2, 67.2]

 # 2,3,4 index       # model should predict datapoint at 5th index in target
[72, 90.3, 48.7
 45, 23.2, 67.2       
 90, 10.8, 37.6]

所以现在输入数据shape=(3,3,3) 请在实施之前阅读 RNN 上的文档,否则会非常混乱。