target_encode {dataPreparation}R Documentation

Target encode

Description

Target encoding is the process of replacing a categorical value with the aggregation of the target variable. the target variable. target_encode is used to apply this transformations on a data set. Function build_target_encoding must be used first to compute aggregations.

Usage

target_encode(data_set, target_encoding, drop = FALSE, verbose = TRUE)

Arguments

data_set

Matrix, data.frame or data.table

target_encoding

result of function build_target_encoding (list)

drop

Should col_to_encode be dropped after generation (logical, default to FALSE)

verbose

Should the algorithm talk? (Logical, default to TRUE)

Value

data_set with new cols of target_encoding merged to data_set using target_encoding names as merging key. data_set is edited by reference.

Examples

# Build a data set
require(data.table)
data_set <- data.table(student = c("Marie", "Marie", "Pierre", "Louis", "Louis"),
                      grades = c(1, 1, 2, 3, 4))

# Construct encoding
target_encoding <- build_target_encoding(data_set, cols_to_encode = "student",
                                         target_col = "grades", functions = c("mean", "sum"))

# Apply them
target_encode(data_set, target_encoding = target_encoding)

[Package dataPreparation version 1.0.4 Index]