创建完整的随机森林模型
随机森林模型在构建许多基于特征子集的子树时,会自然进行特征选择。理解特征重要性的一种方法是先训练模型,然后提取特征重要性。因此,在本练习中,您将使用 Healthcare Job Attrition 数据来训练一个 rand_forest() 分类模型,并从中提取特征重要性。为使特征重要性可用,请确保在创建模型时设置 importance = "impurity"。train 和 test 数据集已为您准备好。
已为您加载 tidyverse、tidymodels 和 vip 包。
本练习是课程的一部分
R 中的降维
练习说明
- 定义一个包含 200 棵树的随机森林分类模型,以便提取特征重要性。
- 使用所有自变量来拟合随机森林模型。
- 将预测结果绑定回测试集。
- 计算 F1 指标。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Specify the random forest model
rf_spec <- ___(mode = "___", ___ = ___) %>%
set_engine("___", ___ = "___")
# Fit the random forest model with all predictors
rf_fit <- ___ %>%
___(___, data = ___)
# Create the test set prediction data frame
predict_df <- ___ %>%
bind_cols(predict = ___(___, ___))
# Calculate F1 performance
f_meas(predict_df, ___, .pred_class)