step_feature_hash {embed} | R Documentation |
Dummy Variables Creation via Feature Hashing
Description
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.
Usage
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")
)
Arguments
recipe |
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 |
role |
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. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
num_hash |
The number of resulting dummy variable columns. |
preserve |
Use |
columns |
A character vector for the selected columns. This is |
keep_original_cols |
A logical to keep the original variables in the
output. Defaults to |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
id |
A character string that is unique to this step to identify it. |
Details
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.
Value
An updated version of recipe
with the new step added to the
sequence of any existing operations.
Tidying
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
- terms
character, the selectors or variables selected
- id
character, id of this step
Case weights
The underlying operation does not allow for case weights.
References
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
See Also
recipes::step_dummy()
, recipes::step_zv()
Examples
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()
# 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()