{ "cells": [ { "cell_type": "markdown", "id": "md59cd2519", "metadata": {}, "source": [ "# BitBullet:Lessons: Model Management\n", "\n", "You trained a great model. Now what?\n", "\n", "In most teams, that question is answered with a folder called `models/` containing files named `final_v3_USE_THIS.pkl`, a Slack message with the test AUC, and a vague memory of what preprocessing was applied. Six months later, no one can reproduce the result, the model is silently serving stale predictions, and rolling back to the previous version means digging through git blame.\n", "\n", "**This is model rot. And it is entirely preventable.**\n", "\n", "**Dataset**: `default_of_credit_card_clients.xls` — same dataset as Lesson 04. \n", "**Source**: [UCI ML Repository — Default of Credit Card Clients](https://archive.uci.edu/dataset/350/default+of+credit+card+clients)\n", "\n", "---\n", "\n", "### What This Tutorial Covers\n", "\n", "| Section | Component | What You Will Learn |\n", "|---------|-----------|---------------------|\n", "| 1 | Setup | Imports and lightweight training run |\n", "| 2 | `ModelMetadata` | Capturing identity, hyperparameters, metrics, and provenance |\n", "| 3 | `DatasetMetadata` | Recording what you trained on — including cryptographic hashing |\n", "| 4 | `metadata.summary()` | Human-readable model cards |\n", "| 5 | `metadata.to_dict()` | JSON-serialisable output for logging |\n", "| 6 | `ModelSerializer.save()` | Full packages — one artefact, everything inside |\n", "| 7 | `load_metadata()` | Fast metadata inspection without loading the model |\n", "| 8 | `ModelSerializer.load()` | Reconstructing the full package from disk |\n", "| 9 | `save_model_only()` | Lean model-only artefacts |\n", "| 10 | `ModelRegistry` | A typed catalogue of every model architecture |\n", "| 11 | `@ModelRegistry.register` | Extending BitBullet with custom model wrappers |\n", "| 12 | Version management | Semantic versioning patterns |\n", "| 13 | V1 vs V2 comparison | Loading, diffing, and visualising metric deltas |\n", "| 14 | Production checklist | What to verify before promoting a model |" ] }, { "cell_type": "markdown", "id": "9e6b0ecf", "metadata": {}, "source": [ "> **BitBullet Platform handles this lifecycle under the hood.**\n", "> [BitBullet Platform](https://bitbullet.co.uk) centralises projects, datasets, managed compute, storage, resolved configurations, fitted artefacts, preprocessing, metrics, and export material in a guided, repeatable workflow. Design and compare modelling configurations, inspect completed evidence, then export the trained result with metadata and generated inference code. This lesson shows how to manage those artefacts explicitly with the BitBullet SDK." ] }, { "cell_type": "markdown", "id": "md0da86e79", "metadata": {}, "source": [ "---\n", "## 1. Environment Setup & Imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "cd7ab0b1a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bitbullet : vdev\n", "All imports successful.\n" ] } ], "source": [ "import sys\n", "import os\n", "import json\n", "import shutil\n", "import warnings\n", "import time\n", "from datetime import datetime\n", "from pathlib import Path\n", "\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.ticker as mticker\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import roc_auc_score, f1_score, accuracy_score\n", "\n", "warnings.filterwarnings('ignore')\n", "\n", "# When running this notebook from bitbullet/lessons in a local clone, prefer the local SDK source.\n", "sdk_root = os.path.abspath(\"..\")\n", "if sdk_root not in sys.path:\n", " sys.path.insert(0, sdk_root)\n", "\n", "import bitbullet\n", "print(f\"bitbullet : v{getattr(bitbullet, '__version__', 'dev')}\")\n", "\n", "from bitbullet.model import (\n", " ModelMetadata,\n", " DatasetMetadata,\n", " ModelSerializer,\n", " ModelRegistry,\n", " BaseModel,\n", ")\n", "from bitbullet.transform import TransformPipeline\n", "from bitbullet.train import TrainConfig, OptunaTrainer\n", "\n", "print(\"All imports successful.\")" ] }, { "cell_type": "markdown", "id": "mdd108c418", "metadata": {}, "source": [ "---\n", "## 2. Quick Training Run\n", "\n", "**Dataset:** Default of Credit Card Clients Dataset \n", "**Source:** [UCI ML Repository — Default of Credit Card Clients](https://archive.uci.edu/dataset/350/default+of+credit+card+clients) \n", "**File:** `default_of_credit_card_clients.xls`\n", "\n", "> **Before running this cell:** download `default_of_credit_card_clients.xls` from the link above and\n", "> place it in the **same directory as this notebook**. If you store it elsewhere,\n", "> update `data_path` in the code cell below to match your chosen location." ] }, { "cell_type": "code", "execution_count": null, "id": "cde567e468", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataset loaded — shape: (30000, 24)\n", "Default rate — 22.1%\n" ] }, { "data": { "text/html": [ "
| \n", " | LIMIT_BAL | \n", "SEX | \n", "EDUCATION | \n", "MARRIAGE | \n", "AGE | \n", "PAY_0 | \n", "PAY_2 | \n", "PAY_3 | \n", "PAY_4 | \n", "PAY_5 | \n", "... | \n", "BILL_AMT4 | \n", "BILL_AMT5 | \n", "BILL_AMT6 | \n", "PAY_AMT1 | \n", "PAY_AMT2 | \n", "PAY_AMT3 | \n", "PAY_AMT4 | \n", "PAY_AMT5 | \n", "PAY_AMT6 | \n", "default_payment | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "20000 | \n", "Female | \n", "University | \n", "Married | \n", "24 | \n", "2 | \n", "2 | \n", "-1 | \n", "-1 | \n", "-2 | \n", "... | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "689 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
| 1 | \n", "120000 | \n", "Female | \n", "University | \n", "Single | \n", "26 | \n", "-1 | \n", "2 | \n", "0 | \n", "0 | \n", "0 | \n", "... | \n", "3272 | \n", "3455 | \n", "3261 | \n", "0 | \n", "1000 | \n", "1000 | \n", "1000 | \n", "0 | \n", "2000 | \n", "1 | \n", "
| 2 | \n", "90000 | \n", "Female | \n", "University | \n", "Single | \n", "34 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "... | \n", "14331 | \n", "14948 | \n", "15549 | \n", "1518 | \n", "1500 | \n", "1000 | \n", "1000 | \n", "1000 | \n", "5000 | \n", "0 | \n", "
3 rows × 24 columns
\n", "