{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1d56c9f-4316-4c64-bdb3-942c3f50945f",
   "metadata": {},
   "source": [
    "This notebook trains a Random Forest regression model on the diabetes dataset while tracking the experiment using MLflow. The model is registered in MLflow’s Model Registry for versioning and deployment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "execution_state": "idle",
   "id": "d97e3fa5-9c49-4ce7-8c7e-98e115aefe1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mlflow\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.datasets import load_diabetes\n",
    "from sklearn.ensemble import RandomForestRegressor\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "execution_state": "idle",
   "id": "c7856ee2-3807-4a89-9906-17c20af9f8c2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Active run ID: d28f620c57bb4384a62541d08d8435f3\n"
     ]
    }
   ],
   "source": [
    "mlflow.set_experiment(\"UAT Test\")\n",
    "mlflow.autolog()\n",
    "\n",
    "# Start a new MLflow run\n",
    "with mlflow.start_run() as run:\n",
    "    # Load data and split it into train and test sets\n",
    "    db = load_diabetes()\n",
    "    X_train, X_test, y_train, y_test = train_test_split(db.data, db.target)\n",
    "\n",
    "    # Build and train a RandomForestRegressor model\n",
    "    rf = RandomForestRegressor(n_estimators=100, max_depth=6, max_features=3)\n",
    "    rf.fit(X_train, y_train)\n",
    "\n",
    "    # Get predictions on the test set\n",
    "    predictions = rf.predict(X_test)\n",
    "\n",
    "    # Get and print the active run's ID\n",
    "    run_id = run.info.run_id  # run.info exists because we're inside a run context\n",
    "    print(f\"Active run ID: {run_id}\")\n",
    "\n",
    "    # Register model (adjust model URI with correct run_id)\n",
    "    model_uri = f\"runs:/{run_id}/model\"\n",
    "    model_name = \"DiabetesRandomForest-2025\"\n",
    "    mlflow.register_model(model_uri, model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c94c8992-9c01-41b5-b1be-ebbc983154af",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9 (Recommended)",
   "language": "python",
   "name": "syntasa_kernel_python_39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
