R 中的树模型机器学习
Sandro Raabe
Data Scientist
predict(model, new_data, type)
predict(model, new_data = test_data,
type = "class")
.pred_class
<fct>
1 no
2 no
3 yes
4 no
predict(model, new_data = test_data,
type = "prob")
.pred_no .pred_yes
<dbl> <dbl>
1 0.866 0.134
2 0.956 0.044
3 0.672 0.328
4 0.877 0.123





# Combine predictions and truth values
pred_combined <- predictions %>%
mutate(true_class = test_data$outcome)
pred_combined
.pred_class true_class
<fct> <fct>
1 no no
2 no yes
3 no no
4 yes yes
# Calculate the confusion matrix conf_mat(data = pred_combined,estimate = .pred_class,truth = true_class)
Truth
Prediction no yes
no 116 31
yes 12 40
$$\text{accuracy} = \frac{\text{正确预测数}}{\text{总预测数}}$$
accuracy()conf_mat() 相同参数data、estimate、truthyardstick 中的通用结构accuracy(pred_combined,
estimate = .pred_class,
truth = true_class)
.metric .estimate
<chr> <dbl>
1 accuracy 0.708
R 中的树模型机器学习