preproc {mlr3pipelines} | R Documentation |
Simple Pre-processing
Description
Function that offers a simple and direct way to train or predict PipeOp
s and Graph
s on Task
s,
data.frame
s or data.table
s.
Training happens if predict
is set to FALSE
and no state
is passed to this function.
Prediction happens if predict
is set to TRUE
and if the passed Graph
or PipeOp
is either trained or a state
is explicitly passed to this function.
The passed PipeOp
or Graph
gets modified by-reference.
Usage
preproc(indata, processor, state = NULL, predict = !is.null(state))
Arguments
indata |
( |
processor |
( |
state |
(named |
predict |
( |
Value
any
| data.frame
| data.table
:
If indata
is a Task
, whatever is returned by the processor
's single output channel is returned.
If indata
is a data.frame
or data.table
, an object of the same class is returned, or
if the processor
's output channel does not return a Task
, an error is thrown.
Internals
If processor
is a PipeOp
, the S3 method preproc.PipeOp
gets called first, converting the PipeOp
into a
Graph
and wrapping the state
appropriately, before calling the S3 method preproc.Graph
with the modified objects.
If indata
is a data.frame
or data.table
, a
TaskUnsupervised
is constructed internally. This implies that processor
s which only work on sub-classes
of TaskSupervised
will not work with these input types for indata
.
Examples
library("mlr3")
task = tsk("iris")
pop = po("pca")
# Training
preproc(task, pop)
# Note that the PipeOp gets trained through this
pop$is_trained
# Predicting a trained PipeOp (trained through previous call to preproc)
preproc(task, pop, predict = TRUE)
# Predicting using a given state
# We use the state of the PipeOp from the last example and then reset it
state = pop$state
pop$state = NULL
preproc(task, pop, state)
# Note that the PipeOp's state may get overwritten inadvertently during
# training or if a state is given
pop$state$sdev
preproc(tsk("wine"), pop)
pop$state$sdev
# Piping multiple preproc() calls, using dictionary sugar to set parameters
# tsk("penguins") |>
# preproc(po("imputemode", affect_columns = selector_name("sex"))) |>
# preproc(po("imputemean"))
# Use preproc with a Graph
gr = po("pca", rank. = 4) %>>% po("learner", learner = lrn("classif.rpart"))
preproc(tsk("sonar"), gr) # returns NULL because of the learner
preproc(tsk("sonar"), gr, predict = TRUE)
# Training with a data.table input
# Note that `$data()` drops the information that "Species" is the target.
# It gets handled like an ordinary feature here.
dt = tsk("iris")$data()
preproc(dt, pop)
# Predicting with a data.table input
preproc(dt, pop)