将随机林预测作为列添加到测试文件中

2024-03-28 11:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在python pandas(在Jupyter笔记本中)中工作,在那里我为泰坦尼克号数据集创建了一个随机林模型。 https://www.kaggle.com/c/titanic/data

我读入测试和训练数据,然后清理它并添加新列(两个列都是相同的列)

在对模型进行了拟合和重新拟合,并尝试了升压等;我决定采用一种模式:

 X2 = train_data[['Pclass','Sex','Age','richness']] 
 rfc_model_3 = RandomForestClassifier(n_estimators=200)
 %time cross_val_score(rfc_model_3, X2, Y_target).mean()
 rfc_model_3.fit(X2, Y_target)

然后我预测,如果有人幸存或没有

 X_test = test_data[['Pclass','Sex','Age','richness']]
 predictions = rfc_model_3.predict(X_test)
 preds = pd.DataFrame(predictions, columns=['Survived'])

有没有办法将预测作为column添加到测试文件中


Tags: 数据testtargetpandasagedatamodelrfc
1条回答
网友
1楼 · 发布于 2024-03-28 11:06:37

rfc_model_3 = RandomForestClassifier(n_estimators=200)
rfc_model_3.predict(X_test)

返回y : array of shape = [n_samples]see docs),您应该能够直接将模型输出添加到X_test,而无需创建中间DataFrame

X_test['survived'] = rfc_model_3.predict(X_test)

如果你想得到中间结果,@EdChum在评论中的建议也行

相关问题 更多 >