step_texthash: Term frequency of tokens

Description Usage Arguments Details Value References See Also Examples

Description

'step_texthash' creates a *specification* of a recipe step that will convert a list of tokens into multiple variables using the hashing trick.

Usage

1
2
3
4
5
6
step_texthash(recipe, ..., role = NA, trained = FALSE,
  columns = NULL, signed = TRUE, num_terms = 1024, prefix = "hash",
  skip = FALSE, id = rand_id("texthash"))

## S3 method for class 'step_texthash'
tidy(x, ...)

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 'step_texthash', this indicates the variables to be encoded into a list column. See [recipes::selections()] for more details. For the 'tidy' method, these are not currently used.

role

Not used by this step since no new variables are created.

trained

A logical to indicate if the recipe has been baked.

columns

A list of tibble results that define the encoding. This is 'NULL' until the step is trained by [recipes::prep.recipe()].

signed

A logical, indicating whether to use a signed hash-function to reduce collisions when hashing. Defaults to TRUE.

num_terms

An integer, the number of variables to output. Defaults to 1024.

prefix

A character string that will be the prefix to the resulting new variables. See notes below.

skip

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

id

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

x

A 'step_texthash' object.

Details

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.

The argument '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 'prefix', then the name of the variable, followed by the tokens all seperated by '-'. The variable names are padded with zeros. For example, if 'num_terms < 10', their names will be 'hash1' - 'hash9'. If 'num_terms = 101', their names will be 'hash001' - 'hash101'.

Value

An updated version of 'recipe' with the new step added to the sequence of existing steps (if any).

References

Kilian Weinberger; Anirban Dasgupta; John Langford; Alex Smola; Josh Attenberg (2009).

See Also

[step_tf()] [step_tfidf()] [step_tokenize()]

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
library(recipes)

data(okc_text)

okc_rec <- recipe(~ ., data = okc_text) %>%
  step_tokenize(essay0) %>%
  step_tokenfilter(essay0, max_tokens = 10) %>%
  step_texthash(essay0)
  
okc_obj <- okc_rec %>%
  prep(training = okc_text, retain = TRUE)
  
bake(okc_obj, okc_text)

tidy(okc_rec, number = 2)
tidy(okc_obj, number = 2)

textrecipes documentation built on May 2, 2019, 1:27 p.m.