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

glmnet: importance() method #76

Closed
wants to merge 13 commits into from
Closed
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
7 changes: 1 addition & 6 deletions .github/workflows/R-CMD-check-extra.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
on:
push:
branches:
- master
schedule:
- cron: '15 8 * * *'
on: [push, pull_request]

name: R-CMD-check-extra

Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
on:
push:
branches:
- master
pull_request:
branches:
- master
on: [push, pull_request]

name: R-CMD-check

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
.DS_Store
.Rproj.user

docs/
docs/
.vscode
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ RdMacros: mlr3misc
Encoding: UTF-8
NeedsCompilation: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
39 changes: 35 additions & 4 deletions R/LearnerClassifGlmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,43 @@ LearnerClassifGlmnet = R6Class("LearnerClassifGlmnet", inherit = LearnerClassif,
param_set = ps,
predict_types = c("response", "prob"),
feature_types = c("logical", "integer", "numeric"),
properties = c("weights", "twoclass", "multiclass"),
properties = c("weights", "twoclass", "multiclass", "importance"),
packages = "glmnet",
man = "mlr3learners::mlr_learners_classif.glmnet"
)
},

#' @description
#' The importance scores are extracted from the beta coefficients of the
#' fitted model.
#' @param task ([Task])\cr
#' Task to calculate the importance on. If a model was already fitted,
#' no refitting is done.
#' @return Named `numeric()`.
importance = function(task) {

if (is.null(self$model)) {
self$train(task)
}

if ("multiclass" %in% task$properties) {
mlr3misc::stopf("Learner 'glmnet' only support feature importance for
'twoclass' tasks.", wrap = TRUE)
}
model = self$model$glmnet.fit

res = mlr3misc::map_dbl(seq_len(nrow(model$beta)), function(.i) {
ind = which(model$beta[.i, ] != 0)[1]
model$lambda[ind]
})

names(res) = model$beta@Dimnames[[1]]
sort(res, decreasing = TRUE)
}
),

private = list(

.train = function(task) {

pars = self$param_set$get_values(tags = "train")
Expand All @@ -76,7 +105,7 @@ LearnerClassifGlmnet = R6Class("LearnerClassifGlmnet", inherit = LearnerClassif,
if ("weights" %in% task$properties) {
pars$weights = task$weights$weight
}
pars$family = ifelse(length(task$class_names) == 2L, "binomial", "multinomial")
pars$family = ifelse(length(task$class_names) == 2L, "binomial", "multinomial") # nolint

saved_ctrl = glmnet::glmnet.control()
on.exit(invoke(glmnet::glmnet.control, .args = saved_ctrl))
Expand All @@ -96,10 +125,12 @@ LearnerClassifGlmnet = R6Class("LearnerClassifGlmnet", inherit = LearnerClassif,
newdata = as.matrix(task$data(cols = task$feature_names))

if (self$predict_type == "response") {
response = invoke(predict, self$model, newx = newdata, type = "class", .args = pars)
response = invoke(predict, self$model, newx = newdata, type = "class",
.args = pars)
PredictionClassif$new(task = task, response = drop(response))
} else {
prob = invoke(predict, self$model, newx = newdata, type = "response", .args = pars)
prob = invoke(predict, self$model, newx = newdata, type = "response",
.args = pars)

if (length(task$class_names) == 2L) {
prob = cbind(prob, 1 - prob)
Expand Down
26 changes: 24 additions & 2 deletions R/LearnerRegrGlmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,31 @@ LearnerRegrGlmnet = R6Class("LearnerRegrGlmnet", inherit = LearnerRegr,
id = "regr.glmnet",
param_set = ps,
feature_types = c("logical", "integer", "numeric"),
properties = "weights",
properties = c("weights", "importance"),
packages = "glmnet",
man = "mlr3learners::mlr_learners_regr.glmnet"
)
},

#' @description
#' The importance scores are extracted from the beta coefficients of the
#' fitted model.
#' @return Named `numeric()`.
importance = function() {

if (is.null(self$model)) {
stopf("No model stored. Please train the model first.")
}

model = self$model$glmnet.fit

res = mlr3misc::map_dbl(seq_len(nrow(model$beta)), function(.i) {
ind = which(model$beta[.i, ] != 0)[1]
model$lambda[ind]
})

names(res) = model$beta@Dimnames[[1]]
sort(res, decreasing = TRUE)
}
),

Expand Down Expand Up @@ -100,7 +121,8 @@ LearnerRegrGlmnet = R6Class("LearnerRegrGlmnet", inherit = LearnerRegr,
pars = self$param_set$get_values(tags = "predict")
newdata = as.matrix(task$data(cols = task$feature_names))

response = invoke(predict, self$model, newx = newdata, type = "response", .args = pars)
response = invoke(predict, self$model, newx = newdata, type = "response",
.args = pars)
PredictionRegr$new(task = task, response = drop(response))
}
)
Expand Down
26 changes: 26 additions & 0 deletions man/mlr_learners_classif.glmnet.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.kknn.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.lda.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.log_reg.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.multinom.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.naive_bayes.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.qda.Rd

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

4 changes: 4 additions & 0 deletions man/mlr_learners_classif.ranger.Rd

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

2 changes: 2 additions & 0 deletions man/mlr_learners_classif.svm.Rd

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

3 changes: 3 additions & 0 deletions man/mlr_learners_classif.xgboost.Rd

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

Loading