Skip to main content

from_dict()

Reconstruct a FittedRecipe from the dict produced by to_dict.

Usage

Source

from_dict(
    data,
    *,
    trusted=(),
)

The payload is treated as untrusted data: it can name only classes from mlfeatures, from packages that register a step under the mlfeatures.steps entry-point group, and from the packages of the classes passed as trusted. Each class must also be of the right kind (a Step subclass for a step, a dataclass for fit state). Loading never calls anything else the payload names, and a name is checked before its module is imported. The values inside the payload are not validated.

A payload without a format_version is read as the current version. One with a different version is refused, because the layout changed incompatibly; fit the recipe again and save it again.

Parameters

data: dict[str, Any]

The dict produced by to_dict.

trusted: Iterable[type] = ()
Classes whose top-level packages the payload may also name classes from, for a custom step that is not registered (one defined in a script or notebook, or still under development). Pass only classes you wrote or trust: a package listed here is imported as the payload asks. Defaults to none.

Returns

FittedRecipe
The recipe, ready to FittedRecipe.transform new data.

Raises

ValueError
If the payload is not a mapping with roles and steps, has a format_version this mlfeatures does not read, names a class outside the trusted packages, or names something that is not a step or dataclass.

Examples

recipe = Recipe().add(StepNormalize(s.numeric_features()))
fitted = recipe.fit(credit)
restored = from_dict(to_dict(fitted))
restored
Recipe (trained)
  Inputs: 14 columns (14 features)
  Steps:
    step_normalize(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price)  [trained]