Dummy Variables Creation via Feature HashingSource:
step_feature_hash() is being deprecated in favor of
textrecipes::step_dummy_hash(). This function creates a specification
of a recipe step that will convert nominal data (e.g. character or factors)
into one or more numeric binary columns using the levels of the original
step_feature_hash( recipe, ..., role = "predictor", trained = FALSE, num_hash = 2^6, preserve = deprecated(), columns = NULL, keep_original_cols = FALSE, skip = FALSE, id = rand_id("feature_hash") )
A recipe object. The step will be added to the sequence of operations for this recipe.
One or more selector functions to choose variables for this step. See
selections()for more details.
For model terms created by this step, what analysis role should they be assigned? By default, the new columns created by this step from the original variables will be used as predictors in a model.
A logical to indicate if the quantities for preprocessing have been estimated.
The number of resulting dummy variable columns.
keep_original_colsinstead to specify whether the selected column(s) should be retained in addition to the new dummy variables.
A character vector for the selected columns. This is
NULLuntil the step is trained by
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
bake()? While all operations are baked when
prep()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 = TRUEas it may affect the computations for subsequent operations.
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 any existing operations.
step_feature_hash() will create a set of binary dummy variables from a
factor or character variable. The values themselves are used to determine
which row that the dummy variable should be assigned (as opposed to having a
specific column that the value will map to).
Since this method does not rely on a pre-determined assignment of levels to columns, new factor levels can be added to the selected columns without issue. Missing values result in missing values for all of the hashed columns.
Note that the assignment of the levels to the hashing columns does not try to
maximize the allocation. It is likely that multiple levels of the column will
map to the same hashed columns (even with small data sets). Similarly, it is
likely that some columns will have all zeros. A zero-variance filter (via
recipes::step_zv()) is recommended for any recipe that uses hashed columns.
tidy() this step, a tibble with columns
(the columns that is selected) is returned.
Weinberger, K, A Dasgupta, J Langford, A Smola, and J Attenberg. 2009. "Feature Hashing for Large Scale Multitask Learning." In Proceedings of the 26th Annual International Conference on Machine Learning, 1113–20. ACM.
Kuhn and Johnson (2020) Feature Engineering and Selection: A Practical Approach for Predictive Models. CRC/Chapman Hall https://bookdown.org/max/FES/encoding-predictors-with-many-categories.html
data(grants, package = "modeldata") rec <- recipe(class ~ sponsor_code, data = grants_other) %>% step_feature_hash( sponsor_code, num_hash = 2^6, keep_original_cols = TRUE ) %>% prep() #> Warning: `step_feature_hash()` was deprecated in embed 0.2.0. #> ℹ Please use `textrecipes::step_dummy_hash()` instead. # How many of the 298 locations ended up in each hash column? results <- bake(rec, new_data = NULL, starts_with("sponsor_code")) %>% distinct() apply(results %>% select(-sponsor_code), 2, sum) %>% table() #> . #> 0 1 2 3 4 5 6 7 8 9 10 11 #> 1 4 6 13 12 7 10 3 4 1 2 1