step_tfidf creates a specification of a recipe step that will convert a tokenlist into multiple variables containing the term frequency-inverse document frequency of tokens.

step_tfidf(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  columns = NULL,
  vocabulary = NULL,
  res = NULL,
  smooth_idf = TRUE,
  norm = "l1",
  sublinear_tf = FALSE,
  prefix = "tfidf",
  skip = FALSE,
  id = rand_id("tfidf")
)

# S3 method for step_tfidf
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_tfidf, this indicates the variables to be encoded into a tokenlist. See recipes::selections() for more details. For the tidy method, these are not currently used.

role

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.

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().

vocabulary

A character vector of strings to be considered.

res

The words that will be used to calculate the term frequency will be stored here once this preprocessing step has be trained by prep.recipe().

smooth_idf

TRUE smooth IDF weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. This prevents division by zero.

norm

A character, defines the type of normalization to apply to term vectors. "l1" by default, i.e., scale by the number of words in the document. Must be one of c("l1", "l2", "none").

sublinear_tf

A logical, apply sublinear term-frequency scaling, i.e., replace the term frequency with 1 + log(TF). Defaults to FALSE.

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_tfidf object.

Value

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

Details

It is strongly advised to use step_tokenfilter before using step_tfidf to limit the number of variables created; otherwise you may run into memory issues. A good strategy is to start with a low token count and increase depending on how much RAM you want to use.

Term frequency-inverse document frequency is the product of two statistics: the term frequency (TF) and the inverse document frequency (IDF).

Term frequency measures how many times each token appears in each observation.

Inverse document frequency is a measure of how informative a word is, e.g., how common or rare the word is across all the observations. If a word appears in all the observations it might not give that much insight, but if it only appears in some it might help differentiate between observations.

