Feature Hashing of TokensSource:
step_texthash creates a specification of a recipe step that will convert
token variable into multiple numeric variables using the
step_texthash( recipe, ..., role = "predictor", trained = FALSE, columns = NULL, signed = TRUE, num_terms = 1024L, prefix = "texthash", keep_original_cols = FALSE, skip = FALSE, id = rand_id("texthash") )
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 to reduce collisions when hashing. Defaults to TRUE.
An integer, the number of variables to output. Defaults to 1024.
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 tokens and using the hash values as feature indices. This allows for a low memory representation of the text. 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) and
value (number of terms).
step_tokenize() to turn characters into
step_text_normalization() to perform text normalization.
Other Steps for Numeric Variables From Tokens:
library(recipes) library(modeldata) data(tate_text) tate_rec <- recipe(~., data = tate_text) %>% step_tokenize(medium) %>% step_tokenfilter(medium, max_tokens = 10) %>% step_texthash(medium) tate_obj <- tate_rec %>% prep() bake(tate_obj, tate_text) #> # A tibble: 4,284 × 1,028 #> texth…¹ texth…² texth…³ texth…⁴ texth…⁵ texth…⁶ texth…⁷ texth…⁸ texth…⁹ #> <int> <int> <int> <int> <int> <int> <int> <int> <int> #> 1 0 0 0 0 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 0 0 0 0 0 0 0 0 #> 5 0 0 0 0 0 0 0 0 0 #> 6 0 0 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 0 #> 9 0 0 0 0 0 0 0 0 0 #> 10 0 0 0 0 0 0 0 0 0 #> # … with 4,274 more rows, 1,019 more variables: #> # texthash_medium_0010 <int>, texthash_medium_0011 <int>, #> # texthash_medium_0012 <int>, texthash_medium_0013 <int>, #> # texthash_medium_0014 <int>, texthash_medium_0015 <int>, #> # texthash_medium_0016 <int>, texthash_medium_0017 <int>, #> # texthash_medium_0018 <int>, texthash_medium_0019 <int>, #> # texthash_medium_0020 <int>, texthash_medium_0021 <int>, … tidy(tate_rec, number = 3) #> # A tibble: 1 × 4 #> terms value length id #> <chr> <lgl> <int> <chr> #> 1 medium NA NA texthash_E2J5E tidy(tate_obj, number = 3) #> # A tibble: 1 × 4 #> terms value length id #> <chr> <lgl> <int> <chr> #> 1 medium TRUE 1024 texthash_E2J5E