Experiment tracking helps you keep a clear record of your machine learning tests. MLflow makes it easy to save and compare your model results.
Experiment tracking (MLflow) in ML Python
import mlflow with mlflow.start_run(): mlflow.log_param("param_name", param_value) mlflow.log_metric("metric_name", metric_value) mlflow.log_artifact("path_to_file") mlflow.sklearn.log_model(model, "model")
Use mlflow.start_run() to begin tracking an experiment.
Log parameters, metrics, artifacts, and models inside the run block.
import mlflow with mlflow.start_run(): mlflow.log_param("learning_rate", 0.01) mlflow.log_metric("accuracy", 0.85)
import mlflow with mlflow.start_run(): mlflow.log_param("max_depth", 5) mlflow.log_metric("rmse", 3.2) mlflow.log_artifact("model.pkl")
This program trains a simple Random Forest on iris data, logs parameters, accuracy, and the model using MLflow.
import mlflow import mlflow.sklearn from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score # Load data iris = load_iris() X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42) # Start MLflow experiment with mlflow.start_run(): # Set and log parameter n_estimators = 10 mlflow.log_param("n_estimators", n_estimators) # Train model model = RandomForestClassifier(n_estimators=n_estimators, random_state=42) model.fit(X_train, y_train) # Predict and calculate accuracy preds = model.predict(X_test) acc = accuracy_score(y_test, preds) # Log metric mlflow.log_metric("accuracy", acc) # Log model mlflow.sklearn.log_model(model, "random_forest_model") print(f"Logged run with accuracy: {acc:.4f}")
MLflow saves experiments locally by default in an 'mlruns' folder.
You can view logged experiments using the MLflow UI by running mlflow ui in your terminal.
Always start a run with mlflow.start_run() to group logs together.
MLflow helps you track your machine learning experiments easily.
You can log parameters, metrics, artifacts, and models to compare results later.
Use the MLflow UI to visualize and manage your experiments.
Practice
Solution
Step 1: Understand MLflow's role
MLflow is designed to help track experiments by logging parameters, metrics, and models.Step 2: Compare options
Options A, C, and D describe tasks outside MLflow's main purpose.Final Answer:
To track and manage machine learning experiments -> Option CQuick Check:
MLflow = experiment tracking [OK]
- Confusing MLflow with model building libraries
- Thinking MLflow is only for visualization
- Assuming MLflow deploys models directly
Solution
Step 1: Recall MLflow run start syntax
The correct function to start a run is mlflow.start_run().Step 2: Check other options
Functions like init_experiment, create_run, and begin_experiment do not exist in MLflow API.Final Answer:
mlflow.start_run() -> Option BQuick Check:
Start run = mlflow.start_run() [OK]
- Using non-existent MLflow functions
- Confusing experiment and run functions
- Misspelling function names
import mlflow
with mlflow.start_run():
mlflow.log_param('alpha', 0.1)
mlflow.log_metric('accuracy', 0.95)
run_info = mlflow.active_run().info
print(run_info.run_id is not None)Solution
Step 1: Understand mlflow.start_run() context
The code starts a run, logs parameters and metrics, then gets the active run info.Step 2: Check run_id existence
Inside the run context, run_id is assigned and not None, so print(run_info.run_id is not None) outputs True.Final Answer:
True -> Option AQuick Check:
Active run has run_id = True [OK]
- Assuming run_id is None before logging
- Confusing run info with experiment info
- Expecting print to show run_id string
import mlflow
mlflow.log_param('learning_rate', 0.01)
with mlflow.start_run():
mlflow.log_metric('loss', 0.2)Solution
Step 1: Check order of MLflow calls
log_param is called before starting a run, which is not allowed.Step 2: Validate run context requirement
MLflow requires logging functions inside a run context started by start_run().Final Answer:
log_param called outside a run context -> Option AQuick Check:
Logging outside run = error [OK]
- Calling log_param before start_run()
- Ignoring run context for logging
- Assuming log_metric order matters
Solution
Step 1: Understand MLflow run separation
Each model should be run and logged in its own mlflow.start_run() block to keep results separate.Step 2: Use MLflow UI for comparison
After logging metrics and parameters, MLflow UI allows easy comparison of runs.Final Answer:
Run each model in separate mlflow.start_run() blocks, log metrics, then use MLflow UI to compare -> Option DQuick Check:
Separate runs + UI = compare models [OK]
- Logging multiple models in one run
- Skipping metric logging
- Ignoring MLflow UI for comparison
Ready to prove it?
Pass the test and this pattern moves unknown
