← retour aux snippets

mlflow: autolog sklearn

Activer l'autologging pour capturer params/métriques automatiquement.

python tracking #mlflow#autolog#sklearn

objectif

Activer l’autologging pour capturer params/métriques automatiquement.

code minimal

import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(return_X_y=True)

mlflow.set_tracking_uri("file:./mlruns")
mlflow.set_experiment("autolog-demo")
mlflow.sklearn.autolog()

with mlflow.start_run(run_name="rf-autolog"):
    clf = RandomForestClassifier(n_estimators=200, random_state=0).fit(X, y)
    # Pas besoin de log manuel basique: autolog capture params et score
print(True)

utilisation

# Désactiver temporairement
mlflow.sklearn.autolog(disable=True)
print(True)

variante(s) utile(s)

# Récupérer dernier run
client = mlflow.tracking.MlflowClient()
runs = client.search_runs(experiment_ids=[client.get_experiment_by_name("autolog-demo").experiment_id])
print(len(runs) >= 1)

notes

  • Autolog capture aussi modèles et artifacts (selon intégration); vérifiez compatibilité des versions.