Skip to contents

Apply a trained Preprocessor to new data, reusing the values learned from the training data. For example, the same mean and mode imputation values, scale centers and coefficients, one-hot levels, and removed features will be applied to the new data.

Usage

apply_preprocessor(preprocessor, new_data, verbosity = 1L)

Arguments

preprocessor

Preprocessor: Trained preprocessor, i.e. the output of preprocess.

new_data

Tabular data, i.e. data.frame, data.table, or tbl_df (tibble): New data to preprocess.

verbosity

Integer: Verbosity level.

Value

Preprocessed data, in the same structure as new_data.

Details

Imputation follows the preprocessor's impute_type. "meanMode" imputes the stored training values. "missRanger" applies the forests fitted on the training data with predict(), and imputes a feature that had no missing training values with its stored mean or mode; predict() stops on a factor level of an imputed feature that is absent from the training data. A preprocessor using "micePMM" cannot be applied to new data and is refused.

Author

EDG

Examples

res <- resample(iris, setup_KFold(seed = 2026))
iris_train <- iris[res[[1]], ]
iris_test <- iris[-res[[1]], ]

# Preprocess training data
iris_pre <- preprocess(iris_train, setup_Preprocessor(scale = TRUE, center = TRUE))
#> 2026-10-20 09:07:32 
#> Scaling and centering 4 numeric features...
#>  [preprocess_data]
#> 2026-10-20 09:07:32 
#> Preprocessing done.
#>  [preprocess_data]

# Apply the same preprocessing to test data
iris_test_pre <- apply_preprocessor(iris_pre, iris_test)
#> 2026-10-20 09:07:32 
#> Scaling and centering 4 numeric features...
#>  [preprocess_data]
#> 2026-10-20 09:07:32 
#> Preprocessing done.
#>  [preprocess_data]