Skip to content

Commit 7fcdcf0

Browse files
authored
Merge pull request #960 from mlr-org/PipeOpFixFactors-&-PipeOpTargetInvert---Examples
Add examples for PipeOpTargetInvert and PipeOpFixFactors
2 parents 617a995 + 72b26d3 commit 7fcdcf0

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

R/PipeOpFixFactors.R

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,30 @@
4545
#' @section Methods:
4646
#' Only methods inherited from [`PipeOpTaskPreprocSimple`]/[`PipeOpTaskPreproc`]/[`PipeOp`].
4747
#'
48+
#' @examples
49+
#' library("mlr3")
50+
#'
51+
#' # Reduced task with no entries for the installment_rate < 20 is defined
52+
#' task = tsk("german_credit")
53+
#' rows = task$row_ids[task$data()[, installment_rate != "< 20"]]
54+
#' reduced_task = task$clone(deep = TRUE)$filter(rows)
55+
#' levels(reduced_task$data()$installment_rate)
56+
#'
57+
#' # PipeOp is trained on the reduced task
58+
#' po = po("fixfactors")
59+
#' processed_task = preproc(reduced_task, po)
60+
#' levels(processed_task$data()$installment_rate)
61+
#' summary(processed_task$data()$installment_rate)
62+
#'
63+
#' predicted_task = preproc(task, po, predict = TRUE)
64+
#'
65+
#' # Predictions are made on the task without any missing data
66+
#' levels(predicted_task$data()$installment_rate)
67+
#' summary(predicted_task$data()$installment_rate)
4868
#' @family PipeOps
4969
#' @template seealso_pipeopslist
5070
#' @include PipeOpTaskPreproc.R
5171
#' @export
52-
#' @examples
53-
#' library("mlr3")
5472
PipeOpFixFactors = R6Class("PipeOpFixFactors",
5573
inherit = PipeOpTaskPreprocSimple,
5674
public = list(

R/PipeOpTrafo.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,37 @@ PipeOpTargetTrafo = R6Class("PipeOpTargetTrafo",
221221
#' @section Methods:
222222
#' Only methods inherited from [`PipeOp`].
223223
#'
224+
#' @examplesIf requireNamespace("rpart")
225+
#' library(mlr3)
226+
#' task = tsk("boston_housing")
227+
#' po = PipeOpTargetMutate$new("logtrafo", param_vals = list(
228+
#' trafo = function(x) log(x, base = 2),
229+
#' inverter = function(x) list(response = 2 ^ x$response))
230+
#' )
231+
#' # Note that this example is ill-equipped to work with
232+
#' # `predict_type == "se"` predictions.
233+
#'
234+
#' po$train(list(task))
235+
#' po$predict(list(task))
236+
#'
237+
#' g = Graph$new()
238+
#' g$add_pipeop(po)
239+
#' g$add_pipeop(LearnerRegrRpart$new())
240+
#' g$add_pipeop(PipeOpTargetInvert$new())
241+
#' g$add_edge(src_id = "logtrafo", dst_id = "targetinvert",
242+
#' src_channel = 1, dst_channel = 1)
243+
#' g$add_edge(src_id = "logtrafo", dst_id = "regr.rpart",
244+
#' src_channel = 2, dst_channel = 1)
245+
#' g$add_edge(src_id = "regr.rpart", dst_id = "targetinvert",
246+
#' src_channel = 1, dst_channel = 2)
247+
#'
248+
#' g$train(task)
249+
#' g$predict(task)
250+
#'
251+
#' #syntactic sugar using ppl():
252+
#' tt = ppl("targettrafo", graph = PipeOpLearner$new(LearnerRegrRpart$new()))
253+
#' tt$param_set$values$targetmutate.trafo = function(x) log(x, base = 2)
254+
#' tt$param_set$values$targetmutate.inverter = function(x) list(response = 2 ^ x$response)
224255
#' @family PipeOps
225256
#' @template seealso_pipeopslist
226257
#' @include PipeOp.R

0 commit comments

Comments
 (0)