Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add selected_features method to all learners #1230

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions R/Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,21 @@ Learner = R6Class("Learner",
}

return(invisible(self))
},

#' @description
#' Returns the features selected by the model.
#' The field `selected_features_impute` controls the behavior if the learner does not support feature selection.
#' If set to `"error"`, an error is thrown, otherwise all features are returned.
selected_features = function() {
if (is.null(self$model)) {
stopf("No model stored")
}
if (private$.selected_features_impute == "error") {
stop("Learner does not support feature selection")
} else {
self$state$feature_names
}
}
),

Expand Down Expand Up @@ -669,6 +684,17 @@ Learner = R6Class("Learner",
private$.hotstart_stack = rhs
},

#' @field selected_features_impute (`character(1)`)\cr
#' Controls the behavior if the learner does not support feature selection.
#' If set to `"error"`, an error is thrown.
#' If set to `"all"` the complete feature set is returned.
selected_features_impute = function(rhs) {
if (missing(rhs)) {
return(private$.selected_features_impute)
}
private$.selected_features_impute = assert_choice(rhs, c("error", "all"))
},

#' @field predict_types (`character()`)\cr
#' Stores the possible predict types the learner is capable of.
#' A complete list of candidate predict types, grouped by task type, is stored in [`mlr_reflections$learner_predict_types`][mlr_reflections].
Expand All @@ -686,6 +712,7 @@ Learner = R6Class("Learner",
.predict_types = NULL,
.param_set = NULL,
.hotstart_stack = NULL,
.selected_features_impute = "error",

deep_clone = function(name, value) {
switch(name,
Expand Down
18 changes: 18 additions & 0 deletions man/Learner.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/LearnerClassif.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/LearnerRegr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test_Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,21 @@ test_that("configure method works", {
expect_equal(learner$param_set$values$xval, 10)
expect_equal(learner$predict_sets, "train")
})

test_that("selected_features works", {
task = tsk("spam")
# alter rpart class to not support feature selection
fun = LearnerClassifRpart$public_methods$selected_features
on.exit({
LearnerClassifRpart$public_methods$selected_features = fun
})
LearnerClassifRpart$public_methods$selected_features = NULL

learner = lrn("classif.rpart")
expect_error(learner$selected_features(), "No model stored")
learner$train(task)
expect_error(learner$selected_features(), "Learner does not support feature selection")

learner$selected_features_impute = "all"
expect_equal(learner$selected_features(), task$feature_names)
})
Loading