Skip to contents

Reverses one-hot or treatment encoding of columns. It collapses multiple numeric or integer columns into one factor column based on a pre-specified grouping pattern of column names.

May be applied to multiple groups of columns, grouped by matching a common naming pattern. The grouping pattern is extracted to form the name of the newly derived factor column, and levels are constructed from the previous column names, with parts matching the grouping pattern removed (see examples). The level per row of the new factor column is generally determined as the name of the column with the maximum value in the group.

Format

R6Class object inheriting from PipeOpTaskPreprocSimple/PipeOpTaskPreproc/PipeOp.

Construction

PipeOpEncode$new(id = "decode", param_vals = list())

  • id :: character(1)
    Identifier of resulting object, default "decode".

  • param_vals :: named list
    List of hyperparameter settings, overwriting the hyperparameter settings that would otherwise be set during construction. Default list().

Input and Output Channels

Input and output channels are inherited from PipeOpTaskPreproc.

The output is the input Task with encoding columns collapsed into new decoded columns.

State

The $state is a named list with the $state elements inherited from PipeOpTaskPreproc, as well as:

  • colmaps :: named list
    Named list of named character vectors. Each element is named according to the new column name extracted by group_pattern. Each vector contains the level names for the new factor column that should be created, named by the corresponding old column name. If treatment_encoding is TRUE, then each vector also contains ref_name as the reference class with an empty string as name.

  • treatment_encoding :: logical(1)
    Value of treatment_encoding hyperparameter.

  • cutoff :: numeric(1)
    Value of treatment_encoding hyperparameter, or 0 if that is not given.

  • ties_method :: character(1)
    Value of ties_method hyperparameter.

Parameters

The parameters are the parameters inherited from PipeOpTaskPreproc, as well as:

  • group_pattern :: character(1)
    A regular expression to be applied to column names. Should contain a capturing group for the new column name, and match everything that should not be interpreted as the new factor levels (which are constructed as the difference between column names and what group_pattern matches). If set to "", all columns matching the group_pattern are collapsed into one factor column called pipeop.decoded. Use PipeOpRenameColumns to rename this column. Initialized to "^([^.]+)\\.", which would extract everything up to the first dot as the new column name and construct new levels as everything after the first dot.

  • treatment_encoding :: logical(1)
    If TRUE, treatment encoding is assumed instead of one-hot encoding. Initialized to FALSE.

  • treatment_cutoff :: numeric(1)
    If treatment_encoding is TRUE, specifies a cutoff value for identifying the reference level. The reference level is set to ref_name in rows where the value is less than or equal to a specified cutoff value (e.g., 0) in all columns in that group. Default is 0.

  • ref_name :: character(1)
    If treatment_encoding is TRUE, specifies the name for reference levels. Default is "ref".

  • ties_method :: character(1)
    Method for resolving ties if multiple columns have the same value. Specifies the value from which of the columns with the same value is to be picked. Options are "first", "last", or "random". Initialized to "random".

Methods

Only methods inherited from PipeOpTaskPreprocSimple/PipeOpTaskPreproc/PipeOp.

See also

https://mlr-org.com/pipeops.html

Other PipeOps: PipeOp, PipeOpEnsemble, PipeOpImpute, PipeOpTargetTrafo, PipeOpTaskPreproc, PipeOpTaskPreprocSimple, mlr_pipeops, mlr_pipeops_adas, mlr_pipeops_blsmote, mlr_pipeops_boxcox, mlr_pipeops_branch, mlr_pipeops_chunk, mlr_pipeops_classbalancing, mlr_pipeops_classifavg, mlr_pipeops_classweights, mlr_pipeops_colapply, mlr_pipeops_collapsefactors, mlr_pipeops_colroles, mlr_pipeops_copy, mlr_pipeops_datefeatures, mlr_pipeops_encode, mlr_pipeops_encodeimpact, mlr_pipeops_encodelmer, mlr_pipeops_featureunion, mlr_pipeops_filter, mlr_pipeops_fixfactors, mlr_pipeops_histbin, mlr_pipeops_ica, mlr_pipeops_imputeconstant, mlr_pipeops_imputehist, mlr_pipeops_imputelearner, mlr_pipeops_imputemean, mlr_pipeops_imputemedian, mlr_pipeops_imputemode, mlr_pipeops_imputeoor, mlr_pipeops_imputesample, mlr_pipeops_kernelpca, mlr_pipeops_learner, mlr_pipeops_learner_pi_cvplus, mlr_pipeops_learner_quantiles, mlr_pipeops_missind, mlr_pipeops_modelmatrix, mlr_pipeops_multiplicityexply, mlr_pipeops_multiplicityimply, mlr_pipeops_mutate, mlr_pipeops_nearmiss, mlr_pipeops_nmf, mlr_pipeops_nop, mlr_pipeops_ovrsplit, mlr_pipeops_ovrunite, mlr_pipeops_pca, mlr_pipeops_proxy, mlr_pipeops_quantilebin, mlr_pipeops_randomprojection, mlr_pipeops_randomresponse, mlr_pipeops_regravg, mlr_pipeops_removeconstants, mlr_pipeops_renamecolumns, mlr_pipeops_replicate, mlr_pipeops_rowapply, mlr_pipeops_scale, mlr_pipeops_scalemaxabs, mlr_pipeops_scalerange, mlr_pipeops_select, mlr_pipeops_smote, mlr_pipeops_smotenc, mlr_pipeops_spatialsign, mlr_pipeops_subsample, mlr_pipeops_targetinvert, mlr_pipeops_targetmutate, mlr_pipeops_targettrafoscalerange, mlr_pipeops_textvectorizer, mlr_pipeops_threshold, mlr_pipeops_tomek, mlr_pipeops_tunethreshold, mlr_pipeops_unbranch, mlr_pipeops_updatetarget, mlr_pipeops_vtreat, mlr_pipeops_yeojohnson

