step_tfidf | R Documentation |
step_tfidf()
creates a specification of a recipe step that will convert a
token
variable 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",
keep_original_cols = FALSE,
skip = FALSE,
id = rand_id("tfidf")
)
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variables are affected by the step. See |
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 quantities for preprocessing have been estimated. |
columns |
A character string of variable names that will
be populated (eventually) by the |
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
|
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. |
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. |
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 variable names are padded with zeros. For example if
prefix = "hash"
, and if num_terms < 10
, their names will be
hash1
- hash9
. If num_terms = 101
, their names will be
hash001
- hash101
.
An updated version of recipe
with the new step added
to the sequence of existing steps (if any).
When you tidy()
this step, a tibble with columns terms
(the selectors or variables selected), token
(name of the tokens),
weight
(the calculated IDF weight) is returned.
The underlying operation does not allow for case weights.
step_tokenize()
to turn characters into tokens
Other Steps for Numeric Variables From Tokens:
step_lda()
,
step_texthash()
,
step_tf()
,
step_word_embeddings()
library(recipes)
library(modeldata)
data(tate_text)
tate_rec <- recipe(~., data = tate_text) %>%
step_tokenize(medium) %>%
step_tfidf(medium)
tate_obj <- tate_rec %>%
prep()
bake(tate_obj, tate_text)
tidy(tate_rec, number = 2)
tidy(tate_obj, number = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.