Skip to content

comet_ml.integration.sklearn ¶

load_model ¶

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)

Parameters:

  • model_uri (str) –

    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 (Any, default: {} ) –

    Passed to persistence_module.load

log_model ¶

log_model(
    experiment: comet_ml.CometExperiment,
    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,
)

Parameters:

  • experiment (CometExperiment) –

    Instance of experiment to log model.

  • model (Any) –

    Model to log.

  • model_name (str) –

    The name of the model.

  • metadata (Optional[Dict], default: None ) –

    Some additional data to attach to the data. Must be a JSON-encodable dict.

  • persistence_module (Optional[ModuleType], default: None ) –

    Module for model serialization. If not specified - joblib is used. Currently supported modules: pickle, cloudpickle, joblib.

  • scikit_learn_dump_kwargs –

    Key-value arguments for passing to persistence_module.dump method.

Dec. 2, 2024