recipes is gaining an exported recipes_map_cols() helper (tidymodels/recipes#1543, tidymodels/recipes#1556, tidymodels/recipes#1558) for use in bake() methods of steps that transform columns in place. Assigning one column at a time in a for loop (new_data[[col_name]] <- ...) copies the whole data frame on each iteration, which is quadratic in the number of columns. recipes_map_cols() computes all the new columns first and assigns them in bulk. In recipes, baking 8,000 factor columns through step_other() dropped from 5.14s to 1.26s.
The following bake() methods in embed use the slow pattern and could be converted:
step_lencode() (R/lencode.R:296)
step_lencode_glm() (R/lencode_glm.R:209)
step_lencode_bayes() (R/lencode_bayes.R:272)
step_lencode_mixed() (R/lencode_mixed.R:259)
step_collapse_stringdist() (R/collapse_stringdist.R:203)
step_discretize_cart() (R/discretize_cart.R:270)
step_discretize_xgb() (R/discretize_xgb.R:545)
The two discretize_* steps are the worst of these: they take a full binned_data <- new_data copy and then assign into it once per column, so they pay the copy twice per iteration.
Separately, step_collapse_cart() (R/collapse_cart.R:165) loops per column too, but each iteration calls convert_keys(), which rebuilds the data frame via a join. recipes_map_cols() doesn't drop in directly there, though it is quadratic in its own way and worth a look.
This is blocked on the recipes release that exports recipes_map_cols().
recipes is gaining an exported
recipes_map_cols()helper (tidymodels/recipes#1543, tidymodels/recipes#1556, tidymodels/recipes#1558) for use inbake()methods of steps that transform columns in place. Assigning one column at a time in aforloop (new_data[[col_name]] <- ...) copies the whole data frame on each iteration, which is quadratic in the number of columns.recipes_map_cols()computes all the new columns first and assigns them in bulk. In recipes, baking 8,000 factor columns throughstep_other()dropped from 5.14s to 1.26s.The following
bake()methods in embed use the slow pattern and could be converted:step_lencode()(R/lencode.R:296)step_lencode_glm()(R/lencode_glm.R:209)step_lencode_bayes()(R/lencode_bayes.R:272)step_lencode_mixed()(R/lencode_mixed.R:259)step_collapse_stringdist()(R/collapse_stringdist.R:203)step_discretize_cart()(R/discretize_cart.R:270)step_discretize_xgb()(R/discretize_xgb.R:545)The two
discretize_*steps are the worst of these: they take a fullbinned_data <- new_datacopy and then assign into it once per column, so they pay the copy twice per iteration.Separately,
step_collapse_cart()(R/collapse_cart.R:165) loops per column too, but each iteration callsconvert_keys(), which rebuilds the data frame via a join.recipes_map_cols()doesn't drop in directly there, though it is quadratic in its own way and worth a look.This is blocked on the recipes release that exports
recipes_map_cols().