R:Tidymodels:是否可以在 tidy 模型中为随机森林模型绘制树木?
R: Tidymodels: Is it possible to plot the trees for a random forest model in tidy models?
是否可以在随机森林模型中绘制树木?以下是可用于解释的示例数据集。
抱歉,我没有在网上找到任何此类示例,因此我没有尝试任何操作 own.The 以下只是一个示例解决方法。
library(palmerpenguins)
penguins <- penguins %>%
filter(!is.na(sex)) %>%
select(-year, -island)
拆分数据
set.seed(123)
penguin_split <- initial_split(penguins, strata = sex)
penguin_train <- training(penguin_split)
penguin_test <- testing(penguin_split)
正在创建模型规格。
rf_spec <- rand_forest() %>%
set_mode("classification") %>%
set_engine("ranger")
penguin_wf <- workflow() %>%
add_formula(sex ~ .)
应用于测试数据
penguin_final <- penguin_wf %>%
add_model(rf_spec) %>%
last_fit(penguin_split)
现在如何绘制树?
提前致谢。
据我所知,没有用于绘制 ranger
树或 randomForest
树的内置函数(有关这些方法的简要介绍,请参阅 here and here). However, the forest of decision trees is made up of 500 trees by default, it seems exaggerated to have a plot for each of them. There are some methods to plot decision trees from other algorithm such as rpart
, party
or tree
. Have a look here绘制树木和森林。
是否可以在随机森林模型中绘制树木?以下是可用于解释的示例数据集。 抱歉,我没有在网上找到任何此类示例,因此我没有尝试任何操作 own.The 以下只是一个示例解决方法。
library(palmerpenguins)
penguins <- penguins %>%
filter(!is.na(sex)) %>%
select(-year, -island)
拆分数据
set.seed(123)
penguin_split <- initial_split(penguins, strata = sex)
penguin_train <- training(penguin_split)
penguin_test <- testing(penguin_split)
正在创建模型规格。
rf_spec <- rand_forest() %>%
set_mode("classification") %>%
set_engine("ranger")
penguin_wf <- workflow() %>%
add_formula(sex ~ .)
应用于测试数据
penguin_final <- penguin_wf %>%
add_model(rf_spec) %>%
last_fit(penguin_split)
现在如何绘制树?
提前致谢。
据我所知,没有用于绘制 ranger
树或 randomForest
树的内置函数(有关这些方法的简要介绍,请参阅 here and here). However, the forest of decision trees is made up of 500 trees by default, it seems exaggerated to have a plot for each of them. There are some methods to plot decision trees from other algorithm such as rpart
, party
or tree
. Have a look here绘制树木和森林。