From 07b9445291e89c99b1b816db62364f2b28888829 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Tue, 7 Mar 2023 10:40:57 +0100 Subject: [PATCH 1/4] feat: allow formula as argument for nnet learner Otherwise, interaction effects can e.g. not be specified. See the SO answer below https://stackoverflow.com/questions/75642879/multiple-runs-and-interaction-terms-in-mlr3-regr-nnet-task --- R/LearnerClassifNnet.R | 12 +++++++++--- R/LearnerRegrNnet.R | 12 +++++++++--- inst/paramtest/test_paramtest_classif.nnet.R | 1 - inst/paramtest/test_paramtest_regr.nnet.R | 1 - man/mlr_learners_classif.nnet.Rd | 8 ++++++++ man/mlr_learners_regr.nnet.Rd | 8 ++++++++ 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/R/LearnerClassifNnet.R b/R/LearnerClassifNnet.R index 615b69cf..b9ec1250 100644 --- a/R/LearnerClassifNnet.R +++ b/R/LearnerClassifNnet.R @@ -17,6 +17,9 @@ #' - Adjusted default: 3L. #' - Reason for change: no default in `nnet()`. #' +#' @section Custom mlr3 parameters: +#' - `formula`: if not provided, the formula is set to `task$formula()`. +#' #' @references #' `r format_bib("ripley_1996")` #' @@ -46,7 +49,8 @@ LearnerClassifNnet = R6Class("LearnerClassifNnet", size = p_int(0L, default = 3L, tags = "train"), skip = p_lgl(default = FALSE, tags = "train"), subset = p_uty(tags = "train"), - trace = p_lgl(default = TRUE, tags = "train") + trace = p_lgl(default = TRUE, tags = "train"), + formula = p_uty(tags = "train") ) ps$values = list(size = 3L) @@ -68,9 +72,11 @@ LearnerClassifNnet = R6Class("LearnerClassifNnet", if ("weights" %in% task$properties) { pv = insert_named(pv, list(weights = task$weights$weight)) } - f = task$formula() + if (is.null(pv$formula)) { + pv$formula = task$formula() + } data = task$data() - invoke(nnet::nnet.formula, formula = f, data = data, .args = pv) + invoke(nnet::nnet.formula, data = data, .args = pv) }, .predict = function(task) { diff --git a/R/LearnerRegrNnet.R b/R/LearnerRegrNnet.R index 68c579ed..a0de9d52 100644 --- a/R/LearnerRegrNnet.R +++ b/R/LearnerRegrNnet.R @@ -17,6 +17,9 @@ #' - Adjusted default: 3L. #' - Reason for change: no default in `nnet()`. #' +#' @section Custom mlr3 parameters: +#' - `formula`: if not provided, the formula is set to `task$formula()`. +#' #' @references #' `r format_bib("ripley_1996")` #' @@ -46,7 +49,8 @@ LearnerRegrNnet = R6Class("LearnerRegrNnet", size = p_int(0L, default = 3L, tags = "train"), skip = p_lgl(default = FALSE, tags = "train"), subset = p_uty(tags = "train"), - trace = p_lgl(default = TRUE, tags = "train") + trace = p_lgl(default = TRUE, tags = "train"), + formula = p_uty(tags = "train") ) ps$values = list(size = 3L) @@ -68,10 +72,12 @@ LearnerRegrNnet = R6Class("LearnerRegrNnet", if ("weights" %in% task$properties) { pv = insert_named(pv, list(weights = task$weights$weight)) } - f = task$formula() + if (is.null(pv$formula)) { + pv$formula = task$formula() + } data = task$data() # force linout = TRUE for regression - invoke(nnet::nnet.formula, formula = f, data = data, linout = TRUE, .args = pv) + invoke(nnet::nnet.formula, data = data, linout = TRUE, .args = pv) }, .predict = function(task) { diff --git a/inst/paramtest/test_paramtest_classif.nnet.R b/inst/paramtest/test_paramtest_classif.nnet.R index d7ed0351..7fb163ce 100644 --- a/inst/paramtest/test_paramtest_classif.nnet.R +++ b/inst/paramtest/test_paramtest_classif.nnet.R @@ -7,7 +7,6 @@ test_that("classif.nnet", { "x", # handled via mlr3 "y", # handled via mlr3 "weights", # handled via mlr3 - "formula", # handled via mlr3 "data", # handled via mlr3 "entropy", # automatically set to TRUE if two-class task "softmax", # automatically set to TRUE if multi-class task diff --git a/inst/paramtest/test_paramtest_regr.nnet.R b/inst/paramtest/test_paramtest_regr.nnet.R index d145c587..6cdfa2b8 100644 --- a/inst/paramtest/test_paramtest_regr.nnet.R +++ b/inst/paramtest/test_paramtest_regr.nnet.R @@ -7,7 +7,6 @@ test_that("regr.nnet", { "x", # handled via mlr3 "y", # handled via mlr3 "weights", # handled via mlr3 - "formula", # handled via mlr3 "data", # handled via mlr3 "linout", # automatically set to TRUE, since it's the regression learner "entropy", # mutually exclusive with linout diff --git a/man/mlr_learners_classif.nnet.Rd b/man/mlr_learners_classif.nnet.Rd index a7e0b028..85a05d10 100644 --- a/man/mlr_learners_classif.nnet.Rd +++ b/man/mlr_learners_classif.nnet.Rd @@ -49,6 +49,7 @@ lrn("classif.nnet") skip \tab logical \tab FALSE \tab TRUE, FALSE \tab - \cr subset \tab untyped \tab - \tab \tab - \cr trace \tab logical \tab TRUE \tab TRUE, FALSE \tab - \cr + formula \tab untyped \tab - \tab \tab - \cr } } @@ -63,6 +64,13 @@ lrn("classif.nnet") } } +\section{Custom mlr3 parameters}{ + +\itemize{ +\item \code{formula}: if not provided, the formula is set to \code{task$formula()}. +} +} + \examples{ if (requireNamespace("nnet", quietly = TRUE)) { learner = mlr3::lrn("classif.nnet") diff --git a/man/mlr_learners_regr.nnet.Rd b/man/mlr_learners_regr.nnet.Rd index bc6e3415..65962304 100644 --- a/man/mlr_learners_regr.nnet.Rd +++ b/man/mlr_learners_regr.nnet.Rd @@ -49,6 +49,7 @@ lrn("regr.nnet") skip \tab logical \tab FALSE \tab TRUE, FALSE \tab - \cr subset \tab untyped \tab - \tab \tab - \cr trace \tab logical \tab TRUE \tab TRUE, FALSE \tab - \cr + formula \tab untyped \tab - \tab \tab - \cr } } @@ -63,6 +64,13 @@ lrn("regr.nnet") } } +\section{Custom mlr3 parameters}{ + +\itemize{ +\item \code{formula}: if not provided, the formula is set to \code{task$formula()}. +} +} + \examples{ if (requireNamespace("nnet", quietly = TRUE)) { learner = mlr3::lrn("regr.nnet") From b8fb8b27bd2779f473422e167aa4728db03abaab Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Tue, 7 Mar 2023 15:25:29 +0100 Subject: [PATCH 2/4] fix: allow integer as feature type for nnet learner --- R/LearnerClassifNnet.R | 2 +- R/LearnerRegrNnet.R | 2 +- man/mlr_learners_classif.nnet.Rd | 2 +- man/mlr_learners_regr.nnet.Rd | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/LearnerClassifNnet.R b/R/LearnerClassifNnet.R index b9ec1250..a9d34720 100644 --- a/R/LearnerClassifNnet.R +++ b/R/LearnerClassifNnet.R @@ -57,7 +57,7 @@ LearnerClassifNnet = R6Class("LearnerClassifNnet", super$initialize( id = "classif.nnet", packages = c("mlr3learners", "nnet"), - feature_types = c("numeric", "factor", "ordered"), + feature_types = c("numeric", "factor", "ordered", "integer"), predict_types = c("prob", "response"), param_set = ps, properties = c("twoclass", "multiclass", "weights"), diff --git a/R/LearnerRegrNnet.R b/R/LearnerRegrNnet.R index a0de9d52..63c32baa 100644 --- a/R/LearnerRegrNnet.R +++ b/R/LearnerRegrNnet.R @@ -57,7 +57,7 @@ LearnerRegrNnet = R6Class("LearnerRegrNnet", super$initialize( id = "regr.nnet", packages = c("mlr3learners", "nnet"), - feature_types = c("numeric", "factor", "ordered"), + feature_types = c("numeric", "factor", "ordered", "integer"), predict_types = c("response"), param_set = ps, properties = c("weights"), diff --git a/man/mlr_learners_classif.nnet.Rd b/man/mlr_learners_classif.nnet.Rd index 85a05d10..85f8e694 100644 --- a/man/mlr_learners_classif.nnet.Rd +++ b/man/mlr_learners_classif.nnet.Rd @@ -25,7 +25,7 @@ lrn("classif.nnet") \itemize{ \item Task type: \dQuote{classif} \item Predict Types: \dQuote{response}, \dQuote{prob} -\item Feature Types: \dQuote{numeric}, \dQuote{factor}, \dQuote{ordered} +\item Feature Types: \dQuote{integer}, \dQuote{numeric}, \dQuote{factor}, \dQuote{ordered} \item Required Packages: \CRANpkg{mlr3}, \CRANpkg{mlr3learners}, \CRANpkg{nnet} } } diff --git a/man/mlr_learners_regr.nnet.Rd b/man/mlr_learners_regr.nnet.Rd index 65962304..50bfb588 100644 --- a/man/mlr_learners_regr.nnet.Rd +++ b/man/mlr_learners_regr.nnet.Rd @@ -25,7 +25,7 @@ lrn("regr.nnet") \itemize{ \item Task type: \dQuote{regr} \item Predict Types: \dQuote{response} -\item Feature Types: \dQuote{numeric}, \dQuote{factor}, \dQuote{ordered} +\item Feature Types: \dQuote{integer}, \dQuote{numeric}, \dQuote{factor}, \dQuote{ordered} \item Required Packages: \CRANpkg{mlr3}, \CRANpkg{mlr3learners}, \CRANpkg{nnet} } } From 4452449663b59f70385eb2dd6e97d30170a5fea6 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 24 Mar 2023 07:58:15 +0100 Subject: [PATCH 3/4] docs: update news --- NEWS.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 5df18b57..bc690111 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,8 @@ -# mlr3learners 0.5.5 +# mlr3learners 0.5.6-9000 + +* Added formula argument to `nnet` learner + +# mlr3learners 0.5.6 - Enable new early stopping mechanism for xgboost. - Improved documentation. From efe7559fa282b2c39463d5515fffb0ab52f2b997 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 24 Mar 2023 07:59:15 +0100 Subject: [PATCH 4/4] docs: update news [skip ci] --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index bc690111..342a46dd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # mlr3learners 0.5.6-9000 -* Added formula argument to `nnet` learner +* Added formula argument to `nnet` learner and support feature type `"integer"` # mlr3learners 0.5.6