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.

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.

Source

https://papers.nips.cc/paper/5782-character-level-convolutional-networks-for-text-classification.pdf

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

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)
#> # A tibble: 750 x 109 #> 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 100 more variables: seq1hot_essay0_1 <dbl>, #> # seq1hot_essay0_2 <dbl>, seq1hot_essay0_3 <dbl>, seq1hot_essay0_4 <dbl>, #> # seq1hot_essay0_5 <dbl>, seq1hot_essay0_6 <dbl>, seq1hot_essay0_7 <dbl>, #> # seq1hot_essay0_8 <dbl>, seq1hot_essay0_9 <dbl>, seq1hot_essay0_10 <dbl>, #> # seq1hot_essay0_11 <dbl>, seq1hot_essay0_12 <dbl>, seq1hot_essay0_13 <dbl>, #> # seq1hot_essay0_14 <dbl>, seq1hot_essay0_15 <dbl>, seq1hot_essay0_16 <dbl>, #> # seq1hot_essay0_17 <dbl>, seq1hot_essay0_18 <dbl>, seq1hot_essay0_19 <dbl>, #> # seq1hot_essay0_20 <dbl>, seq1hot_essay0_21 <dbl>, seq1hot_essay0_22 <dbl>, #> # seq1hot_essay0_23 <dbl>, seq1hot_essay0_24 <dbl>, seq1hot_essay0_25 <dbl>, #> # seq1hot_essay0_26 <dbl>, seq1hot_essay0_27 <dbl>, seq1hot_essay0_28 <dbl>, #> # seq1hot_essay0_29 <dbl>, seq1hot_essay0_30 <dbl>, seq1hot_essay0_31 <dbl>, #> # seq1hot_essay0_32 <dbl>, seq1hot_essay0_33 <dbl>, seq1hot_essay0_34 <dbl>, #> # seq1hot_essay0_35 <dbl>, seq1hot_essay0_36 <dbl>, seq1hot_essay0_37 <dbl>, #> # seq1hot_essay0_38 <dbl>, seq1hot_essay0_39 <dbl>, seq1hot_essay0_40 <dbl>, #> # seq1hot_essay0_41 <dbl>, seq1hot_essay0_42 <dbl>, seq1hot_essay0_43 <dbl>, #> # seq1hot_essay0_44 <dbl>, seq1hot_essay0_45 <dbl>, seq1hot_essay0_46 <dbl>, #> # seq1hot_essay0_47 <dbl>, seq1hot_essay0_48 <dbl>, seq1hot_essay0_49 <dbl>, #> # seq1hot_essay0_50 <dbl>, seq1hot_essay0_51 <dbl>, seq1hot_essay0_52 <dbl>, #> # seq1hot_essay0_53 <dbl>, seq1hot_essay0_54 <dbl>, seq1hot_essay0_55 <dbl>, #> # seq1hot_essay0_56 <dbl>, seq1hot_essay0_57 <dbl>, seq1hot_essay0_58 <dbl>, #> # seq1hot_essay0_59 <dbl>, seq1hot_essay0_60 <dbl>, seq1hot_essay0_61 <dbl>, #> # seq1hot_essay0_62 <dbl>, seq1hot_essay0_63 <dbl>, seq1hot_essay0_64 <dbl>, #> # seq1hot_essay0_65 <dbl>, seq1hot_essay0_66 <dbl>, seq1hot_essay0_67 <dbl>, #> # seq1hot_essay0_68 <dbl>, seq1hot_essay0_69 <dbl>, seq1hot_essay0_70 <dbl>, #> # seq1hot_essay0_71 <dbl>, seq1hot_essay0_72 <dbl>, seq1hot_essay0_73 <dbl>, #> # seq1hot_essay0_74 <dbl>, seq1hot_essay0_75 <dbl>, seq1hot_essay0_76 <dbl>, #> # seq1hot_essay0_77 <dbl>, seq1hot_essay0_78 <dbl>, seq1hot_essay0_79 <dbl>, #> # seq1hot_essay0_80 <dbl>, seq1hot_essay0_81 <dbl>, seq1hot_essay0_82 <dbl>, #> # seq1hot_essay0_83 <dbl>, seq1hot_essay0_84 <dbl>, seq1hot_essay0_85 <dbl>, #> # seq1hot_essay0_86 <dbl>, seq1hot_essay0_87 <dbl>, seq1hot_essay0_88 <dbl>, #> # seq1hot_essay0_89 <dbl>, seq1hot_essay0_90 <dbl>, seq1hot_essay0_91 <dbl>, #> # seq1hot_essay0_92 <dbl>, seq1hot_essay0_93 <dbl>, seq1hot_essay0_94 <dbl>, #> # seq1hot_essay0_95 <dbl>, seq1hot_essay0_96 <dbl>, seq1hot_essay0_97 <dbl>, #> # seq1hot_essay0_98 <dbl>, seq1hot_essay0_99 <dbl>, seq1hot_essay0_100 <dbl>
tidy(okc_rec, number = 1)
#> # A tibble: 1 x 3 #> terms integer_key id #> <chr> <chr> <chr> #> 1 essay0 <NA> sequence_onehot_aq6LC
tidy(okc_obj, number = 1)
#> # A tibble: 26 x 3 #> terms integer_key id #> <chr> <chr> <chr> #> 1 essay0 a sequence_onehot_aq6LC #> 2 essay0 b sequence_onehot_aq6LC #> 3 essay0 c sequence_onehot_aq6LC #> 4 essay0 d sequence_onehot_aq6LC #> 5 essay0 e sequence_onehot_aq6LC #> 6 essay0 f sequence_onehot_aq6LC #> 7 essay0 g sequence_onehot_aq6LC #> 8 essay0 h sequence_onehot_aq6LC #> 9 essay0 i sequence_onehot_aq6LC #> 10 essay0 j sequence_onehot_aq6LC #> # … with 16 more rows