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

基于mlr与xgboost:如何从重采样模型创建对应Learner对象

Convert Trained Wrapped Models from resample() to mlr Learner Objects

Great question! I’ve dealt with this exact situation in mlr before—when you resample wrapped learners (like your combination of FilterWrapper + TuneWrapper), you end up with trained WrappedModel objects instead of the reusable, untrained Learner templates you might want for further tuning, ensembling, or deployment.

Unfortunately, mlr doesn’t have a built-in helper function for this conversion directly, but we can easily build a custom function to extract the key details from your resampled models and reconstruct the corresponding Learners. Here’s how:

Step 1: Create a Conversion Function

This function will pull the filtered features and tuned hyperparameters from each trained model, then rebuild the wrapped Learner with those exact settings:

trainedModelToLearner <- function(trained_model, base_learner = "classif.xgboost", target = "y", positive = 1) {
  # Extract the features selected by FilterWrapper
  filtered_features <- getFilteredFeatures(trained_model)
  
  # Extract the optimal hyperparameters found by TuneWrapper
  tune_results <- getTuneResult(trained_model)
  optimal_params <- tune_results$x
  
  # Reconstruct the wrapped Learner
  learner <- makeLearner(base_learner, target = target, positive = positive) %>%
    # Use the pre-selected features directly instead of re-running filtering
    makeFilterWrapper(
      fw.method = "auc",
      fw.abs = length(filtered_features),
      fw.features = filtered_features
    ) %>%
    # Set the tuned hyperparameters
    setHyperPars(par.vals = optimal_params)
  
  return(learner)
}

Key Notes on the Function:

  • We use fw.features to directly specify the pre-filtered features, which skips re-running the AUC filter (saves time and ensures consistency with your resampled models).
  • The function assumes your base learner is classif.xgboost; adjust the base_learner parameter if you’re using a different one.
  • We pull the optimal parameters from getTuneResult(trained_model)$x, which gives the exact values that performed best during tuning.

Step 2: Generate Your Learner List

Apply the function to your resample results to create a list of ready-to-use Learners:

# Convert all resampled models to Learners
learner_list <- lapply(r$models, trainedModelToLearner)

Step 3: Verify the Results

You can double-check that the Learners match your original resampled models with these quick checks:

  • getFilteredFeatures(learner_list[[1]]) should match the features from the first resampled model
  • getHyperPars(learner_list[[1]]) should show the tuned hyperparameters from that model

Once you have this list, you can use these Learners for further tuning (if needed), ensemble methods, or training on new data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:34:28