The IDF is defined as follows: idf = log(1 + (# documents in the corpus) / (# documents where the term appears))

The new components will have names that begin with prefix, then the name of the variable, followed by the tokens all separated by -. The new variables will be created alphabetically according to token.

See also

step_tokenize() to turn character into tokenlist.

Other tokenlist to numeric steps: step_texthash(), step_tf(), step_word_embeddings()

Examples

# \donttest{ library(recipes) library(modeldata) data(okc_text) okc_rec <- recipe(~ ., data = okc_text) %>% step_tokenize(essay0) %>% step_tfidf(essay0) okc_obj <- okc_rec %>% prep() bake(okc_obj, okc_text)
#> # A tibble: 750 x 9,265 #> essay1 essay2 essay3 essay4 essay5 essay6 essay7 essay8 essay9 #> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> #> 1 "i ju… "writ… "that… "musi… "frie… "roma… "usua… "i ha… "you'… #> 2 "eati… "pick… "i lo… "non-… "dese… "ever… "maki… "this… "you'… #> 3 "rais… "proc… "my s… "sush… "my c… "movi… "at h… "on m… "you … #> 4 "i ju… "i've… "my f… "nove… "fami… "how … "havi… "is t… "you'… #> 5 "i'm … "bein… "that… "book… "- fr… "the … "prob… "uhh.… "you … #> 6 "i'm … "i'm … "the … "book… "guit… "a li… "hang… "i'm … "if y… #> 7 "well… "well… "eith… "i do… "1) m… "the … "out … "i ow… "you … #> 8 "i wo… "well… "dimp… "book… "1-la… "sex<… "depe… "i li… "you … #> 9 "phot… "eati… "my g… "donn… "sush… "love… "drin… "hm..… "." #> 10 "occu… "livi… "long… "film… "yuzu… "menu… "ofte… "but … "you … #> # … with 740 more rows, and 9,256 more variables: tfidf_essay0______ <dbl>, #> # tfidf_essay0_______ <dbl>, tfidf_essay0________________________ <dbl>, #> # tfidf_essay0_____coming <dbl>, tfidf_essay0__blank <dbl>, #> # tfidf_essay0__updates_ <dbl>, tfidf_essay0_0 <dbl>, tfidf_essay0_01 <dbl>, #> # tfidf_essay0_0aare <dbl>, tfidf_essay0_0abilly <dbl>, #> # tfidf_essay0_0aboondocks <dbl>, tfidf_essay0_0abrothers <dbl>, #> # tfidf_essay0_0aconfidential <dbl>, tfidf_essay0_0aconversation <dbl>, #> # tfidf_essay0_0adebates <dbl>, tfidf_essay0_0afly <dbl>, #> # tfidf_essay0_0afriends <dbl>, tfidf_essay0_0agiants <dbl>, #> # tfidf_essay0_0ahop <dbl>, tfidf_essay0_0ahunters <dbl>, #> # tfidf_essay0_0aking <dbl>, tfidf_essay0_0amovies <dbl>, #> # tfidf_essay0_0amusic <dbl>, tfidf_essay0_0aparties <dbl>, #> # tfidf_essay0_0arailroading <dbl>, tfidf_essay0_0ashows <dbl>, #> # tfidf_essay0_0atrips <dbl>, tfidf_essay0_0aweapons <dbl>, #> # tfidf_essay0_1 <dbl>, tfidf_essay0_10 <dbl>, `tfidf_essay0_10,000` <dbl>, #> # tfidf_essay0_100 <dbl>, tfidf_essay0_1000 <dbl>, tfidf_essay0_105 <dbl>, #> # tfidf_essay0_11 <dbl>, tfidf_essay0_110 <dbl>, tfidf_essay0_1193 <dbl>, #> # tfidf_essay0_12 <dbl>, tfidf_essay0_125 <dbl>, tfidf_essay0_12s <dbl>, #> # tfidf_essay0_13 <dbl>, tfidf_essay0_1337 <dbl>, tfidf_essay0_14 <dbl>, #> # tfidf_essay0_1400 <dbl>, tfidf_essay0_15 <dbl>, tfidf_essay0_150 <dbl>, #> # tfidf_essay0_16 <dbl>, tfidf_essay0_16th <dbl>, tfidf_essay0_17 <dbl>, #> # tfidf_essay0_18 <dbl>, tfidf_essay0_180 <dbl>, #> # tfidf_essay0_1886866717 <dbl>, tfidf_essay0_19 <dbl>, #> # tfidf_essay0_1904 <dbl>, tfidf_essay0_1964 <dbl>, tfidf_essay0_1966 <dbl>, #> # tfidf_essay0_1982 <dbl>, tfidf_essay0_1988 <dbl>, tfidf_essay0_1991 <dbl>, #> # tfidf_essay0_1992 <dbl>, tfidf_essay0_1996 <dbl>, tfidf_essay0_1998 <dbl>, #> # tfidf_essay0_1st <dbl>, tfidf_essay0_2 <dbl>, tfidf_essay0_20 <dbl>, #> # tfidf_essay0_200 <dbl>, tfidf_essay0_2000s <dbl>, tfidf_essay0_2001 <dbl>, #> # tfidf_essay0_2005 <dbl>, tfidf_essay0_2007 <dbl>, tfidf_essay0_2008 <dbl>, #> # tfidf_essay0_2009 <dbl>, tfidf_essay0_2010 <dbl>, tfidf_essay0_2011 <dbl>, #> # tfidf_essay0_2012 <dbl>, tfidf_essay0_202 <dbl>, tfidf_essay0_2021 <dbl>, #> # tfidf_essay0_20s <dbl>, tfidf_essay0_20snot <dbl>, tfidf_essay0_20th <dbl>, #> # tfidf_essay0_21 <dbl>, tfidf_essay0_22 <dbl>, tfidf_essay0_23 <dbl>, #> # tfidf_essay0_23yo <dbl>, tfidf_essay0_24 <dbl>, tfidf_essay0_245lb <dbl>, #> # tfidf_essay0_25 <dbl>, tfidf_essay0_250 <dbl>, tfidf_essay0_26 <dbl>, #> # tfidf_essay0_27 <dbl>, tfidf_essay0_27ish <dbl>, tfidf_essay0_27s <dbl>, #> # tfidf_essay0_28 <dbl>, tfidf_essay0_28th <dbl>, tfidf_essay0_29 <dbl>, #> # tfidf_essay0_2cedd <dbl>, tfidf_essay0_2cehksxolna <dbl>, #> # tfidf_essay0_2fbowling <dbl>, tfidf_essay0_2fdarts <dbl>, #> # tfidf_essay0_2fodd <dbl>, …
tidy(okc_rec, number = 2)
#> # A tibble: 1 x 2 #> terms id #> <chr> <chr> #> 1 essay0 tfidf_6Jz2e
tidy(okc_obj, number = 2)
#> # A tibble: 1 x 2 #> terms id #> <quos> <chr> #> 1 essay0 tfidf_6Jz2e
# }