Export a recipe to SQL, pandas and dplyr
Take your cleaning out of the app as code, and choose Replay or Refit for how learned values behave.
You will be able to
- Export a recipe to SQL, pandas or dplyr
- Tell Replay from Refit
- Know when to use scikit-learn or tidymodels recipes instead
Read first
A recipe is code you haven't looked at yet
ReadEvery recipe compiles to code. Open a recipe in Prepare (⌘3) and use Code in its toolbar.
| Target | What you get |
|---|---|
| SQL | The DuckDB SQL the app runs |
| pandas | Python with pandas, same output |
| dplyr | R with dplyr, same output |
| scikit-learn | A Pipeline to fit on training data |
| recipes | A tidymodels recipe to prep on training data |
Copy takes the text, and Open as script creates a script in your project.
Replay or Refit?
PredictPredict first
A step fills missing income with a learned median. You want the exported code to reproduce the app's output exactly. Which mode do you choose?
Reveal
The answer: Replay
Replay writes the learned values into the code, so it reproduces the app. Refit recomputes them from whatever data the code runs on, which is what you want for next month's file. Neither is more correct; they answer different needs.
See the difference
In the appOpen Code on a recipe that has a Fill missing values step, and choose pandas. Toggle between Replay and Refit and find the median in the code.
Under Replay you see a literal number. Under Refit you see a calculation.
The scikit-learn and recipes targets are always Refit, because fitting on training data is their whole purpose.
What the two dialects look like
ReadThe same step, in the two forms a Refit export might take:
import pandas as pd
df = pd.read_csv("customers.csv")
df["income"] = df["income"].fillna(df["income"].median())library(dplyr)
df <- read.csv("customers.csv")
df <- df |> mutate(income = coalesce(income, median(income, na.rm = TRUE)))Which should you hand over?
ReflectThink of who runs this next.
- An analyst rerunning this month's report on new data needs Refit in their language.
- A colleague checking your published numbers needs Replay.
- An engineer putting a model into production needs the Pipeline, so that values are learned from training rows.
Who is that person for your current work?
Check your understanding
CheckpointCheck yourself
You send code to run on next quarter's data, and imputation should reflect that quarter. Which mode?
Show the answer
Answer: b
Replay would keep this quarter's median forever. Refit recalculates it.
Check yourself
Why are the scikit-learn and recipes targets always Refit?
Show the answer
Answer: a
Fit on training rows, apply elsewhere: that is what a pipeline is, and it is how leakage is avoided.
Recap
Recap- Code exports a recipe to SQL, pandas, dplyr, scikit-learn or recipes.
- Replay reproduces the app; Refit recomputes on new data.
- Pipelines are always Refit, because they fit on training rows.