How to hot encode/generate dummy columns using sparklyr

Viewed 114

I know there are number of questions similar to this here but 1) most of the solutions rely on deprecated functions like ml_create_dummy_variables and 2) other solutions are incomplete.

Is there a function or an approach to easily hot encode a categorical variable into multiple dummy variables in sparklyr?

This post asks for a solution in SparkR, incidentally a sparklyr solution is given that only works when the categories are unique in a given column, which renders its pointless.

This solution, results in a single dummy instead of a dummy for each category (grabs the first category). This is also the solution I stumbled onto (based on this post), which does not cut it:

iris_sdf <- copy_to(sc, iris, overwrite = TRUE)

iris_sdf %>%
  ft_string_indexer(input_col = "Species", output_col = "species_num") %>%
  mutate(cat_num = species_num + 1) %>%
  ft_one_hot_encoder("species_num", "species_dum") %>%
  ft_vector_assembler(c("species_dum")) 

I'm looking for a solution that will take Species from the iris dataset and generate three columns -one for each category in Species (virginica, setosa, and versicolor). Using R, fastDummies package has what I need, but I'm left wondering how to achieve similar functionality in sparklyr.

Again, I'll note that ml_create_dummy_variables (suggested by this post) produced the following error:

Error in ml_create_dummy_variables(., "species_num", "species_dum") : Error in ml_create_dummy_variables(., "species_num", "species_dum") : 
  could not find function "ml_create_dummy_variables"

Note: I'm using sparklyr_1.3.1

0 Answers
Related