|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "counterlab-intro", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Customer churn: does 99% mean new-customer generalization?\n", |
| 9 | + "\n", |
| 10 | + "This public sample intentionally uses a row-wise split while retaining `customer_id`." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "load-fixture", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "Loaded 2880 rows across 480 customers\n" |
| 24 | + ] |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "from pathlib import Path\n", |
| 29 | + "import pandas as pd\n", |
| 30 | + "\n", |
| 31 | + "data_path = Path('../public/customer_churn.csv')\n", |
| 32 | + "df = pd.read_csv(data_path)\n", |
| 33 | + "df[['customer_id', 'churned']].head()" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "id": "evaluation-note", |
| 39 | + "metadata": {}, |
| 40 | + "source": [ |
| 41 | + "## Reported evaluation\n", |
| 42 | + "\n", |
| 43 | + "The next cell uses a random row split. Repeated observations from one customer can land on both sides." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 2, |
| 49 | + "id": "random-row-split", |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [ |
| 52 | + { |
| 53 | + "name": "stdout", |
| 54 | + "output_type": "stream", |
| 55 | + "text": [ |
| 56 | + "Customer group-split accuracy: 0.625\n", |
| 57 | + "Customer group-split ROC AUC: 0.662\n", |
| 58 | + "Train/test customer overlap: 0 (0.0%)\n" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + "COUNTERLAB_PATCHED_RESULT={\"dropFeatures\":[\"customer_id\"],\"entityCounts\":{\"test\":120,\"train\":360},\"entityOverlap\":{\"count\":0,\"rate\":0.0},\"featureSetFingerprint\":\"9641773fdead963da5fb0582be52c255307fc4d601c4a4b580f5730d088f2362\",\"groupBy\":\"customer_id\",\"id\":\"patched_customer_group_split\",\"inputFingerprint\":\"5c482f39e4e948a92dab61bf9c9f5c6577fbe9fc688fd597c9fefd785ee1be70\",\"kernelVersion\":\"0.1.0\",\"metrics\":{\"accuracy\":0.625,\"rocAuc\":0.662186573617},\"model\":\"logistic_regression\",\"pipelineFingerprint\":\"234782290268e64b80af2e23b2b9b97268db8cd8e8740522834cde57cae78824\",\"resultHash\":\"df4cbaf93266decd43e51f3fe0bcb9d1d6d3476c1b0e466e0fa87fa8c43aa508\",\"sampleSizes\":{\"test\":720,\"train\":2160},\"schemaVersion\":\"1\",\"seed\":1729,\"splitStrategy\":\"group\"}" |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "from sklearn.compose import ColumnTransformer\n", |
| 71 | + "from sklearn.impute import SimpleImputer\n", |
| 72 | + "from sklearn.linear_model import LogisticRegression\n", |
| 73 | + "from sklearn.metrics import accuracy_score, roc_auc_score\n", |
| 74 | + "from sklearn.model_selection import GroupShuffleSplit\n", |
| 75 | + "from sklearn.pipeline import Pipeline\n", |
| 76 | + "from sklearn.preprocessing import OneHotEncoder, StandardScaler\n", |
| 77 | + "\n", |
| 78 | + "groups = df['customer_id']\n", |
| 79 | + "X = df.drop(columns=['churned', 'customer_id', 'observation_id'])\n", |
| 80 | + "y = df['churned']\n", |
| 81 | + "categorical = [c for c in X.columns if X[c].dtype == 'object']\n", |
| 82 | + "numeric = [c for c in X.columns if c not in categorical]\n", |
| 83 | + "preprocess = ColumnTransformer([\n", |
| 84 | + " ('categorical', OneHotEncoder(handle_unknown='ignore'), categorical),\n", |
| 85 | + " ('numeric', Pipeline([('impute', SimpleImputer()), ('scale', StandardScaler())]), numeric),\n", |
| 86 | + "])\n", |
| 87 | + "model = Pipeline([('preprocess', preprocess), ('model', LogisticRegression(C=100, max_iter=2000))])\n", |
| 88 | + "splitter = GroupShuffleSplit(n_splits=1, test_size=0.25, random_state=1729)\n", |
| 89 | + "train_index, test_index = next(splitter.split(X, y, groups=groups))\n", |
| 90 | + "X_train, X_test = X.iloc[train_index], X.iloc[test_index]\n", |
| 91 | + "y_train, y_test = y.iloc[train_index], y.iloc[test_index]\n", |
| 92 | + "train_customers = set(groups.iloc[train_index])\n", |
| 93 | + "test_customers = set(groups.iloc[test_index])\n", |
| 94 | + "customer_overlap = train_customers.intersection(test_customers)\n", |
| 95 | + "assert not customer_overlap\n", |
| 96 | + "model.fit(X_train, y_train)\n", |
| 97 | + "probability = model.predict_proba(X_test)[:, 1]\n", |
| 98 | + "prediction = (probability >= 0.5).astype(int)\n", |
| 99 | + "print(f'Customer group-split accuracy: {accuracy_score(y_test, prediction):.3f}')\n", |
| 100 | + "print(f'Customer group-split ROC AUC: {roc_auc_score(y_test, probability):.3f}')\n", |
| 101 | + "print(f'Train/test customer overlap: {len(customer_overlap)}')" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "id": "learner-claim", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "**Learner claim to test:** This result proves the model generalizes to customers it has never seen." |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "metadata": { |
| 114 | + "counterlab": { |
| 115 | + "createdAt": "2026-07-14T00:00:00.000Z", |
| 116 | + "fixtureSeed": 1729, |
| 117 | + "resultHash": "a6ae7652e04e4d70196f991c63b8f7bcb3b76f8c4ab833d3ce2b626df0ab6c94", |
| 118 | + "schemaSummary": { |
| 119 | + "entityCandidates": [ |
| 120 | + "customer_id" |
| 121 | + ], |
| 122 | + "fields": [ |
| 123 | + { |
| 124 | + "inferredType": "categorical", |
| 125 | + "name": "observation_id", |
| 126 | + "privacyClass": "feature" |
| 127 | + }, |
| 128 | + { |
| 129 | + "inferredType": "categorical", |
| 130 | + "name": "customer_id", |
| 131 | + "privacyClass": "entity_identifier" |
| 132 | + }, |
| 133 | + { |
| 134 | + "inferredType": "number", |
| 135 | + "name": "tenure_months", |
| 136 | + "privacyClass": "feature" |
| 137 | + }, |
| 138 | + { |
| 139 | + "inferredType": "number", |
| 140 | + "name": "monthly_charges", |
| 141 | + "privacyClass": "feature" |
| 142 | + }, |
| 143 | + { |
| 144 | + "inferredType": "number", |
| 145 | + "name": "support_tickets", |
| 146 | + "privacyClass": "feature" |
| 147 | + }, |
| 148 | + { |
| 149 | + "inferredType": "number", |
| 150 | + "name": "activity_days_30d", |
| 151 | + "privacyClass": "feature" |
| 152 | + }, |
| 153 | + { |
| 154 | + "inferredType": "categorical", |
| 155 | + "name": "contract_type", |
| 156 | + "privacyClass": "feature" |
| 157 | + }, |
| 158 | + { |
| 159 | + "inferredType": "integer", |
| 160 | + "name": "churned", |
| 161 | + "privacyClass": "target" |
| 162 | + } |
| 163 | + ], |
| 164 | + "rowCount": 2880, |
| 165 | + "targetCandidates": [ |
| 166 | + "churned" |
| 167 | + ] |
| 168 | + }, |
| 169 | + "storedOutputSource": "counterlab-kernel" |
| 170 | + }, |
| 171 | + "kernelspec": { |
| 172 | + "display_name": "Python 3", |
| 173 | + "language": "python", |
| 174 | + "name": "python3" |
| 175 | + }, |
| 176 | + "language_info": { |
| 177 | + "name": "python", |
| 178 | + "version": "3.14" |
| 179 | + } |
| 180 | + }, |
| 181 | + "nbformat": 4, |
| 182 | + "nbformat_minor": 5 |
| 183 | +} |
0 commit comments