开始使用免费开始使用

评估性能

最后,和往常一样,您需要评估最佳模型的性能,看看效果如何。理想情况下,最好做回测,但这是一个更复杂的流程,本课程无法展开。

我们已经看过 R\(^2\) 分数了,现在使用 matplotlib 查看预测值与真实结果的散点图。若预测完美,点应沿着从左下到右上的对角线分布。

本练习是课程的一部分

Python 金融机器学习

查看课程

练习说明

  • RandomForestRegressorrfr)中使用我们在上一个练习中找到的最佳 max_features 数值(为 4)。
  • 使用该模型分别对 train_featurestest_features 进行预测。
  • 绘制真实目标(train/test_targets)与预测值(train/test_predictions)的散点图,并将数据集标注为 traintest

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Use the best hyperparameters from before to fit a random forest model
rfr = RandomForestRegressor(n_estimators=200, max_depth=3, max_features=____, random_state=42)
rfr.fit(train_features, train_targets)

# Make predictions with our model
train_predictions = rfr.predict(____)
test_predictions = ____

# Create a scatter plot with train and test actual vs predictions
plt.scatter(train_targets, train_predictions, label='train')
plt.scatter(____)
plt.legend()
plt.show()
编辑并运行代码