one_hot_encode: One-hot encoding

View source: R/deepDummy.r

one_hot_encodeR Documentation

One-hot encoding

Description

one_hot_encode rebuilds a categorical variable to a so-called 'one-hot vector'. Within a sample (row) of a one-hot vector (matrix) each level of the variable is rebuild in the binary form (0|1,0|1,0|1,...).

Usage

one_hot_encode(x, ordered = FALSE)

Arguments

x

A vector with values (levels) of a categorical variable.

ordered

A logical value indicating whether the factor levels of x should be encoded in an ordered way or not (default).

Details

An unordered encoding creates an indicator matrix with the value 1 only for the given level of x and 0 for all other levels. In opposite, an ordered encoding assumes an intrinsic order of all levels whereby a given level automatically means reaching and exceeding all previous levels. In that case, all previous levels are also encoded with value 1.

Value

A matrix with all levels as columns with either 0 or 1 values.

See Also

one_hot_decode, sparse_encode.

Other Dummifying: append_rows(), dummify(), dummify_multilabel(), effectcoding(), one_hot_decode(), remove_columns(), resample_imbalanced(), sparse_encode()

Examples

  x <- factor(sample(lvls <- c("dog", "cat", "mouse"), 10, TRUE), levels = lvls)
  one_hot_encode(x)
  one_hot_encode(x, ordered = TRUE)


stschn/deepANN documentation built on June 25, 2024, 7:27 a.m.