目前使用该tidymodels
框架并努力理解我得到的模型预测和性能结果的一些差异,特别是当我在完全相同的数据集(即模型训练的数据集)上同时使用两者fit
时。predict
下面是一个可重现的示例 - 我正在使用单元数据集并在数据上训练一个随机森林 ( rf_fit
)。该对象rf_fit$fit$predictions
是我评估其准确性的一组预测之一。然后,我rf_fit
通过该函数对相同的数据进行预测predict
(产量rf_training_pred
,我评估其准确性的另一组预测)。
我的问题是 - 为什么这些预测集彼此不同?为什么它们如此不同?
我认为某些事情必须在我不知道的情况下发生,但我希望这些是相同的,因为我假设fit()
训练了一个模型(并且有一些与这个训练过的模型相关的预测)然后predict()
采取该精确模型并将其重新应用于(在这种情况下)相同的数据 - 因此两者的预测应该是相同的。
我错过了什么?任何建议或帮助理解将不胜感激 - 在此先感谢!
# Load required libraries
library(tidymodels); library(modeldata)
#> Registered S3 method overwritten by 'tune':
#> method from
#> required_pkgs.model_spec parsnip
# Set seed
set.seed(123)
# Split up data into training and test
data(cells, package = "modeldata")
# Define Model
rf_mod <- rand_forest(trees = 1000) %>%
set_engine("ranger") %>%
set_mode("classification")
# Fit the model to training data and then predict on same training data
rf_fit <- rf_mod %>%
fit(class ~ ., data = cells)
rf_training_pred <- rf_fit %>%
predict(cells, type = "prob")
# Evaluate accuracy
data.frame(rf_fit$fit$predictions) %>%
bind_cols(cells %>% select(class)) %>%
roc_auc(truth = class, PS)
#> # A tibble: 1 x 3
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 roc_auc binary 0.903
rf_training_pred %>%
bind_cols(cells %>% select(class)) %>%
roc_auc(truth = class, .pred_PS)
#> # A tibble: 1 x 3
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 roc_auc binary 1.00
由reprex 包于 2021-09-25 创建(v2.0.1)