Skip to main content

to_json()

Serialize a FittedRecipe to a diffable JSON string.

Usage

Source

to_json(
    recipe,
    *,
    indent=2,
)

The string is the to_dict payload as strictly valid JSON (non-finite floats are encoded explicitly, never as a bare NaN). It needs no optional dependency, so it is the default choice for storing a recipe next to a model. Warns like to_dict when the recipe names a class that from_json refuses by default.

Parameters

recipe: FittedRecipe

The fitted recipe to serialize.

indent: int = 2
Spaces of indentation per level. Defaults to 2.

Returns

str
The JSON text.

Raises

TypeError
As for to_dict.

Examples

recipe = Recipe().add(StepNormalize(s.numeric_features()))
fitted = recipe.fit(credit)
text = to_json(fitted)
text[:200]
'{\n  "mlfeatures_version": "0.1.1.dev1+g5e6dda29d",\n  "format_version": 1,\n  "roles": {\n    "Status": [\n      "feature"\n    ],\n    "Seniority": [\n      "feature"\n    ],\n    "Home": [\n      "feature"\n  '

A smaller indent gives a more compact file.

compact = to_json(fitted, indent=0)
compact[:200]
'{\n"mlfeatures_version": "0.1.1.dev1+g5e6dda29d",\n"format_version": 1,\n"roles": {\n"Status": [\n"feature"\n],\n"Seniority": [\n"feature"\n],\n"Home": [\n"feature"\n],\n"Time": [\n"feature"\n],\n"Age": [\n"feature"\n]'