python - 如何在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 |
我的代码哪里做错了?
解决方案
a = 0 # for rows or 1 for columns
final_prediction=pd.concat(pred_list, axis = a)
推荐阅读
- python - 将过滤后的数据复制到新工作簿(同时创建新工作簿)并将名称保存为 Pivot 中的过滤器值
- reactjs - Mapbox 令牌在本地环境中有效,但在 QA 环境中无效
- javascript - 检查是否所有变量在 JavaScript 中都有值
- asp.net-core-3.1 - Microsoft.AspNetCore.Authentication.WsFederation 中的 RedirectToIdentityProvider
- python - 修改函数内的numpy数组
- go - 如何在 golang 中提供静态文件
- css - 溢出换行:断字未按预期工作
- reactjs - 通过观察 ID 使 Apollo 客户端自动更新缓存
- javascript - 如果用户拒绝摄像头访问,如何将用户重定向到单独的页面?
- google-cloud-platform - 使用 App Engine Cron 服务的 Cron 作业:每天在给定时间