首页 > 解决方案 > 如何在for循环中连接列

问题描述

我从每个循环中创建了一系列预测。最后,我想将它们垂直连接成一个数据框。我就是这样做的:

pred_list=[]

for i in range (80, 0, -1):
    train_X, train_y = train[:, :-n_features], train[:, -i]
    test_X, test_y = test[:, :-n_features], test[:, -i]

    model = Sequential()
    model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mse', optimizer='adam')
   
    # fit network
    history = model.fit(train_X, train_y, epochs=5, batch_size=50)

    # make a prediction
    yhat = model.predict(test_X)
    prediction = pd.DataFrame(yhat, columns=['predicition{}'.format(i)])
    pred_list.append(prediction)

final_prediction=pd.concat(pred_list)

但是,final_prediction 不会将 prediction1、prediction2、prediction3 连接到列中。它创造了像水流一样的图案。我的输出如下(假设我在每个预测中只有 3 个样本):

预测1 预测2 预测3
0.3
0.01
0.2
0.004
0.3
0.1
0.12
0.2
0.01

我想要的是:

预测1 预测2 预测3
0.3 0.004 0.12
0.01 0.3 0.2
0.2 0.1 0.01

我的代码哪里做错了?

标签: pythonpandasdataframe

解决方案


a = 0 # for rows or 1 for columns
final_prediction=pd.concat(pred_list, axis = a)

推荐阅读