recipe = (
Recipe(
data=credit,
target="Status",
features=["Age", "Income", "Home"],
)
.set_roles(id="Seniority")
.update_role_requirements("id", transform_required=False)
.add(StepNormalize(s.numeric_features()))
)
fitted = recipe.fit(credit)Recipe.update_role_requirements()
Return a new recipe overriding whether columns are required at transform.
Usage
Recipe.update_role_requirements(
role,
*,
transform_required,
)By default every column the recipe was fitted on must be present in the data given to FittedRecipe.transform(), except the target and case weights. That includes columns that no step uses, such as an ID column with a custom role, which is rarely what you want: transforming without it raises. Mark it as not required, or drop it from the data before fitting. The requirement of every column is recorded in the transform_required mapping of FittedRecipe.schema.
The feature and target roles are fixed: features are always required at transform and targets never are. A column that holds a custom role and the feature role stays required, so declare features explicitly when a column carries a custom role (see Recipe).
Parameters
role: str | FeaturesSelector-
A role name (e.g.
"id"), or a selector such asSelectors.starts_with("id_")resolved against the training data frame when the recipe is fitted. Passing"feature"or"target"raisesValueError. A selector that happens to match a feature or target column leaves that column unchanged. transform_required: bool-
Trueto require the columns at transform,Falseto allow them to be absent. When several overrides match a column, the last one wins.
Examples
Give Seniority an "id" role, relax its requirement, and fit:
New data without the ID column now transforms:
fitted.transform(credit.drop("Seniority").head(5))| Status | Home | Time | Age | Marital | Records | Job | Expenses | Income | Assets | Debt | Amount | Price |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | str | i64 | f64 | str | str | str | i64 | f64 | i64 | i64 | i64 | i64 |
| "good" | "rent" | 60 | -0.630317 | "married" | "no" | "freelance" | 73 | -0.157881 | 0 | 0 | 800 | 846 |
| "good" | "rent" | 60 | 1.909999 | "widow" | "no" | "fixed" | 48 | -0.133084 | 0 | 0 | 1000 | 1658 |
| "bad" | "owner" | 36 | 0.821292 | "married" | "yes" | "freelance" | 90 | 0.722397 | 3000 | 0 | 2000 | 2985 |
| "good" | "rent" | 60 | -1.17467 | "single" | "no" | "fixed" | 63 | 0.499228 | 2500 | 0 | 900 | 1325 |
| "good" | "rent" | 36 | -0.993219 | "single" | "no" | "fixed" | 46 | -0.430643 | 0 | 0 | 310 | 910 |