|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "7fb27b941602401d91542211134fc71a", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Optuna + `SEFRBoostClassifier`\n", |
| 9 | + "\n", |
| 10 | + "Tune [`SEFRBoostClassifier`](https://github.com/LinearBoost/linearboost-classifier) (gradient boosting with SEFR oblique splits) using [Optuna](https://optuna.org/) on sklearn’s **Breast Cancer Wisconsin** dataset (binary).\n", |
| 11 | + "\n", |
| 12 | + "**Install (if needed):** `pip install linearboost optuna scikit-learn` — or install this repo editable: `pip install -e .` from the repository root." |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "id": "acae54e37e7d407bbb7b55eff062a284", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import warnings\n", |
| 23 | + "\n", |
| 24 | + "import numpy as np\n", |
| 25 | + "import optuna\n", |
| 26 | + "from sklearn.datasets import load_breast_cancer\n", |
| 27 | + "from sklearn.metrics import f1_score, roc_auc_score\n", |
| 28 | + "from sklearn.model_selection import StratifiedKFold, train_test_split\n", |
| 29 | + "from sklearn.pipeline import Pipeline\n", |
| 30 | + "from sklearn.preprocessing import StandardScaler\n", |
| 31 | + "\n", |
| 32 | + "from linearboost import SEFRBoostClassifier\n", |
| 33 | + "\n", |
| 34 | + "warnings.filterwarnings(\"ignore\")\n", |
| 35 | + "optuna.logging.set_verbosity(optuna.logging.WARNING)" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "id": "9a63283cbaf04dbcab1f6479b197f3a8", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "## 1. Load data and train / test split\n", |
| 44 | + "\n", |
| 45 | + "`SEFRBoostClassifier` expects **dense numeric** input; we use `StandardScaler` in a pipeline." |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "id": "8dd0d8092fe74a7c96281538738b07e2", |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "X, y = load_breast_cancer(return_X_y=True)\n", |
| 56 | + "X_train, X_test, y_train, y_test = train_test_split(\n", |
| 57 | + " X, y, test_size=0.25, stratify=y, random_state=42\n", |
| 58 | + ")\n", |
| 59 | + "print(\"Train:\", X_train.shape, \"Test:\", X_test.shape, \"Classes:\", np.unique(y))" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "markdown", |
| 64 | + "id": "72eea5119410473aa328ad9291626812", |
| 65 | + "metadata": {}, |
| 66 | + "source": [ |
| 67 | + "## 2. Quick baseline (default hyperparameters)\n", |
| 68 | + "\n", |
| 69 | + "`Pipeline(StandardScaler → SEFRBoostClassifier)`." |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "id": "8edb47106e1a46a883d545849b8ab81b", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "baseline = Pipeline(\n", |
| 80 | + " [\n", |
| 81 | + " (\"scale\", StandardScaler()),\n", |
| 82 | + " (\"clf\", SEFRBoostClassifier(n_estimators=50, random_state=42)),\n", |
| 83 | + " ]\n", |
| 84 | + ")\n", |
| 85 | + "baseline.fit(X_train, y_train)\n", |
| 86 | + "y_pred = baseline.predict(X_test)\n", |
| 87 | + "y_proba = baseline.predict_proba(X_test)[:, 1]\n", |
| 88 | + "print(\"Baseline F1 (weighted):\", f1_score(y_test, y_pred, average=\"weighted\"))\n", |
| 89 | + "print(\"Baseline ROC-AUC:\", roc_auc_score(y_test, y_proba))" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "id": "10185d26023b46108eb7d9f57d49d2b3", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "## 3. Optuna: maximize cross-validated F1\n", |
| 98 | + "\n", |
| 99 | + "Objective: suggest tree size, learning rate, depth, leaf constraints, and subsample; evaluate with **5-fold stratified CV** on the training set only (fast enough for local runs)." |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "id": "8763a12b2bbd4a93a75aff182afb95dc", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n", |
| 110 | + "\n", |
| 111 | + "\n", |
| 112 | + "def objective(trial: optuna.Trial) -> float:\n", |
| 113 | + " params = {\n", |
| 114 | + " \"n_estimators\": trial.suggest_int(\"n_estimators\", 20, 150),\n", |
| 115 | + " \"learning_rate\": trial.suggest_float(\"learning_rate\", 0.02, 0.3, log=True),\n", |
| 116 | + " \"max_depth\": trial.suggest_int(\"max_depth\", 2, 6),\n", |
| 117 | + " \"min_samples_leaf\": trial.suggest_int(\"min_samples_leaf\", 5, 40),\n", |
| 118 | + " \"min_samples_split\": trial.suggest_int(\"min_samples_split\", 10, 80),\n", |
| 119 | + " \"subsample\": trial.suggest_float(\"subsample\", 0.6, 1.0),\n", |
| 120 | + " \"random_state\": 42,\n", |
| 121 | + " }\n", |
| 122 | + " pipe = Pipeline(\n", |
| 123 | + " [\n", |
| 124 | + " (\"scale\", StandardScaler()),\n", |
| 125 | + " (\"clf\", SEFRBoostClassifier(**params)),\n", |
| 126 | + " ]\n", |
| 127 | + " )\n", |
| 128 | + " scores = []\n", |
| 129 | + " for train_idx, val_idx in cv.split(X_train, y_train):\n", |
| 130 | + " pipe.fit(X_train[train_idx], y_train[train_idx])\n", |
| 131 | + " pred = pipe.predict(X_train[val_idx])\n", |
| 132 | + " scores.append(f1_score(y_train[val_idx], pred, average=\"weighted\"))\n", |
| 133 | + " return float(np.mean(scores))\n", |
| 134 | + "\n", |
| 135 | + "\n", |
| 136 | + "study = optuna.create_study(direction=\"maximize\")\n", |
| 137 | + "study.optimize(objective, n_trials=30, show_progress_bar=True)\n", |
| 138 | + "print(\"Best trial:\", study.best_trial.number, \"F1 (CV mean):\", study.best_value)\n", |
| 139 | + "print(\"Best params:\", study.best_params)" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "id": "7623eae2785240b9bd12b16a66d81610", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "## 4. Fit tuned model on full training set and evaluate on held-out test" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "id": "7cdc8c89c7104fffa095e18ddfef8986", |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "best = study.best_params.copy()\n", |
| 158 | + "best[\"random_state\"] = 42\n", |
| 159 | + "tuned = Pipeline(\n", |
| 160 | + " [\n", |
| 161 | + " (\"scale\", StandardScaler()),\n", |
| 162 | + " (\"clf\", SEFRBoostClassifier(**best)),\n", |
| 163 | + " ]\n", |
| 164 | + ")\n", |
| 165 | + "tuned.fit(X_train, y_train)\n", |
| 166 | + "y_pred_t = tuned.predict(X_test)\n", |
| 167 | + "y_proba_t = tuned.predict_proba(X_test)[:, 1]\n", |
| 168 | + "print(\"Tuned F1 (weighted):\", f1_score(y_test, y_pred_t, average=\"weighted\"))\n", |
| 169 | + "print(\"Tuned ROC-AUC:\", roc_auc_score(y_test, y_proba_t))" |
| 170 | + ] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": "Python 3", |
| 176 | + "language": "python", |
| 177 | + "name": "python3" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "name": "python", |
| 181 | + "version": "3.11.0" |
| 182 | + } |
| 183 | + }, |
| 184 | + "nbformat": 4, |
| 185 | + "nbformat_minor": 5 |
| 186 | +} |
0 commit comments