Scikit-Learn#

About this page

This is an API reference for using Scikit-Learn in BentoML. Please refer to Scikit-Learn Guide for more information about how to use Scikit-learn in BentoML.

bentoml.sklearn.save_model(name: str, model: SklearnModel, *, signatures: ModelSignaturesType | None = None, labels: t.Dict[str, str] | None = None, custom_objects: t.Dict[str, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, metadata: t.Dict[str, t.Any] | None = None) bentoml.Model[source]#

Save a model instance to BentoML modelstore.

Parameters
  • name (str) – Name for given model instance. This should pass Python identifier check.

  • model (Union[BaseEstimator, Pipeline]) – Instance of model to be saved.

  • ( (signatures) –

    code: Dict[str, ModelSignatureDict]) Methods to expose for running inference on the target model. Signatures are

    used for creating Runner instances when serving model with bentoml.Service

  • labels (Dict[str, str], optional, default to None) – user-defined labels for managing models, e.g. team=nlp, stage=dev

  • custom_objects (Dict[str, Any]], optional, default to None) –

    user-defined additional python objects to be saved alongside the model,

    e.g. a tokenizer instance, preprocessor function, model configuration json

  • external_modules (List[ModuleType], optional, default to None) – user-defined additional python modules to be saved alongside the model or custom objects, e.g. a tokenizer module, preprocessor module, model configuration module

  • metadata (Dict[str, Any], optional, default to None) – Custom metadata for given model.

Returns

A tag with a format name:version where name is the user-defined model’s name, and a generated version.

Return type

Tag

Examples:

import bentoml

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
iris = load_iris()
X = iris.data[:, :4]
Y = iris.target
model.fit(X, Y)

bento_model = bentoml.sklearn.save_model('kneighbors', model)
bentoml.sklearn.load_model(bento_model: str | Tag | Model) SklearnModel[source]#

Load the scikit-learn model with the given tag from the local BentoML model store.

Parameters
  • bento_model (str | Tag | Model) – Either the tag of the model to get from the store, or a BentoML ~bentoml.Model instance to load the model from.

  • ... –

Returns

The scikit-learn model loaded from the model store or BentoML Model.

Return type

BaseEstimator | Pipeline

Example: .. code-block:: python

import bentoml sklearn = bentoml.sklearn.load_model(β€˜my_model:latest’)

bentoml.sklearn.get(tag_like: str | bentoml._internal.tag.Tag) Model[source]#