Skip to content

Commit fe3f02b

Browse files
authored
Merge pull request #20 from davidrsch/workflows-examples
Workflows examples
2 parents 3e76530 + a184868 commit fe3f02b

File tree

8 files changed

+824
-1
lines changed

8 files changed

+824
-1
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Imports:
1818
tibble,
1919
purrr,
2020
dplyr,
21-
cli
21+
cli,
22+
recipes
2223
Suggests:
2324
testthat (>= 3.0.0),
2425
modeldata,

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(bake,step_collapse)
4+
S3method(prep,step_collapse)
5+
S3method(print,step_collapse)
36
export(compile_keras_grid)
47
export(create_keras_functional_spec)
58
export(create_keras_sequential_spec)
@@ -24,6 +27,7 @@ export(register_keras_loss)
2427
export(register_keras_metric)
2528
export(register_keras_optimizer)
2629
export(remove_keras_spec)
30+
export(step_collapse)
2731
importFrom(cli,cli_alert_danger)
2832
importFrom(cli,cli_alert_info)
2933
importFrom(cli,cli_alert_success)
@@ -37,6 +41,8 @@ importFrom(dplyr,filter)
3741
importFrom(dplyr,select)
3842
importFrom(keras3,to_categorical)
3943
importFrom(parsnip,update_dot_check)
44+
importFrom(recipes,bake)
45+
importFrom(recipes,prep)
4046
importFrom(rlang,arg_match)
4147
importFrom(rlang,dots_list)
4248
importFrom(rlang,enquos)

R/register_core_model.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ register_core_model <- function(model_name, mode) {
2121
parsnip::set_model_mode(model_name, mode)
2222
parsnip::set_model_engine(model_name, mode, "keras")
2323
parsnip::set_dependency(model_name, "keras", "keras3")
24+
parsnip::set_dependency(model_name, "keras", "kerasnip")
2425

2526
parsnip::set_encoding(
2627
model = model_name,

R/step_collapse.R

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#' Collapse Predictors into a single list-column
2+
#'
3+
#' `step_collapse()` creates a a *specification* of a recipe step that will
4+
#' convert a group of predictors into a single list-column. This is useful
5+
#' for custom models that need the predictors in a different format.
6+
#'
7+
#' @param recipe A recipe object. The step will be added to the sequence of
8+
#' operations for this recipe.
9+
#' @param ... One or more selector functions to choose which variables are
10+
#' affected by the step. See `[selections()]` for more details. For the `tidy`
11+
#' method, these are not currently used.
12+
#' @param role For model terms created by this step, what analysis role should
13+
#' they be assigned?. By default, the new columns are used as predictors.
14+
#' @param trained A logical to indicate if the quantities for preprocessing
15+
#' have been estimated.
16+
#' @param columns A character string of the selected variable names. This is
17+
#' `NULL` until the step is trained by `[prep.recipe()]`.
18+
#' @param new_col A character string for the name of the new list-column. The
19+
#' default is "predictor_matrix".
20+
#' @param skip A logical. Should the step be skipped when the recipe is
21+
#' baked by `[bake.recipe()]`? While all operations are baked when `prep` is run,
22+
#' skipping when `bake` is run may be other times when it is desirable to
23+
#' skip a processing step.
24+
#' @param id A character string that is unique to this step to identify it.
25+
#'
26+
#' @return An updated version of `recipe` with the new step added to the
27+
#' sequence of existing steps (if any). For the `tidy` method, a tibble with
28+
#' columns `terms` which is the columns that are affected and `value` which is
29+
#' the type of collapse.
30+
#'
31+
#' @examples
32+
#' library(recipes)
33+
#'
34+
#' # 2 predictors
35+
#' dat <- data.frame(
36+
#' x1 = 1:10,
37+
#' x2 = 11:20,
38+
#' y = 1:10
39+
#' )
40+
#'
41+
#' rec <- recipe(y ~ ., data = dat) %>%
42+
#' step_collapse(x1, x2, new_col = "pred") %>%
43+
#' prep()
44+
#'
45+
#' bake(rec, new_data = NULL)
46+
#' @importFrom recipes prep bake
47+
#' @export
48+
step_collapse <- function(
49+
recipe,
50+
...,
51+
role = "predictor",
52+
trained = FALSE,
53+
columns = NULL,
54+
new_col = "predictor_matrix",
55+
skip = FALSE,
56+
id = recipes::rand_id("collapse")
57+
) {
58+
recipes::add_step(
59+
recipe,
60+
step_collapse_new(
61+
terms = enquos(...),
62+
role = role,
63+
trained = trained,
64+
columns = columns,
65+
new_col = new_col,
66+
skip = skip,
67+
id = id
68+
)
69+
)
70+
}
71+
72+
step_collapse_new <- function(
73+
terms,
74+
role,
75+
trained,
76+
columns,
77+
new_col,
78+
skip,
79+
id
80+
) {
81+
recipes::step(
82+
subclass = "collapse",
83+
terms = terms,
84+
role = role,
85+
trained = trained,
86+
columns = columns,
87+
new_col = new_col,
88+
skip = skip,
89+
id = id
90+
)
91+
}
92+
93+
#' @export
94+
prep.step_collapse <- function(x, training, info = NULL, ...) {
95+
col_names <- recipes::recipes_eval_select(x$terms, training, info)
96+
97+
step_collapse_new(
98+
terms = x$terms,
99+
role = x$role,
100+
trained = TRUE,
101+
columns = col_names,
102+
new_col = x$new_col,
103+
skip = x$skip,
104+
id = x$id
105+
)
106+
}
107+
108+
#' @export
109+
bake.step_collapse <- function(object, new_data, ...) {
110+
recipes::check_new_data(object$columns, object, new_data)
111+
112+
rows_list <- apply(
113+
new_data[, object$columns, drop = FALSE],
114+
1,
115+
function(row) matrix(row, nrow = 1),
116+
simplify = FALSE
117+
)
118+
119+
new_data[[object$new_col]] <- rows_list
120+
121+
# drop original predictor columns
122+
new_data <- new_data[, setdiff(names(new_data), object$columns), drop = FALSE]
123+
124+
new_data
125+
}
126+
127+
#' @export
128+
print.step_collapse <- function(x, ...) {
129+
if (is.null(x$columns)) {
130+
cat("Collapse predictors into list-column (unprepped)\\n")
131+
} else {
132+
cat(
133+
"Collapse predictors into list-column:",
134+
paste(x$columns, collapse = ", "),
135+
" -> ",
136+
x$new_col,
137+
"\\n"
138+
)
139+
}
140+
invisible(x)
141+
}

_pkgdown.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ guides:
1919
contents:
2020
- sequential_model
2121
- functional_api
22+
- workflows_sequential
23+
- workflows_functional
2224

2325
# examples:
2426

@@ -54,6 +56,12 @@ reference:
5456
- extract_keras_model
5557
- keras_evaluate
5658

59+
- title: "Custom recipe steps"
60+
desc: >
61+
Custom stpes for recipe which uses kerasnip models specifications
62+
contents:
63+
- step_collapse
64+
5765
development:
5866
mode: auto
5967

@@ -75,6 +83,11 @@ navbar:
7583
href: articles/sequential_model.html
7684
- text: "Functional API"
7785
href: articles/functional_api.html
86+
- text: "Workflows"
87+
- text: "Sequential Model"
88+
href: articles/workflows_sequential.html
89+
- text: "Functional API"
90+
href: articles/workflows_functional.html
7891
github:
7992
icon: fa-github
8093
href: https://github.com/davidrsch/kerasnip

man/step_collapse.Rd

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)