Skip to main content

Recipe.fit()

Fit each step in order on the training data.

Usage

Source

Recipe.fit(data)

Each step is fitted on the output of the step before it, so a later step sees the columns as the earlier ones left them, and its selector is resolved against those columns. Steps that only act on training samples, such as row filters, are applied here, so later steps are fitted on the filtered rows, but they are skipped by FittedRecipe.transform().

Every call estimates everything from scratch: the recipe is not changed, it can be fitted again on other data, and each FittedRecipe is independent of the recipe and of every other fit. Returning a separate trained object keeps the untrained specification reusable (for example inside cross-validation, where it is fitted once per fold) and makes the object that holds the statistics the one you save and apply to new data. Missing values are handled by the individual steps, not by the recipe. The training data is not kept, so to get the transformed training data call FittedRecipe.transform() on it; there is no fit_transform(). Note that transform() skips row filters, so it can return more rows than the steps were fitted on. To see which columns each step added or removed, use FittedRecipe.summary().

When no features were declared, every column that is not a target or a case weight is given the "feature" role (see Recipe).

Parameters

data: DataFrame
The training data frame.

Raises

StepError

If a step fails while fitting or while transforming the training data during the fit; the error names the step and the columns it was working on.

ValueError

If a step would produce a column name that already exists, or a role operation (add_role(), update_role(), remove_role(), or set_roles() written after one) fails: its columns cannot be selected, a column lacks the role it is asked to give up, the result would hold incompatible roles, or it is positioned after a step the recipe does not have.

ImportError
If a step needs an optional dependency that is not installed.

Examples

fit() estimates each step’s statistics on the training data and returns a FittedRecipe with the selectors resolved to concrete columns:

recipe = (
    Recipe(data=credit, target="Status")
    .add(StepNormalize(s.numeric_features()))
    .add(StepDummy(s.nominal_features()))
)
fitted = recipe.fit(credit)
fitted
Recipe (trained)
  Inputs: 23 columns (22 features, 1 target)
  Steps:
    step_normalize(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price)                                                                                                                                           [trained]
    step_dummy(Home, Marital, Records, Job → Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_separated, Marital_single, Marital_widow, Records_yes, Job_freelance, Job_others, Job_partime)  [trained]