Lesson 3 of 11 · 2 min
MLflow: Experiment Tracking
MLflow tracks parameters, metrics, artifacts (plots, models) for every experiment run.
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score
mlflow.set_tracking_uri('sqlite:///mlflow.db') # Local SQLite
mlflow.set_experiment('cancer_classification')
with mlflow.start_run(run_name='RandomForest_v1'):
# Log parameters
params = {'n_estimators': 100, 'max_depth': 5, 'random_state': 42}
mlflow.log_params(params)
# Train model
model = RandomForestClassifier(**params)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# Log metrics
mlflow.log_metric('accuracy', accuracy_score(y_test, y_pred))
mlflow.log_metric('f1_score', f1_score(y_test, y_pred))
# Log artifact (e.g., confusion matrix plot)
mlflow.log_artifact('confusion_matrix.png')
# Log model
mlflow.sklearn.log_model(model, artifact_path='model',
registered_model_name='CancerClassifier')Launch MLflow UI: mlflow ui --backend-store-uri sqlite:///mlflow.db
