检查结果
当我们得到一个已优化的模型后,需要更细致地检查其表现。我们已经看过 R\(^2\) 分数,但将预测值与真实值作图对比也很有帮助。我们可以使用决策树模型的 .predict() 方法在训练集和测试集上生成预测。
理想情况下,我们希望看到从左下到右上的对角线。然而,由于决策树的简单性,我们的模型在测试集上的表现不会很好。但它会在训练集上表现出色。
本练习是课程的一部分
Python 金融机器学习
练习说明
- 创建一个名为
decision_tree的DecisionTreeRegressor模型,并将max_depth超参数设为 3。 - 使用我们的决策树模型对训练集和测试集(
train_features和test_features)进行预测。 - 使用
plt.scatter()将训练集和测试集的预测值与真实目标值作散点图;对于测试集,将label参数设为test。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Use the best max_depth of 3 from last exercise to fit a decision tree
decision_tree = ____
decision_tree.fit(train_features, train_targets)
# Predict values for train and test
train_predictions = decision_tree.predict(train_features)
test_predictions = ____
# Scatter the predictions vs actual values
plt.scatter(train_predictions, train_targets, label='train')
plt.scatter(____)
plt.show()