View source: R/CallbackResample.R
callback_resample | R Documentation |
Function to create a CallbackResample.
Predefined callbacks are stored in the dictionary mlr_callbacks and can be retrieved with clbk()
.
Evaluation callbacks are called at different stages of the resampling process.
Each stage is called once per resampling iteration.
The stages are prefixed with on_resample_*
.
The text in brackets indicates what happens between the stages in the internal workhorse()
function and which accesses to the ContextResample (ctx
) are typical for the stage.
Start Resampling Iteration on Worker - on_resample_begin (Split `ctx$task` into training and test set with `ctx$resampling` and `ctx$iteration`) - on_resample_before_train (Train the learner `ctx$learner` on training data) - on_resample_before_predict (Predict on predict sets and store prediction data `ctx$pdatas`) - on_resample_end (Erase model `ctx$learner$model` if requested and return results) End Resampling Iteration on Worker
The callback can store data in ctx$learner$state
or ctx$data_extra
.
The data in ctx$data_extra
is stored in the ResampleResult or BenchmarkResult.
See also the section on parameters for more information on the stages.
callback_resample(
id,
label = NA_character_,
man = NA_character_,
on_resample_begin = NULL,
on_resample_before_train = NULL,
on_resample_before_predict = NULL,
on_resample_end = NULL
)
id |
( |
label |
( |
man |
( |
on_resample_begin |
( |
on_resample_before_train |
( |
on_resample_before_predict |
( |
on_resample_end |
( |
When implementing a callback, each function must have two arguments named callback
and context
.
A callback can write data to the state ($state
), e.g. settings that affect the callback itself.
We highly discourage changing the task, learner and resampling objects via the callback.
learner = lrn("classif.rpart")
task = tsk("pima")
resampling = rsmp("cv", folds = 3)
# save selected features callback
callback = callback_resample("selected_features",
on_resample_end = function(callback, context) {
context$learner$state$selected_features = context$learner$selected_features()
}
)
rr = resample(task, learner, resampling, callbacks = callback)
rr$learners[[1]]$state$selected_features
# holdout task callback
callback = callback_resample("holdout_task",
on_resample_before_predict = function(callback, context) {
pred = context$learner$predict(callback$state$task)
context$data_extra = list(prediction_holdout = pred)
}
)
task_holdout = tsk("pima")
splits = partition(task, 0.7)
task$filter(splits$train)
task_holdout$filter(splits$test)
callback$state$task = task_holdout
rr = resample(task, learner, resampling, callbacks = callback)
rr$data_extra
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.