You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MLR3框架下如何提取模型在训练集上的预测结果?

提取自动调优XGBoost模型在训练集上的预测结果

核心方法

和你提取测试集预测结果的逻辑完全一致,只需在predict()方法中传入训练集对应的行ID(即你定义的train_rows_outer),就能获取模型在训练集上的预测结果。

完整实现代码

# Auto tuning xgboost
learner_xgboost = lrn("classif.xgboost", predict_type = "prob", nrounds = to_tune(1, 5000), eta = to_tune(1e-4, 1, logscale = TRUE), subsample = to_tune(0.1,1), max_depth = to_tune(1,15), min_child_weight = to_tune(0, 7), colsample_bytree = to_tune(0,1), colsample_bylevel = to_tune(0,1), lambda = to_tune(1e-3, 1e3, logscale = TRUE), alpha = to_tune(1e-3, 1e3, logscale = TRUE))

at_xgboost = auto_tuner(
  tuner= tnr("random_search"),
  learner = learner_xgboost,
  resampling = resampling_inner,
  measure = msr("classif.auc"),
  term_evals = 50,
  store_tuning_instance = TRUE,
  store_models = TRUE,
  store_benchmark_result = TRUE
)

set.seed(12345)
at_xgboost$train(task_IG, row_ids = train_rows_outer)

# 提取测试集预测结果(原代码保留)
predictions_IG_test <- at_xgboost$predict(task_IG, row_ids = test_rows)
predictions_IG_test$score(msr("classif.auc"))
# classif.auc 0.6541043 
predictions_IG_test$score(msr("classif.bacc"))
# classif.bacc 0.6184822 

# 提取训练集预测结果
predictions_IG_train <- at_xgboost$predict(task_IG, row_ids = train_rows_outer)
# 可选:查看训练集上的评估指标
predictions_IG_train$score(msr("classif.auc"))
predictions_IG_train$score(msr("classif.bacc"))

# 整理训练集预测结果为data.table
row_ids_train <- predictions_IG_train$row_ids
truth_train <- predictions_IG_train$truth
response_train <- predictions_IG_train$response
prob.0_train <- predictions_IG_train$prob[, 1]
prob.1_train <- predictions_IG_train$prob[, 2]

Predictions_train_information_gain <- data.table(
  row_ids = row_ids_train, 
  truth = truth_train, 
  response = response_train, 
  prob.0 = prob.0_train, 
  prob.1 = prob.1_train
)

关键注意点

  • 你已经在auto_tuner初始化时设置了store_models = TRUE,这是能调用最优模型进行预测的前提,无需额外修改。
  • 训练集和测试集的预测提取逻辑完全相同,仅需替换row_ids参数为对应的训练集行ID即可。

内容的提问来源于stack exchange,提问作者NDe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 13:42:36