我在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'])
有没有办法将预测结果作为列
添加到测试文件中?
回答:
因为
rfc_model_3 = RandomForestClassifier(n_estimators=200)rfc_model_3.predict(X_test)
返回y : array of shape = [n_samples]
(见文档),你应该可以直接将模型输出添加到X_test
中,而无需创建中间DataFrame
:
X_test['survived'] = rfc_model_3.predict(X_test)
如果你仍然想要中间结果,评论中的@EdChum的建议将很好地工作。