基于梯度提升的基线
让我们基于随机森林构建最终的基线。您在视频中已经看到,从"分组基线"提升到"梯度提升"后,分数有了大幅提升。现在,您将使用 sklearn 的随机森林来进一步提升分数。
本练习的目标是选取数值特征,在不做任何调参的情况下训练一个随机森林模型。随后,您可以对测试集进行预测,并在 Public Leaderboard 上验证结果。请注意,您已经有一个 "hour" 特征,也可以作为模型的输入。
本练习是课程的一部分
用 Python 赢下 Kaggle 竞赛
练习说明
- 将
"hour"特征添加到数值特征列表中。 - 在包含数值特征的训练数据上拟合
RandomForestRegressor,并以"fare_amount"作为目标变量。 - 使用训练好的随机森林模型对测试数据进行预测。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from sklearn.ensemble import RandomForestRegressor
# Select only numeric features
features = ['pickup_longitude', 'pickup_latitude', 'dropoff_longitude',
'dropoff_latitude', 'passenger_count', ____]
# Train a Random Forest model
rf = RandomForestRegressor()
rf.____(train[____], train.fare_amount)
# Make predictions on the test data
test['fare_amount'] = ____.____(test[features])
# Write predictions
test[['id','fare_amount']].to_csv('rf_sub.csv', index=False)