随机森林:预测
现在,您需要使用随机森林模型进行预测。其语法与梯度提升树模型相同。
本练习是课程的一部分
R 中使用 sparklyr 的 Spark 入门
练习说明
已为您创建名为 spark_conn 的 Spark 连接。已将附加到存储在 Spark 中的训练集和测试集的 tibbles 预定义为 track_data_to_model_tbl 和 track_data_to_predict_tbl。随机森林模型已预定义为 random_forest_model。
- 定义变量
predicted,其中包含模型对测试数据的预测。- 调用
ml_predict(),将模型和测试数据作为参数。该函数会为测试数据集生成预测,并将其作为名为prediction的新列添加进去。
- 调用
- 定义变量
responses,以便将预测的响应与真实响应进行比较:- 选择响应列
year。 - 收集结果。
- 使用
mutate()将predicted中的预测添加进来。
- 选择响应列
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Training, testing sets & model are pre-defined
track_data_to_model_tbl
track_data_to_predict_tbl
random_forest_model
# Predict the responses for the testing data
predicted <- ml_predict(
___,
___) %>% pull(prediction)
# Create a response vs. actual dataset
responses <- ___