Skip to content

Use recipes_map_cols() in bake() methods that transform columns in place #278

Description

@EmilHvitfeldt

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().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    upkeepmaintenance, infrastructure, and similar

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions