Indicator Variables via Feature HashingSource:
step_dummy_hash creates a specification of a recipe step that will
convert factors or character columns into a series of binary (or signed
binary) indicator columns.
step_dummy_hash( recipe, ..., role = "predictor", trained = FALSE, columns = NULL, signed = TRUE, num_terms = 32L, collapse = FALSE, prefix = "dummyhash", keep_original_cols = FALSE, skip = FALSE, id = rand_id("dummy_hash") )
A recipe object. The step will be added to the sequence of operations for this recipe.
One or more selector functions to choose which variables are affected by the step. See
recipes::selections()for more details.
For model terms created by this step, what analysis role should they be assigned?. By default, the function assumes that the new columns created by the original variables will be used as predictors in a model.
A logical to indicate if the quantities for preprocessing have been estimated.
A character string of variable names that will be populated (eventually) by the
termsargument. This is
NULLuntil the step is trained by
A logical, indicating whether to use a signed hash-function (generating values of -1, 0, or 1), to reduce collisions when hashing. Defaults to TRUE.
An integer, the number of variables to output. Defaults to 32.
A logical; should all of the selected columns be collapsed into a single column to create a single set of hashed features?
A character string that will be the prefix to the resulting new variables. See notes below.
A logical to keep the original variables in the output. Defaults to
A logical. Should the step be skipped when the recipe is baked by
recipes::bake.recipe()? While all operations are baked when
recipes::prep.recipe()is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using
skip = FALSE.
A character string that is unique to this step to identify it.
An updated version of
recipe with the new step added
to the sequence of existing steps (if any).
Feature hashing, or the hashing trick, is a transformation of a text variable into a new set of numerical variables. This is done by applying a hashing function over the values of the factor levels and using the hash values as feature indices. This allows for a low memory representation of the data and can be very helpful when a qualitative predictor has many levels or is expected to have new levels during prediction. This implementation is done using the MurmurHash3 method.
num_terms controls the number of indices that the hashing
function will map to. This is the tuning parameter for this transformation.
Since the hashing function can map two different tokens to the same index,
will a higher value of
num_terms result in a lower chance of collision.
The new components will have names that begin with
the name of the variable, followed by the tokens all separated by
-. The variable names are padded with zeros. For example if
prefix = "hash", and if
num_terms < 10, their names will be
num_terms = 101, their names will be
tidy() this step, a tibble with columns
(the selectors or variables selected),
value (whether a signed hashing was
num_terms (number of terms), and
collapse (where columns
Kilian Weinberger; Anirban Dasgupta; John Langford; Alex Smola; Josh Attenberg (2009).
Kuhn and Johnson (2019), Chapter 7, https://bookdown.org/max/FES/encoding-predictors-with-many-categories.html
Other Steps for Numeric Variables From Characters:
library(recipes) library(modeldata) data(grants) grants_rec <- recipe(~sponsor_code, data = grants_other) %>% step_dummy_hash(sponsor_code) grants_obj <- grants_rec %>% prep() #> as(<dgTMatrix>, "dgCMatrix") is deprecated since Matrix 1.5-0; do as(., "CsparseMatrix") instead bake(grants_obj, grants_test) #> # A tibble: 518 × 32 #> dummy…¹ dummy…² dummy…³ dummy…⁴ dummy…⁵ dummy…⁶ dummy…⁷ dummy…⁸ dummy…⁹ #> <int> <int> <int> <int> <int> <int> <int> <int> <int> #> 1 0 0 0 1 0 0 0 0 0 #> 2 0 0 0 0 0 0 0 0 0 #> 3 0 0 0 0 0 0 0 0 0 #> 4 0 1 0 0 0 0 0 0 0 #> 5 0 0 0 0 0 0 0 0 0 #> 6 0 1 0 0 0 0 0 0 0 #> 7 0 0 0 0 0 0 0 0 0 #> 8 0 0 0 0 0 0 0 0 -1 #> 9 0 0 0 0 0 0 0 0 0 #> 10 0 1 0 0 0 0 0 0 0 #> # … with 508 more rows, 23 more variables: #> # dummyhash_sponsor_code_10 <int>, dummyhash_sponsor_code_11 <int>, #> # dummyhash_sponsor_code_12 <int>, dummyhash_sponsor_code_13 <int>, #> # dummyhash_sponsor_code_14 <int>, dummyhash_sponsor_code_15 <int>, #> # dummyhash_sponsor_code_16 <int>, dummyhash_sponsor_code_17 <int>, #> # dummyhash_sponsor_code_18 <int>, dummyhash_sponsor_code_19 <int>, #> # dummyhash_sponsor_code_20 <int>, dummyhash_sponsor_code_21 <int>, … tidy(grants_rec, number = 1) #> # A tibble: 1 × 5 #> terms value num_terms collapse id #> <chr> <lgl> <int> <lgl> <chr> #> 1 sponsor_code NA NA NA dummy_hash_OcDQ1 tidy(grants_obj, number = 1) #> # A tibble: 1 × 5 #> terms value num_terms collapse id #> <chr> <lgl> <int> <lgl> <chr> #> 1 sponsor_code TRUE 32 FALSE dummy_hash_OcDQ1