step_feature_hash: Dummy Variables Creation via Feature Hashing

View source: R/feature_hash.R

step_feature_hashR Documentation

Dummy Variables Creation via Feature Hashing



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 data.


  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.


Use keep_original_cols instead 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 NULL until the step is trained by recipes::prep().


A logical to keep the original variables in the output. Defaults to FALSE.


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 = TRUE as it may affect the computations for subsequent operations.


A character string that is unique to this step to identify it.


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.


An updated version of recipe with the new step added to the sequence of any existing operations.


When you tidy() this step, a tibble with columns terms (the columns that is selected) is returned.

Case weights

The underlying operation does not allow for case weights.


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

See Also

recipes::step_dummy(), recipes::step_zv()


data(grants, package = "modeldata")
rec <-
  recipe(class ~ sponsor_code, data = grants_other) %>%
    num_hash = 2^6, keep_original_cols = TRUE
  ) %>%

# How many of the 298 locations ended up in each hash column?
results <-
  bake(rec, new_data = NULL, starts_with("sponsor_code")) %>%

apply(results %>% select(-sponsor_code), 2, sum) %>% table()

embed documentation built on Nov. 2, 2023, 6:20 p.m.