Impute features by fitting a Learner for each feature.
Uses the features indicated by the context_columns parameter as features to train the imputation Learner.
Note this parameter is part of the PipeOpImpute base class and explained there.
Additionally, only features supported by the learner can be imputed; i.e. learners of type
regr can only impute features of type integer, numeric, POSIXct and Date, while classif can impute
features of type factor, ordered and logical.
The Learner used for imputation is trained on all context_columns; if these contain missing values,
the Learner typically either needs to be able to handle missing values itself, or needs to do its
own imputation (see examples).
Format
R6Class object inheriting from PipeOpImpute/PipeOp.
Construction
id::character(1)
Identifier of resulting object, default"impute.", followed by theidof theLearner.learner::Learner|character(1)Learnerto wrap, or a string identifying aLearnerin themlr3::mlr_learnersDictionary. TheLearnerusually needs to be able to handle missing values, i.e. have themissingsproperty, unless care is taken thatcontext_columnsdo not contain missings; see examples.
This argument is always cloned; to access theLearnerinsidePipeOpImputeLearnerby-reference, use$learner.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 PipeOpImpute.
The output is the input Task with missing values from all affected features imputed by the trained model.
State
The $state is a named list with the $state elements inherited from PipeOpImpute.
The $state$models is a named list of models created by the Learner's $.train() function
for each column. If a column consists of missing values only during training, the model is 0 or the levels of the
feature; these are used for sampling during prediction.
This state is given the class "pipeop_impute_learner_state".
Parameters
The parameters are the parameters inherited from PipeOpImpute, in addition to the parameters of the Learner
used for imputation.
Internals
Uses the $train and $predict functions of the provided learner. Features that are entirely NA are imputed as 0
or randomly sampled from available (factor / logical) levels.
The Learner does not necessarily need to handle missing values in cases
where context_columns is chosen well (or there is only one column with missing values present).
Fields
Fields inherited from PipeOpTaskPreproc/PipeOp, as well as:
learner_models::listofLearner|NULLLearnerthat is being wrapped. This list is named by features for which aLearnerwas fitted, and contains the sameLearner, but with different respective models for each feature. If thisPipeOpis not trained, this is an emptylist. For features that were entirelyNAduring training, thelistcontainsNULLelements.
Methods
Only methods inherited from PipeOpImpute/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_classweightsex,
mlr_pipeops_colapply,
mlr_pipeops_collapsefactors,
mlr_pipeops_colroles,
mlr_pipeops_copy,
mlr_pipeops_datefeatures,
mlr_pipeops_decode,
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_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_materialize,
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_splines,
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
Other Imputation PipeOps:
PipeOpImpute,
mlr_pipeops_imputeconstant,
mlr_pipeops_imputehist,
mlr_pipeops_imputemean,
mlr_pipeops_imputemedian,
mlr_pipeops_imputemode,
mlr_pipeops_imputeoor,
mlr_pipeops_imputesample
Examples
library("mlr3")
task = tsk("diabetes")
task$missings()
#> diabetes age glucose insulin mass pedigree pregnant pressure
#> 0 0 5 405 13 0 0 35
#> triceps
#> 251
po = po("imputelearner", lrn("regr.rpart"))
new_task = po$train(list(task = task))[[1]]
new_task$missings()
#> diabetes age pedigree pregnant glucose insulin mass pressure
#> 0 0 0 0 0 0 0 0
#> triceps
#> 0
# '$state' of the "regr.rpart" Learner, trained to predict the 'mass' column:
po$state$model$mass
#> $model
#> n= 755
#>
#> node), split, n, deviance, yval
#> * denotes terminal node
#>
#> 1) root 755 37110.0000 32.86503
#> 2) triceps< 28.5 272 7845.6920 29.45551
#> 4) triceps< 21.5 129 2519.8400 27.53178 *
#> 5) triceps>=21.5 143 4417.7980 31.19091
#> 10) glucose< 124 81 2762.1110 29.63827 *
#> 11) glucose>=124 62 1205.3170 33.21935 *
#> 3) triceps>=28.5 483 24321.7100 34.78509
#> 6) pressure< 74.5 278 10929.7900 32.81079
#> 12) pregnant>=3.5 159 6495.9200 31.63459
#> 24) age< 31.5 45 1189.1280 28.66000 *
#> 25) age>=31.5 114 4751.4510 32.80877
#> 50) age>=49.5 28 807.7371 29.57143 *
#> 51) age< 49.5 86 3554.7210 33.86279 *
#> 13) pregnant< 3.5 119 3919.9930 34.38235
#> 26) triceps< 39.5 86 2481.2570 33.12907 *
#> 27) triceps>=39.5 33 951.6224 37.64848 *
#> 7) pressure>=74.5 205 10838.8400 37.46244
#> 14) triceps< 35.5 90 3074.9580 34.30444 *
#> 15) triceps>=35.5 115 6163.8780 39.93391
#> 30) glucose< 162.5 80 3396.0090 38.11625 *
#> 31) glucose>=162.5 35 1899.4150 44.08857 *
#>
#> $param_vals
#> $param_vals$xval
#> [1] 0
#>
#>
#> $log
#> Empty data.table (0 rows and 3 cols): stage,class,condition
#>
#> $train_time
#> elapsed
#> 0.004
#>
#> $task_hash
#> [1] "2ac44a951f63767e"
#>
#> $feature_names
#> [1] "age" "glucose" "insulin" "pedigree" "pregnant" "pressure" "triceps"
#>
#> $validate
#> NULL
#>
#> $mlr3_version
#> [1] ‘1.8.0’
#>
#> $data_prototype
#> Empty data.table (0 rows and 8 cols): .impute_col,age,glucose,insulin,pedigree,pregnant...
#>
#> $train_task
#>
#> ── <TaskRegr> (768x8) ──────────────────────────────────────────────────────────
#> • Target: .impute_col
#> • Properties: -
#> • Features (7):
#> • dbl (7): age, glucose, insulin, pedigree, pregnant, pressure, triceps
#>
#> attr(,"class")
#> [1] "learner_state" "list"
library("mlr3learners")
# To use the "regr.lm" Learner, prefix it with its own imputation method!
# The "imputehist" PipeOp is used to train "regr.lm"; predictions of this
# trained Learner are then used to impute the missing values in the Task.
po = po("imputelearner",
po("imputehist") %>>% lrn("regr.lm")
)
new_task = po$train(list(task = task))[[1]]
new_task$missings()
#> diabetes age pedigree pregnant glucose insulin mass pressure
#> 0 0 0 0 0 0 0 0
#> triceps
#> 0
