現在フレームワークを使用しており、ランダム フォレスト ( を使用) とブースト回帰ツリー ( を使用)tidymodels
のトレーニング済みモデルの出力の違いを理解するのに苦労しています。ranger
xgboost
私の質問は使用に関するものですxgboost
- 具体的には、トレーニングされている基礎となるモデルのトレーニングデータへの予測/適合にどのようにアクセスできますか(を使用せずにpredict
)。
言いたいことを明確にするために、ランダム フォレスト モデルをフィッティングするときに、フィッティング モデル (rf_fit
以下の表現で) とトレーニング データに対するその予測を 2 つの方法で調べることができます。
- 使用
predict()
- 呼び出しpredict(rf_fit, cells, type = "prob"
。(方法 1)。 rf_fit
直接 ( )から予測を取得するrf_fit$fit$predictions
(方法 2)。
これらは、ここで明らかにされた理由により、異なる予測をもたらします。
この場合、rf_fit$fit$predictions
ブーストされた回帰木と私のxgb_fit
オブジェクトの (つまり方法 2) と同等のものに特に関心があります。私の質問は 2 つあります。
xgb_fit
トレーニング済みモデルからの予測はどこにありますか?rf_fit$fit$predictions
(つまり、ランダムフォレストモデルで得られるものと同等のものはどこにありますか)?または、これらの予測を出力するには何を追加する必要がありますか?- 上記が可能である場合、これらの予測をどのように解釈すればよいですか? それらは呼び出しとは異なり
predict
ますか?もしそうなら、それらは何を表しているのですか (アウトオブバッグの推定値は、ブーストされた回帰ツリーにとって自明ではありません)?
(基本的に、training_logloss
反復 1000 でエラーを生成したモデルからの予測が必要ですxgb_fit$fit$evaluation_log
)。
# 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)
# Load in data
data(cells, package = "modeldata")
# Define Random Forest Model
rf_mod <- rand_forest(trees = 1000) %>%
set_mode("classification") %>%
set_engine("ranger")
# Define BRT Model
xgb_mod <- boost_tree(trees = 1000) %>%
set_mode("classification") %>%
set_engine("xgboost",
objective = 'binary:logistic',
eval_metric = 'logloss')
# Fit the models to training data
rf_fit <- rf_mod %>%
fit(class ~ ., data = cells)
xgb_fit <- xgb_mod %>%
fit(class ~ ., data = cells)
xgb_fit$fit$evaluation_log
#> iter training_logloss
#> 1: 1 0.542353
#> 2: 2 0.443275
#> 3: 3 0.382232
#> 4: 4 0.333377
#> 5: 5 0.303415
#> ---
#> 996: 996 0.001918
#> 997: 997 0.001917
#> 998: 998 0.001917
#> 999: 999 0.001916
#> 1000: 1000 0.001915
# Examine output predictions on training data for RANDOM FOREST Model
rf_whole <- predict(rf_fit, cells, type = "prob") # predictions based on whole fitted model
rf_oob <- head(rf_fit$fit$predictions) # predictions based on out of bag samples
## these are different to each other as we would expect
rf_whole$.pred_PS[1]
#> [1] 0.9229111
rf_oob[1, "PS"]
#> PS
#> 0.8503902
# Examine output predictions on training data for BOOSTED REGRESSION TREE Model
xgb_whole <- predict(xgb_fit, cells, type = "prob")
reprex
#> Error in eval(expr, envir, enclos): object 'reprex' not found
reprex パッケージ(v2.0.1)により 2021-10-05 に作成