在设计了一个基于Tidymodels配方的工作流,它经过调整然后适合一些训练数据,我不清楚哪些对象(适合的“工作流程”、“食谱”、..etc)应该保存到磁盘以用于预测新的生产中的数据。我知道我可以使用saveRDS()
/ readRDS()
、write_rds()
/read_rds()
或其他选项来实际保存/加载这些对象,但是哪些?
在一个干净的 R 环境中,我将收到新的原始数据,这些数据需要使用我在训练模型时使用的“配方”进行预处理。然后,我想在预处理后根据该数据进行预测。如果我打算像处理训练数据一样使用prep()
和bake()
函数来预处理新数据,那么我将最少需要它似乎开始prep()
工作的配方和原始训练数据。另外,我还需要拟合模型/工作流程来进行预测。所以看起来是三个对象。pull_workflow_prepped_recipe()
如果我将 SESSION 1 中的工作流对象保存到磁盘,那么我可以在 SESSION 2 中分别使用和从中提取配方和模型pull_workflow_fit()
。但prep()
似乎需要原始训练数据,我可以通过早期使用将其保留在工作流程中retain = TRUE
...但是在调用fit()
. 听我的呼救声!:)
所以,想象一下两个不同的 R 会话,第一个会话我进行所有的训练和模型构建,第二个会话是一些正在运行的生产应用程序,它使用从第一个会话中学到的东西。我在 SESSION1 底部的箭头处以及 SESSION 2 中的多个位置需要帮助。我使用 Tidymodels Get Started作为此示例的基础。
第一节
library(tidymodels)
library(nycflights13)
library(readr)
set.seed(123)
flight_data <-
head(flights, 500) %>%
mutate(
arr_delay = ifelse(arr_delay >= 30, "late", "on_time"),
arr_delay = factor(arr_delay),
date = as.Date(time_hour)
) %>%
inner_join(weather, by = c("origin", "time_hour")) %>%
select(dep_time, flight, origin, dest, air_time, distance, carrier, date, arr_delay, time_hour) %>%
na.omit() %>%
mutate_if(is.character, as.factor)
set.seed(555)
data_split <- initial_split(flight_data, prop = 3/4)
train_data <- training(data_split)
test_data <- testing(data_split)
flights_rec <-
recipe(arr_delay ~ ., data = train_data) %>%
update_role(flight, time_hour, new_role = "ID") %>%
step_date(date, features = c("dow", "month")) %>%
step_holiday(date, holidays = timeDate::listHolidays("US")) %>%
step_rm(date) %>%
step_dummy(all_nominal(), -all_outcomes()) %>%
step_zv(all_predictors())
lr_mod <-
logistic_reg() %>%
set_engine("glm")
flights_wflow <-
workflow() %>%
add_model(lr_mod) %>%
add_recipe(flights_rec)
flights_fit <-
flights_wflow %>%
fit(data = train_data)
predict(flights_fit, test_data)
### SAVE ONE OR MORE OBJECTS HERE FOR NEXT SESSION <------------
# What to save? workflow (pre or post fit()?), recipe, training data...etc.
write_rds(flights_wflow, "flights_wflow.rds") # Not fitted workflow
write_rds(flights_fit, "flights_fit.rds") # Fitted workflow
会议 2
### READ ONE OR MORE OBJECTS HERE FROM PRIOR SESSION <------------
flights_wflow <- read_rds("flights_wflow.rds")
flights_fit <- read_rds("flights_fit.rds")
# Acquire new data, do some basic transforms as before
new_flight_data <-
tail(flights, 500) %>%
mutate(
arr_delay = ifelse(arr_delay >= 30, "late", "on_time"),
arr_delay = factor(arr_delay),
date = as.Date(time_hour)
) %>%
inner_join(weather, by = c("origin", "time_hour")) %>%
select(dep_time, flight, origin, dest, air_time, distance, carrier, date, arr_delay, time_hour) %>%
na.omit() %>%
mutate_if(is.character, as.factor)
# Something here to preprocess the data with recipe as in SESSION 1 <----------
# new_flight_data_prep <- prep(??)
# new_flight_data_preprocessed <- bake(??)
# Predict new data
predict(flights_fit, new_data = new_flight_data_preprocessed)