Examples

library("mlr3")

# Reverse one-hot encoding
df = data.frame(
  target = runif(4),
  x.1 = rep(c(1, 0), 2),
  x.2 = rep(c(0, 1), 2),
  y.1 = rep(c(1, 0), 2),
  y.2 = rep(c(0, 1), 2),
  a = runif(4)
)
task_one_hot = TaskRegr$new(id = "example", backend = df, target = "target")

pop = po("decode")

train_out = pop$train(list(task_one_hot))[[1]]
# x.1 and x.2 are collapsed into x, same for y; a is ignored.
train_out$data()
#>       target          a      x      y
#>        <num>      <num> <fctr> <fctr>
#> 1: 0.2779660 0.06445754      1      1
#> 2: 0.7875405 0.75470562      2      2
#> 3: 0.7024625 0.62041003      1      1
#> 4: 0.1650276 0.16957677      2      2

# Reverse treatment encoding from PipeOpEncode
df = data.frame(
  target = runif(6),
  fct = factor(rep(c("a", "b", "c"), 2))
)
task = TaskRegr$new(id = "example", backend = df, target = "target")

po_enc = po("encode", method = "treatment")
task_encoded = po_enc$train(list(task))[[1]]
task_encoded$data()
#>        target fct.b fct.c
#>         <num> <num> <num>
#> 1: 0.06221405     0     0
#> 2: 0.10902927     1     0
#> 3: 0.38171635     0     1
#> 4: 0.16931091     0     0
#> 5: 0.29865254     1     0
#> 6: 0.19220954     0     1

po_dec = po("decode", treatment_encoding = TRUE)
task_decoded = pop$train(list(task))[[1]]
# x.1 and x.2 are collapsed into x. All rows where all values
# are smaller or equal to 0, the level is set to the reference level.
task_decoded$data()
#>        target    fct
#>         <num> <fctr>
#> 1: 0.06221405      a
#> 2: 0.10902927      b
#> 3: 0.38171635      c
#> 4: 0.16931091      a
#> 5: 0.29865254      b
#> 6: 0.19220954      c

# Different group_pattern
df = data.frame(
  target = runif(4),
  x_1 = rep(c(1, 0), 2),
  x_2 = rep(c(0, 1), 2),
  y_1 = rep(c(2, 0), 2),
  y_2 = rep(c(0, 1), 2)
)
task = TaskRegr$new(id = "example", backend = df, target = "target")

# Grouped by first underscore
pop = po("decode", group_pattern = "^([^_]+)\\_")
train_out = pop$train(list(task))[[1]]
# x_1 and x_2 are collapsed into x, same for y
train_out$data()
#>       target      x      y
#>        <num> <fctr> <fctr>
#> 1: 0.2571700      1      1
#> 2: 0.1812318      2      2
#> 3: 0.4773137      1      1
#> 4: 0.7707370      2      2

# Empty string to collapse all matches into one factor column.
pop$param_set$set_values(group_pattern = "")
train_out = pop$train(list(task))[[1]]
# All columns are combined into a single column.
# The level for each row is determined by the column with the largest value in that row.
# By default, ties are resolved randomly.
train_out$data()
#>       target pipeop.decoded
#>        <num>         <fctr>
#> 1: 0.2571700            y_1
#> 2: 0.1812318            y_2
#> 3: 0.4773137            y_1
#> 4: 0.7707370            x_2