Learn R Programming

dataPreparation (version 0.4.3)

target_encode: 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(dataSet, target_encoding, drop = FALSE, verbose = TRUE)

Arguments

dataSet

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

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

Examples

Run this code
# NOT RUN {
# Build a data set
require(data.table)
dataSet <- data.table(student = c("Marie", "Marie", "Pierre", "Louis", "Louis"), 
                      grades = c(1, 1, 2, 3, 4))

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

# Apply them
target_encode(dataSet, target_encoding = target_encoding)
# }

Run the code above in your browser using DataLab