{
"cells": [
{
"cell_type": "markdown",
"id": "8a7af915",
"metadata": {},
"source": [
"# MLflow Integration\n",
"\n",
"This notebook walks through a practical MLOps workflow with Darts' native MLflow integration: enable autolog, compare forecasting models, promote the best one to the registry, and run inference from a production alias.\n",
"\n",
"If you are new to Darts, see the [Quickstart Guide](https://unit8co.github.io/darts/quickstart/00-quickstart.html) first.\n",
"\n",
"**Prerequisites:** install MLflow as an optional dependency:\n",
"\n",
"```bash\n",
"pip install \"mlflow>=3.0\"\n",
"```\n",
"\n",
"API reference: [darts.utils.mlflow](https://unit8co.github.io/darts/generated_api/darts.utils.mlflow.html)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7b28941c",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11bca7fd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import os\n",
"import tempfile\n",
"import warnings\n",
"\n",
"import mlflow\n",
"import plotly\n",
"from mlflow import MlflowClient\n",
"\n",
"import darts.metrics as metrics\n",
"from darts import set_option\n",
"from darts.datasets import AirPassengersDataset\n",
"from darts.models import ExponentialSmoothing, LinearRegressionModel\n",
"from darts.utils.mlflow import autolog, load_model\n",
"\n",
"warnings.filterwarnings(\"ignore\", category=FutureWarning)\n",
"set_option(\"plotting.use_darts_style\", True)\n",
"plotly.offline.init_notebook_mode()\n",
"\n",
"PLOTLY_KWARGS = dict(\n",
" legend=dict(yanchor=\"top\", y=0.99, xanchor=\"left\", x=0.01),\n",
")"
]
},
{
"cell_type": "markdown",
"id": "4eb6fc63",
"metadata": {},
"source": [
"## 1. MLflow setup\n",
"\n",
"Point MLflow at a tracking backend and create an experiment. We use a temporary directory so this notebook runs self-contained: run metadata goes to a SQLite database, and artifacts (models, JSON files) are stored alongside it. In production, set `tracking_uri` to your team's MLflow server or local database."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7aced0df",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2026/09/04 14:03:54 INFO mlflow.store.db.utils: Creating initial MLflow database tables...\n",
"2026/09/04 14:03:54 INFO mlflow.store.db.utils: Updating database tables\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tracking URI: sqlite:////var/folders/4h/l09drklx06g8022q7khgyxyr0000gn/T/tmpjnang74m/mlflow.db\n",
"Experiment: darts-mlflow-examples\n",
"\n",
"To explore runs in the UI:\n",
" mlflow ui --backend-store-uri sqlite:////var/folders/4h/l09drklx06g8022q7khgyxyr0000gn/T/tmpjnang74m/mlflow.db\n"
]
}
],
"source": [
"tmpdir = tempfile.mkdtemp()\n",
"mlflow_db = os.path.join(tmpdir, \"mlflow.db\")\n",
"artifact_root = os.path.join(tmpdir, \"mlruns\")\n",
"\n",
"EXPERIMENT_NAME = \"darts-mlflow-examples\"\n",
"mlflow.set_tracking_uri(f\"sqlite:///{mlflow_db}\")\n",
"mlflow.set_experiment(\n",
" experiment_id=mlflow.create_experiment(\n",
" EXPERIMENT_NAME,\n",
" artifact_location=artifact_root,\n",
" )\n",
")\n",
"\n",
"print(f\"Tracking URI: {mlflow.get_tracking_uri()}\")\n",
"print(f\"Experiment: {mlflow.get_experiment_by_name(EXPERIMENT_NAME).name}\")\n",
"print(\n",
" f\"\\nTo explore runs in the UI:\\n mlflow ui --backend-store-uri sqlite:///{mlflow_db}\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "72438a10",
"metadata": {},
"source": [
"## 2. Load data\n",
"\n",
"We use the classic AirPassengers dataset. The last 36 months are held-out for evaluation using a rolling backtest."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f7e5ffa6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training: 108 points | Validation: 36 points\n"
]
},
{
"data": {
"application/vnd.plotly.v1+json": {
"data": [
{
"hovertemplate": "#Passengers: %{y:.3g}
| \n", " | run_id | \n", "tags.mlflow.runName | \n", "metrics.backtest_agg_mae | \n", "
|---|---|---|---|
| 0 | \n", "ee57c5714d6442ce93fbcf9899968842 | \n", "linear_regression_pretrained | \n", "18.870433 | \n", "
| 1 | \n", "0baa4c6c0e4d4ad5b7b53f5ae887ba02 | \n", "exponential_smoothing | \n", "19.609498 | \n", "
| 2 | \n", "8869add4ede74eebae1570acae7d82c6 | \n", "linear_regression | \n", "22.533742 | \n", "