从熊猫数据框中获取序列

时间:2020-05-22 10:26:29

标签: python pandas keras sequence lstm

我有一个带有6873行和720列的pandas数据框。每行是一个单独的观察,每列是2分钟的时间步长(720 col * 2 min = 1440 min = 24h)。因此,每一行都是一个人在一天中进行的活动的1和0的序列(0表示该人正在做某事,1表示该人正在旅行)。

好。现在,我想训练一个LSTM网络,以便根据前20个时间步长的信息来预测3个时间步长(3列)。我已经对此进行了编码以准备数据(在这里,我仅使用1行df.iloc[1]来完成此操作):

   # split a univariate sequence into samples
def split_sequence(sequence, n_steps):
    X, y = list(), list()
    for i in range(len(sequence)):
        # find the end of this pattern
        end_ix = i + n_steps

        # check if we are beyond the sequence
        if end_ix > len(sequence)-1:
            break
        # gather input and output parts of the pattern
        seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:end_ix+3]
        X.append(seq_x)
        y.append(seq_y)
    return array(X), array(y)

# choose a number of time steps
n_steps = 20

# define input and output sequence
raw_seq = df.iloc[1]
X, y = split_sequence(raw_seq, n_steps)

# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))

当我尝试在下面运行此网络时,我的问题来了。我发现这是一个根据输入序列预测长度为1的序列的网络。我认为出现问题是因为我要预测的序列的长度为3。也许还因为我应该重塑y,但是我不知道如何,我尝试了不同的方法,但是没有成功。

model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# fit model
model.fit(X, y, epochs=30, verbose=1)
# demonstrate prediction

我收到此错误ValueError: setting an array element with a sequence.,我该怎么更改?

谢谢!

0 个答案:

没有答案