Skip to content

integration.sklearn

log_model

comet_ml.integration.sklearn.log_model(
    experiment: "comet_ml.BaseExperiment", model_name: str, model: Any,
    metadata: Optional[Dict] = None,
    persistence_module: Optional[ModuleType] = None,
    **scikit_learn_dump_kwargs)

Logs a scikit-learn model to an experiment. This will save the model using provided persistence module and save it as an Experiment Model.

The model argument can be any object that is serializable by provided persistence module.

If a model has best_estimator_ attribute (as different model_selection.*SearchCV classes) then only this value will be logged to experiment.

Here is an example of logging a model:

experiment = comet_ml.Experiment()

model = svm.SVC()
model.fit(iris.data, iris.target)

comet_ml.integration.sklearn.log_model(
    experiment,
    "my-model",
    model,
    persistence_module=pickle,
)

Args:

  • experiment: Experiment (required), instance of experiment to log model
  • model: model to log
  • model_name: string (required), the name of the model
  • metadata: dict (optional), some additional data to attach to the the data. Must be a JSON-encodable dict
  • persistence_module: module (optional), module for model serialization. If not specified - joblib is used. Currently supported modules: pickle, cloudpickle, joblib.
  • scikit_learn_dump_kwargs: optional key-value arguments for passing to persistence_module.dump method

Returns: None

load_model

comet_ml.integration.sklearn.load_model(MODEL_URI: str,
    **sklearn_load_args) -> Any

Load model from experiment, registry or from disk by uri. This will load the model using the persistence module used for saving this model via log_model.

Here is an example of loading a model from the Model Registry for inference:

model = comet_ml.integration.sklearn.load_model("registry://WORKSPACE/my-model")

X, y = datasets.load_iris(return_X_y=True)
model.predict(X)

Args:

  • uri: string (required), a uri string defining model location. Possible options are:

    • file://data/my-model

    • file:///path/to/my-model

    • registry://workspace/registry_name (takes the last version)

    • registry://workspace/registry_name:version

    • experiment://experiment_key/model_name

    • experiment://workspace/project_name/experiment_name/model_name

  • sklearn_load_args: (optional) passed to persistence_module.load

Returns: model

Apr. 24, 2024