Learn R Programming

textrecipes (version 0.3.0)

step_sequence_onehot: Generate the basic set of text features

Description

step_sequence_onehot creates a specification of a recipe step that will take a string and do one hot encoding for each character by position.

Usage

step_sequence_onehot(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  columns = NULL,
  string_length = 100,
  integer_key = letters,
  prefix = "seq1hot",
  skip = FALSE,
  id = rand_id("sequence_onehot")
)

# S3 method for step_sequence_onehot 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_sequence_onehot, 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().

string_length

A numeric, number of characters to keep before discarding. Defaults to 100.

integer_key

A character vector, characters to be mapped to integers. Characters not in the integer_key will be encoded as 0. Defaults to letters.

prefix

A prefix for generated column names, default to "seq1hot".

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

Value

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

Details

The string will be capped by the string_length argument, strings shorter then string_length will be padded with empty characters. The encoding will assign a integer to each character in the integer_key, and will encode accordingly. Characters not in the integer_key will be encoded as 0.

See Also

Other character to numeric steps: step_lda(), step_textfeature()

Examples

Run this code
# NOT RUN {
library(recipes)
library(modeldata)
data(okc_text)

okc_rec <- recipe(~ ., data = okc_text) %>%
  step_sequence_onehot(essay0) 
  
okc_obj <- okc_rec %>%
  prep()

juice(okc_obj)
  
tidy(okc_rec, number = 1)
tidy(okc_obj, number = 1)

# }

Run the code above in your browser using DataLab