生成预测
利用数据进行预测是机器学习的基本目标之一。 既然您已经会拆分数据并拟合模型,现在就该用模型对未见过的样本进行预测了。
您将使用将训练数据拟合到树模型规范后得到的模型,对测试集进行预测。
您的工作空间中已提供先前生成的数据集(diabetes_train 和 diabetes_test)以及一个决策树规范 tree_spec,它是通过以下代码生成的:
tree_spec <- decision_tree() %>%
set_engine("rpart") %>%
set_mode("classification")
本练习是课程的一部分
R 中的树模型机器学习
练习说明
- 使用
outcome作为目标变量、包含所有预测变量,将您的规范拟合到训练数据以创建model。 - 使用该模型对测试集中每条观测的糖尿病结局进行预测,并将结果赋给
predictions。 - 将测试集中的真实结局作为名为
true_class的列添加到predictions中,并将结果保存为predictions_combined。 - 使用
head()函数打印结果的前几行。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Train your model
model <- tree_spec %>%
___
# Generate predictions
predictions <- ___(model,
___)
# Add the true outcomes
predictions_combined <- predictions %>%
___(true_class = ___)
# Print the first lines of the result
___