-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set default fallback with set_fallback
- Loading branch information
Showing
5 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#' @title Set a Fallback Learner | ||
#' | ||
#' @description | ||
#' Set a fallback learner for a given learner. | ||
#' The function searches for a suitable fallback learner based on the task type. | ||
#' Additional checks are performed to ensure that the fallback learner supports the predict type. | ||
#' | ||
#' @param learner [Learner]\cr | ||
#' The learner for which a fallback learner should be set. | ||
#' | ||
#' @return | ||
#' Returns the learner itself, but modified **by reference**. | ||
set_fallback = function(learner) { | ||
assert_learner(learner) | ||
|
||
# search for suitable fallback learner | ||
fallback_id = mlr_reflections$learner_fallback[[learner$task_type]] | ||
|
||
if (is.null(fallback_id)) { | ||
stopf("No fallback learner available for task type '%s'.", learner$task_type) | ||
} | ||
|
||
fallback = lrn(fallback_id) | ||
|
||
# set predict type | ||
if (learner$predict_type %nin% fallback$predict_types) { | ||
stopf("Fallback learner '%s' does not support predict type '%s'.", fallback_id, learner$predict_type) | ||
} | ||
|
||
fallback$predict_type = learner$predict_type | ||
|
||
# set quantiles | ||
if (learner$predict_type == "quantiles") { | ||
|
||
if (is.null(learner$quantiles) || is.null(learner$quantile_response)) { | ||
stopf("Cannot set quantiles for fallback learner. Set `$quantiles` and `$quantile_response` in %s.", learner$id) | ||
} | ||
|
||
fallback$quantiles = learner$quantiles | ||
fallback$quantile_response = learner$quantile_response | ||
} | ||
|
||
learner$fallback = fallback | ||
return(learner) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
test_that("set_fallback() works", { | ||
learner = lrn("classif.rpart") | ||
set_fallback(learner) | ||
|
||
expect_class(learner, "LearnerClassifRpart") | ||
expect_class(learner$fallback, "LearnerClassifFeatureless") | ||
expect_equal(learner$fallback$predict_type, "response") | ||
|
||
learner = lrn("classif.rpart", predict_type = "prob") | ||
set_fallback(learner) | ||
|
||
expect_class(learner, "LearnerClassifRpart") | ||
expect_class(learner$fallback, "LearnerClassifFeatureless") | ||
expect_equal(learner$fallback$predict_type, "prob") | ||
|
||
learner = lrn("regr.rpart") | ||
set_fallback(learner) | ||
|
||
expect_class(learner, "LearnerRegrRpart") | ||
expect_class(learner$fallback, "LearnerRegrFeatureless") | ||
expect_equal(learner$fallback$predict_type, "response") | ||
|
||
learner = lrn("regr.debug", predict_type = "se") | ||
set_fallback(learner) | ||
|
||
expect_class(learner, "LearnerRegrDebug") | ||
expect_class(learner$fallback, "LearnerRegrFeatureless") | ||
expect_equal(learner$fallback$predict_type, "se") | ||
}) |