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
id::character(1)
Identifier of resulting object, default"decode".param_vals:: namedlist
List of hyperparameter settings, overwriting the hyperparameter settings that would otherwise be set during construction. Defaultlist().
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:: namedlist
Named list of named character vectors. Each element is named according to the new column name extracted bygroup_pattern. Each vector contains the level names for the new factor column that should be created, named by the corresponding old column name. Iftreatment_encodingisTRUE, then each vector also containsref_nameas the reference class with an empty string as name.treatment_encoding::logical(1)
Value oftreatment_encodinghyperparameter.cutoff::numeric(1)
Value oftreatment_encodinghyperparameter, or0if that is not given.ties_method::character(1)
Value ofties_methodhyperparameter.
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 whatgroup_patternmatches). If set to"", all columns matching thegroup_patternare collapsed into one factor column calledpipeop.decoded. UsePipeOpRenameColumnsto 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)
IfTRUE, treatment encoding is assumed instead of one-hot encoding. Initialized toFALSE.treatment_cutoff::numeric(1)
Iftreatment_encodingisTRUE, specifies a cutoff value for identifying the reference level. The reference level is set toref_namein rows where the value is less than or equal to a specified cutoff value (e.g.,0) in all columns in that group. Default is0.ref_name::character(1)
Iftreatment_encodingisTRUE, 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".
Fields
Only fields inherited from PipeOp.
Methods
Only methods inherited from PipeOpTaskPreprocSimple/PipeOpTaskPreproc/PipeOp.
See also
https://mlr-org.com/pipeops.html
Other PipeOps:
PipeOp,
PipeOpEncodePL,
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_encodeplquantiles,
mlr_pipeops_encodepltree,
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_info,
mlr_pipeops_isomap,
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.70246251 0.62041003 1 1
#> 2: 0.16502764 0.16957677 2 2
#> 3: 0.06445754 0.06221405 1 1
#> 4: 0.75470562 0.10902927 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.3817164 0 0
#> 2: 0.1693109 1 0
#> 3: 0.2986525 0 1
#> 4: 0.1922095 0 0
#> 5: 0.2571700 1 0
#> 6: 0.1812318 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.3817164 a
#> 2: 0.1693109 b
#> 3: 0.2986525 c
#> 4: 0.1922095 a
#> 5: 0.2571700 b
#> 6: 0.1812318 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.47731371 1 1
#> 2: 0.77073704 2 2
#> 3: 0.02778712 1 1
#> 4: 0.52731078 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.47731371 y_1
#> 2: 0.77073704 x_2
#> 3: 0.02778712 y_1
#> 4: 0.52731078 y_2
