mayorsupy/Customer_Churn_Prediction_App
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 1,6 "id": "6b7961b1-55fe-45c1-99ce-e3fe57bf6bf2",7 "metadata": {},8 "outputs": [],9 "source": [10 "# Data handling\n",11 "import pandas as pd\n",12 "import numpy as np\n",13 "\n",14 "# Machine Learning\n",15 "from sklearn.model_selection import train_test_split\n",16 "from sklearn.preprocessing import OneHotEncoder\n",17 "from sklearn.metrics import accuracy_score, precision_score, roc_auc_score\n",18 "\n",19 "from xgboost import XGBClassifier\n",20 "\n",21 "# App Interface\n",22 "import gradio as gr\n"23 ]24 },25 {26 "cell_type": "code",27 "execution_count": 2,28 "id": "45381ff2-88e3-4d5f-bcdb-b364193af3cb",29 "metadata": {30 "scrolled": true31 },32 "outputs": [33 {34 "data": {35 "text/html": [36 "<div>\n",37 "<style scoped>\n",38 " .dataframe tbody tr th:only-of-type {\n",39 " vertical-align: middle;\n",40 " }\n",41 "\n",42 " .dataframe tbody tr th {\n",43 " vertical-align: top;\n",44 " }\n",45 "\n",46 " .dataframe thead th {\n",47 " text-align: right;\n",48 " }\n",49 "</style>\n",50 "<table border=\"1\" class=\"dataframe\">\n",51 " <thead>\n",52 " <tr style=\"text-align: right;\">\n",53 " <th></th>\n",54 " <th>customerID</th>\n",55 " <th>gender</th>\n",56 " <th>SeniorCitizen</th>\n",57 " <th>Partner</th>\n",58 " <th>Dependents</th>\n",59 " <th>tenure</th>\n",60 " <th>PhoneService</th>\n",61 " <th>MultipleLines</th>\n",62 " <th>InternetService</th>\n",63 " <th>OnlineSecurity</th>\n",64 " <th>...</th>\n",65 " <th>DeviceProtection</th>\n",66 " <th>TechSupport</th>\n",67 " <th>StreamingTV</th>\n",68 " <th>StreamingMovies</th>\n",69 " <th>Contract</th>\n",70 " <th>PaperlessBilling</th>\n",71 " <th>PaymentMethod</th>\n",72 " <th>MonthlyCharges</th>\n",73 " <th>TotalCharges</th>\n",74 " <th>Churn</th>\n",75 " </tr>\n",76 " </thead>\n",77 " <tbody>\n",78 " <tr>\n",79 " <th>0</th>\n",80 " <td>7590-VHVEG</td>\n",81 " <td>Female</td>\n",82 " <td>0</td>\n",83 " <td>Yes</td>\n",84 " <td>No</td>\n",85 " <td>1</td>\n",86 " <td>No</td>\n",87 " <td>No phone service</td>\n",88 " <td>DSL</td>\n",89 " <td>No</td>\n",90 " <td>...</td>\n",91 " <td>No</td>\n",92 " <td>No</td>\n",93 " <td>No</td>\n",94 " <td>No</td>\n",95 " <td>Month-to-month</td>\n",96 " <td>Yes</td>\n",97 " <td>Electronic check</td>\n",98 " <td>29.85</td>\n",99 " <td>29.85</td>\n",100 " <td>No</td>\n",101 " </tr>\n",102 " <tr>\n",103 " <th>1</th>\n",104 " <td>5575-GNVDE</td>\n",105 " <td>Male</td>\n",106 " <td>0</td>\n",107 " <td>No</td>\n",108 " <td>No</td>\n",109 " <td>34</td>\n",110 " <td>Yes</td>\n",111 " <td>No</td>\n",112 " <td>DSL</td>\n",113 " <td>Yes</td>\n",114 " <td>...</td>\n",115 " <td>Yes</td>\n",116 " <td>No</td>\n",117 " <td>No</td>\n",118 " <td>No</td>\n",119 " <td>One year</td>\n",120 " <td>No</td>\n",121 " <td>Mailed check</td>\n",122 " <td>56.95</td>\n",123 " <td>1889.5</td>\n",124 " <td>No</td>\n",125 " </tr>\n",126 " <tr>\n",127 " <th>2</th>\n",128 " <td>3668-QPYBK</td>\n",129 " <td>Male</td>\n",130 " <td>0</td>\n",131 " <td>No</td>\n",132 " <td>No</td>\n",133 " <td>2</td>\n",134 " <td>Yes</td>\n",135 " <td>No</td>\n",136 " <td>DSL</td>\n",137 " <td>Yes</td>\n",138 " <td>...</td>\n",139 " <td>No</td>\n",140 " <td>No</td>\n",141 " <td>No</td>\n",142 " <td>No</td>\n",143 " <td>Month-to-month</td>\n",144 " <td>Yes</td>\n",145 " <td>Mailed check</td>\n",146 " <td>53.85</td>\n",147 " <td>108.15</td>\n",148 " <td>Yes</td>\n",149 " </tr>\n",150 " <tr>\n",151 " <th>3</th>\n",152 " <td>7795-CFOCW</td>\n",153 " <td>Male</td>\n",154 " <td>0</td>\n",155 " <td>No</td>\n",156 " <td>No</td>\n",157 " <td>45</td>\n",158 " <td>No</td>\n",159 " <td>No phone service</td>\n",160 " <td>DSL</td>\n",161 " <td>Yes</td>\n",162 " <td>...</td>\n",163 " <td>Yes</td>\n",164 " <td>Yes</td>\n",165 " <td>No</td>\n",166 " <td>No</td>\n",167 " <td>One year</td>\n",168 " <td>No</td>\n",169 " <td>Bank transfer (automatic)</td>\n",170 " <td>42.30</td>\n",171 " <td>1840.75</td>\n",172 " <td>No</td>\n",173 " </tr>\n",174 " <tr>\n",175 " <th>4</th>\n",176 " <td>9237-HQITU</td>\n",177 " <td>Female</td>\n",178 " <td>0</td>\n",179 " <td>No</td>\n",180 " <td>No</td>\n",181 " <td>2</td>\n",182 " <td>Yes</td>\n",183 " <td>No</td>\n",184 " <td>Fiber optic</td>\n",185 " <td>No</td>\n",186 " <td>...</td>\n",187 " <td>No</td>\n",188 " <td>No</td>\n",189 " <td>No</td>\n",190 " <td>No</td>\n",191 " <td>Month-to-month</td>\n",192 " <td>Yes</td>\n",193 " <td>Electronic check</td>\n",194 " <td>70.70</td>\n",195 " <td>151.65</td>\n",196 " <td>Yes</td>\n",197 " </tr>\n",198 " <tr>\n",199 " <th>...</th>\n",200 " <td>...</td>\n",201 " <td>...</td>\n",202 " <td>...</td>\n",203 " <td>...</td>\n",204 " <td>...</td>\n",205 " <td>...</td>\n",206 " <td>...</td>\n",207 " <td>...</td>\n",208 " <td>...</td>\n",209 " <td>...</td>\n",210 " <td>...</td>\n",211 " <td>...</td>\n",212 " <td>...</td>\n",213 " <td>...</td>\n",214 " <td>...</td>\n",215 " <td>...</td>\n",216 " <td>...</td>\n",217 " <td>...</td>\n",218 " <td>...</td>\n",219 " <td>...</td>\n",220 " <td>...</td>\n",221 " </tr>\n",222 " <tr>\n",223 " <th>7038</th>\n",224 " <td>6840-RESVB</td>\n",225 " <td>Male</td>\n",226 " <td>0</td>\n",227 " <td>Yes</td>\n",228 " <td>Yes</td>\n",229 " <td>24</td>\n",230 " <td>Yes</td>\n",231 " <td>Yes</td>\n",232 " <td>DSL</td>\n",233 " <td>Yes</td>\n",234 " <td>...</td>\n",235 " <td>Yes</td>\n",236 " <td>Yes</td>\n",237 " <td>Yes</td>\n",238 " <td>Yes</td>\n",239 " <td>One year</td>\n",240 " <td>Yes</td>\n",241 " <td>Mailed check</td>\n",242 " <td>84.80</td>\n",243 " <td>1990.5</td>\n",244 " <td>No</td>\n",245 " </tr>\n",246 " <tr>\n",247 " <th>7039</th>\n",248 " <td>2234-XADUH</td>\n",249 " <td>Female</td>\n",250 " <td>0</td>\n",251 " <td>Yes</td>\n",252 " <td>Yes</td>\n",253 " <td>72</td>\n",254 " <td>Yes</td>\n",255 " <td>Yes</td>\n",256 " <td>Fiber optic</td>\n",257 " <td>No</td>\n",258 " <td>...</td>\n",259 " <td>Yes</td>\n",260 " <td>No</td>\n",261 " <td>Yes</td>\n",262 " <td>Yes</td>\n",263 " <td>One year</td>\n",264 " <td>Yes</td>\n",265 " <td>Credit card (automatic)</td>\n",266 " <td>103.20</td>\n",267 " <td>7362.9</td>\n",268 " <td>No</td>\n",269 " </tr>\n",270 " <tr>\n",271 " <th>7040</th>\n",272 " <td>4801-JZAZL</td>\n",273 " <td>Female</td>\n",274 " <td>0</td>\n",275 " <td>Yes</td>\n",276 " <td>Yes</td>\n",277 " <td>11</td>\n",278 " <td>No</td>\n",279 " <td>No phone service</td>\n",280 " <td>DSL</td>\n",281 " <td>Yes</td>\n",282 " <td>...</td>\n",283 " <td>No</td>\n",284 " <td>No</td>\n",285 " <td>No</td>\n",286 " <td>No</td>\n",287 " <td>Month-to-month</td>\n",288 " <td>Yes</td>\n",289 " <td>Electronic check</td>\n",290 " <td>29.60</td>\n",291 " <td>346.45</td>\n",292 " <td>No</td>\n",293 " </tr>\n",294 " <tr>\n",295 " <th>7041</th>\n",296 " <td>8361-LTMKD</td>\n",297 " <td>Male</td>\n",298 " <td>1</td>\n",299 " <td>Yes</td>\n",300 " <td>No</td>\n",301 " <td>4</td>\n",302 " <td>Yes</td>\n",303 " <td>Yes</td>\n",304 " <td>Fiber optic</td>\n",305 " <td>No</td>\n",306 " <td>...</td>\n",307 " <td>No</td>\n",308 " <td>No</td>\n",309 " <td>No</td>\n",310 " <td>No</td>\n",311 " <td>Month-to-month</td>\n",312 " <td>Yes</td>\n",313 " <td>Mailed check</td>\n",314 " <td>74.40</td>\n",315 " <td>306.6</td>\n",316 " <td>Yes</td>\n",317 " </tr>\n",318 " <tr>\n",319 " <th>7042</th>\n",320 " <td>3186-AJIEK</td>\n",321 " <td>Male</td>\n",322 " <td>0</td>\n",323 " <td>No</td>\n",324 " <td>No</td>\n",325 " <td>66</td>\n",326 " <td>Yes</td>\n",327 " <td>No</td>\n",328 " <td>Fiber optic</td>\n",329 " <td>Yes</td>\n",330 " <td>...</td>\n",331 " <td>Yes</td>\n",332 " <td>Yes</td>\n",333 " <td>Yes</td>\n",334 " <td>Yes</td>\n",335 " <td>Two year</td>\n",336 " <td>Yes</td>\n",337 " <td>Bank transfer (automatic)</td>\n",338 " <td>105.65</td>\n",339 " <td>6844.5</td>\n",340 " <td>No</td>\n",341 " </tr>\n",342 " </tbody>\n",343 "</table>\n",344 "<p>7043 rows × 21 columns</p>\n",345 "</div>"346 ],347 "text/plain": [348 " customerID gender SeniorCitizen Partner Dependents tenure \\\n",349 "0 7590-VHVEG Female 0 Yes No 1 \n",350 "1 5575-GNVDE Male 0 No No 34 \n",351 "2 3668-QPYBK Male 0 No No 2 \n",352 "3 7795-CFOCW Male 0 No No 45 \n",353 "4 9237-HQITU Female 0 No No 2 \n",354 "... ... ... ... ... ... ... \n",355 "7038 6840-RESVB Male 0 Yes Yes 24 \n",356 "7039 2234-XADUH Female 0 Yes Yes 72 \n",357 "7040 4801-JZAZL Female 0 Yes Yes 11 \n",358 "7041 8361-LTMKD Male 1 Yes No 4 \n",359 "7042 3186-AJIEK Male 0 No No 66 \n",360 "\n",361 " PhoneService MultipleLines InternetService OnlineSecurity ... \\\n",362 "0 No No phone service DSL No ... \n",363 "1 Yes No DSL Yes ... \n",364 "2 Yes No DSL Yes ... \n",365 "3 No No phone service DSL Yes ... \n",366 "4 Yes No Fiber optic No ... \n",367 "... ... ... ... ... ... \n",368 "7038 Yes Yes DSL Yes ... \n",369 "7039 Yes Yes Fiber optic No ... \n",370 "7040 No No phone service DSL Yes ... \n",371 "7041 Yes Yes Fiber optic No ... \n",372 "7042 Yes No Fiber optic Yes ... \n",373 "\n",374 " DeviceProtection TechSupport StreamingTV StreamingMovies Contract \\\n",375 "0 No No No No Month-to-month \n",376 "1 Yes No No No One year \n",377 "2 No No No No Month-to-month \n",378 "3 Yes Yes No No One year \n",379 "4 No No No No Month-to-month \n",380 "... ... ... ... ... ... \n",381 "7038 Yes Yes Yes Yes One year \n",382 "7039 Yes No Yes Yes One year \n",383 "7040 No No No No Month-to-month \n",384 "7041 No No No No Month-to-month \n",385 "7042 Yes Yes Yes Yes Two year \n",386 "\n",387 " PaperlessBilling PaymentMethod MonthlyCharges TotalCharges \\\n",388 "0 Yes Electronic check 29.85 29.85 \n",389 "1 No Mailed check 56.95 1889.5 \n",390 "2 Yes Mailed check 53.85 108.15 \n",391 "3 No Bank transfer (automatic) 42.30 1840.75 \n",392 "4 Yes Electronic check 70.70 151.65 \n",393 "... ... ... ... ... \n",394 "7038 Yes Mailed check 84.80 1990.5 \n",395 "7039 Yes Credit card (automatic) 103.20 7362.9 \n",396 "7040 Yes Electronic check 29.60 346.45 \n",397 "7041 Yes Mailed check 74.40 306.6 \n",398 "7042 Yes Bank transfer (automatic) 105.65 6844.5 \n",399 "\n",400 " Churn \n",401 "0 No \n",402 "1 No \n",403 "2 Yes \n",404 "3 No \n",405 "4 Yes \n",406 "... ... \n",407 "7038 No \n",408 "7039 No \n",409 "7040 No \n",410 "7041 Yes \n",411 "7042 No \n",412 "\n",413 "[7043 rows x 21 columns]"414 ]415 },416 "execution_count": 2,417 "metadata": {},418 "output_type": "execute_result"419 }420 ],421 "source": [422 "# Load dataset\n",423 "df = pd.read_csv(\"Customer-Churn.csv\")\n",424 "df"425 ]426 },427 {428 "cell_type": "code",429 "execution_count": 3,430 "id": "b488707c-5a62-462f-90f4-a9becbba65d5",431 "metadata": {},432 "outputs": [],433 "source": [434 "\n",435 "\n",436 "# Remove customer ID (not useful for prediction)\n",437 "df.drop(columns=[\"customerID\"], inplace=True)\n",438 "\n",439 "# Convert TotalCharges to numeric (some values are blank strings)\n",440 "df[\"TotalCharges\"] = pd.to_numeric(df[\"TotalCharges\"], errors=\"coerce\")\n",441 "\n",442 "# Remove rows with missing values\n",443 "df.dropna(inplace=True)\n"444 ]445 },446 {447 "cell_type": "code",448 "execution_count": 4,449 "id": "0e35b975-62e0-45de-9b11-498dd47fea90",450 "metadata": {},451 "outputs": [],452 "source": [453 "# Target variable: Churn (Yes → 1, No → 0)\n",454 "y = df[\"Churn\"].map({\"Yes\": 1, \"No\": 0})\n",455 "\n",456 "# Feature set\n",457 "X = df.drop(columns=[\"Churn\"])\n"458 ]459 },460 {461 "cell_type": "code",462 "execution_count": 5,463 "id": "6d0ffaa6-44ef-4c1e-9355-f110496ff58b",464 "metadata": {},465 "outputs": [],466 "source": [467 "# Identify categorical and numerical columns\n",468 "categorical_cols = X.select_dtypes(include=\"object\").columns\n",469 "numerical_cols = X.select_dtypes(exclude=\"object\").columns\n",470 "\n",471 "# One-hot encode categorical features\n",472 "ohe = OneHotEncoder(sparse_output=False, handle_unknown=\"ignore\")\n",473 "\n",474 "encoded_cat = ohe.fit_transform(X[categorical_cols])\n",475 "\n",476 "encoded_cat_df = pd.DataFrame(\n",477 " encoded_cat,\n",478 " columns=ohe.get_feature_names_out(categorical_cols),\n",479 " index=X.index\n",480 ")\n",481 "\n",482 "# Combine numerical + encoded categorical features\n",483 "X_final = pd.concat([X[numerical_cols], encoded_cat_df], axis=1)\n"484 ]485 },486 {487 "cell_type": "code",488 "execution_count": 6,489 "id": "855ad304-3c17-4451-af5f-8e9819225da7",490 "metadata": {491 "scrolled": true492 },493 "outputs": [494 {495 "data": {496 "text/html": [497 "<div>\n",498 "<style scoped>\n",499 " .dataframe tbody tr th:only-of-type {\n",500 " vertical-align: middle;\n",501 " }\n",502 "\n",503 " .dataframe tbody tr th {\n",504 " vertical-align: top;\n",505 " }\n",506 "\n",507 " .dataframe thead th {\n",508 " text-align: right;\n",509 " }\n",510 "</style>\n",511 "<table border=\"1\" class=\"dataframe\">\n",512 " <thead>\n",513 " <tr style=\"text-align: right;\">\n",514 " <th></th>\n",515 " <th>SeniorCitizen</th>\n",516 " <th>tenure</th>\n",517 " <th>MonthlyCharges</th>\n",518 " <th>TotalCharges</th>\n",519 " <th>gender_Female</th>\n",520 " <th>gender_Male</th>\n",521 " <th>Partner_No</th>\n",522 " <th>Partner_Yes</th>\n",523 " <th>Dependents_No</th>\n",524 " <th>Dependents_Yes</th>\n",525 " <th>...</th>\n",526 " <th>StreamingMovies_Yes</th>\n",527 " <th>Contract_Month-to-month</th>\n",528 " <th>Contract_One year</th>\n",529 " <th>Contract_Two year</th>\n",530 " <th>PaperlessBilling_No</th>\n",531 " <th>PaperlessBilling_Yes</th>\n",532 " <th>PaymentMethod_Bank transfer (automatic)</th>\n",533 " <th>PaymentMethod_Credit card (automatic)</th>\n",534 " <th>PaymentMethod_Electronic check</th>\n",535 " <th>PaymentMethod_Mailed check</th>\n",536 " </tr>\n",537 " </thead>\n",538 " <tbody>\n",539 " <tr>\n",540 " <th>0</th>\n",541 " <td>0</td>\n",542 " <td>1</td>\n",543 " <td>29.85</td>\n",544 " <td>29.85</td>\n",545 " <td>1.0</td>\n",546 " <td>0.0</td>\n",547 " <td>0.0</td>\n",548 " <td>1.0</td>\n",549 " <td>1.0</td>\n",550 " <td>0.0</td>\n",551 " <td>...</td>\n",552 " <td>0.0</td>\n",553 " <td>1.0</td>\n",554 " <td>0.0</td>\n",555 " <td>0.0</td>\n",556 " <td>0.0</td>\n",557 " <td>1.0</td>\n",558 " <td>0.0</td>\n",559 " <td>0.0</td>\n",560 " <td>1.0</td>\n",561 " <td>0.0</td>\n",562 " </tr>\n",563 " <tr>\n",564 " <th>1</th>\n",565 " <td>0</td>\n",566 " <td>34</td>\n",567 " <td>56.95</td>\n",568 " <td>1889.50</td>\n",569 " <td>0.0</td>\n",570 " <td>1.0</td>\n",571 " <td>1.0</td>\n",572 " <td>0.0</td>\n",573 " <td>1.0</td>\n",574 " <td>0.0</td>\n",575 " <td>...</td>\n",576 " <td>0.0</td>\n",577 " <td>0.0</td>\n",578 " <td>1.0</td>\n",579 " <td>0.0</td>\n",580 " <td>1.0</td>\n",581 " <td>0.0</td>\n",582 " <td>0.0</td>\n",583 " <td>0.0</td>\n",584 " <td>0.0</td>\n",585 " <td>1.0</td>\n",586 " </tr>\n",587 " <tr>\n",588 " <th>2</th>\n",589 " <td>0</td>\n",590 " <td>2</td>\n",591 " <td>53.85</td>\n",592 " <td>108.15</td>\n",593 " <td>0.0</td>\n",594 " <td>1.0</td>\n",595 " <td>1.0</td>\n",596 " <td>0.0</td>\n",597 " <td>1.0</td>\n",598 " <td>0.0</td>\n",599 " <td>...</td>\n",600 " <td>0.0</td>\n",601 " <td>1.0</td>\n",602 " <td>0.0</td>\n",603 " <td>0.0</td>\n",604 " <td>0.0</td>\n",605 " <td>1.0</td>\n",606 " <td>0.0</td>\n",607 " <td>0.0</td>\n",608 " <td>0.0</td>\n",609 " <td>1.0</td>\n",610 " </tr>\n",611 " <tr>\n",612 " <th>3</th>\n",613 " <td>0</td>\n",614 " <td>45</td>\n",615 " <td>42.30</td>\n",616 " <td>1840.75</td>\n",617 " <td>0.0</td>\n",618 " <td>1.0</td>\n",619 " <td>1.0</td>\n",620 " <td>0.0</td>\n",621 " <td>1.0</td>\n",622 " <td>0.0</td>\n",623 " <td>...</td>\n",624 " <td>0.0</td>\n",625 " <td>0.0</td>\n",626 " <td>1.0</td>\n",627 " <td>0.0</td>\n",628 " <td>1.0</td>\n",629 " <td>0.0</td>\n",630 " <td>1.0</td>\n",631 " <td>0.0</td>\n",632 " <td>0.0</td>\n",633 " <td>0.0</td>\n",634 " </tr>\n",635 " <tr>\n",636 " <th>4</th>\n",637 " <td>0</td>\n",638 " <td>2</td>\n",639 " <td>70.70</td>\n",640 " <td>151.65</td>\n",641 " <td>1.0</td>\n",642 " <td>0.0</td>\n",643 " <td>1.0</td>\n",644 " <td>0.0</td>\n",645 " <td>1.0</td>\n",646 " <td>0.0</td>\n",647 " <td>...</td>\n",648 " <td>0.0</td>\n",649 " <td>1.0</td>\n",650 " <td>0.0</td>\n",651 " <td>0.0</td>\n",652 " <td>0.0</td>\n",653 " <td>1.0</td>\n",654 " <td>0.0</td>\n",655 " <td>0.0</td>\n",656 " <td>1.0</td>\n",657 " <td>0.0</td>\n",658 " </tr>\n",659 " <tr>\n",660 " <th>...</th>\n",661 " <td>...</td>\n",662 " <td>...</td>\n",663 " <td>...</td>\n",664 " <td>...</td>\n",665 " <td>...</td>\n",666 " <td>...</td>\n",667 " <td>...</td>\n",668 " <td>...</td>\n",669 " <td>...</td>\n",670 " <td>...</td>\n",671 " <td>...</td>\n",672 " <td>...</td>\n",673 " <td>...</td>\n",674 " <td>...</td>\n",675 " <td>...</td>\n",676 " <td>...</td>\n",677 " <td>...</td>\n",678 " <td>...</td>\n",679 " <td>...</td>\n",680 " <td>...</td>\n",681 " <td>...</td>\n",682 " </tr>\n",683 " <tr>\n",684 " <th>7038</th>\n",685 " <td>0</td>\n",686 " <td>24</td>\n",687 " <td>84.80</td>\n",688 " <td>1990.50</td>\n",689 " <td>0.0</td>\n",690 " <td>1.0</td>\n",691 " <td>0.0</td>\n",692 " <td>1.0</td>\n",693 " <td>0.0</td>\n",694 " <td>1.0</td>\n",695 " <td>...</td>\n",696 " <td>1.0</td>\n",697 " <td>0.0</td>\n",698 " <td>1.0</td>\n",699 " <td>0.0</td>\n",700 " <td>0.0</td>\n",701 " <td>1.0</td>\n",702 " <td>0.0</td>\n",703 " <td>0.0</td>\n",704 " <td>0.0</td>\n",705 " <td>1.0</td>\n",706 " </tr>\n",707 " <tr>\n",708 " <th>7039</th>\n",709 " <td>0</td>\n",710 " <td>72</td>\n",711 " <td>103.20</td>\n",712 " <td>7362.90</td>\n",713 " <td>1.0</td>\n",714 " <td>0.0</td>\n",715 " <td>0.0</td>\n",716 " <td>1.0</td>\n",717 " <td>0.0</td>\n",718 " <td>1.0</td>\n",719 " <td>...</td>\n",720 " <td>1.0</td>\n",721 " <td>0.0</td>\n",722 " <td>1.0</td>\n",723 " <td>0.0</td>\n",724 " <td>0.0</td>\n",725 " <td>1.0</td>\n",726 " <td>0.0</td>\n",727 " <td>1.0</td>\n",728 " <td>0.0</td>\n",729 " <td>0.0</td>\n",730 " </tr>\n",731 " <tr>\n",732 " <th>7040</th>\n",733 " <td>0</td>\n",734 " <td>11</td>\n",735 " <td>29.60</td>\n",736 " <td>346.45</td>\n",737 " <td>1.0</td>\n",738 " <td>0.0</td>\n",739 " <td>0.0</td>\n",740 " <td>1.0</td>\n",741 " <td>0.0</td>\n",742 " <td>1.0</td>\n",743 " <td>...</td>\n",744 " <td>0.0</td>\n",745 " <td>1.0</td>\n",746 " <td>0.0</td>\n",747 " <td>0.0</td>\n",748 " <td>0.0</td>\n",749 " <td>1.0</td>\n",750 " <td>0.0</td>\n",751 " <td>0.0</td>\n",752 " <td>1.0</td>\n",753 " <td>0.0</td>\n",754 " </tr>\n",755 " <tr>\n",756 " <th>7041</th>\n",757 " <td>1</td>\n",758 " <td>4</td>\n",759 " <td>74.40</td>\n",760 " <td>306.60</td>\n",761 " <td>0.0</td>\n",762 " <td>1.0</td>\n",763 " <td>0.0</td>\n",764 " <td>1.0</td>\n",765 " <td>1.0</td>\n",766 " <td>0.0</td>\n",767 " <td>...</td>\n",768 " <td>0.0</td>\n",769 " <td>1.0</td>\n",770 " <td>0.0</td>\n",771 " <td>0.0</td>\n",772 " <td>0.0</td>\n",773 " <td>1.0</td>\n",774 " <td>0.0</td>\n",775 " <td>0.0</td>\n",776 " <td>0.0</td>\n",777 " <td>1.0</td>\n",778 " </tr>\n",779 " <tr>\n",780 " <th>7042</th>\n",781 " <td>0</td>\n",782 " <td>66</td>\n",783 " <td>105.65</td>\n",784 " <td>6844.50</td>\n",785 " <td>0.0</td>\n",786 " <td>1.0</td>\n",787 " <td>1.0</td>\n",788 " <td>0.0</td>\n",789 " <td>1.0</td>\n",790 " <td>0.0</td>\n",791 " <td>...</td>\n",792 " <td>1.0</td>\n",793 " <td>0.0</td>\n",794 " <td>0.0</td>\n",795 " <td>1.0</td>\n",796 " <td>0.0</td>\n",797 " <td>1.0</td>\n",798 " <td>1.0</td>\n",799 " <td>0.0</td>\n",800 " <td>0.0</td>\n",801 " <td>0.0</td>\n",802 " </tr>\n",803 " </tbody>\n",804 "</table>\n",805 "<p>7032 rows × 45 columns</p>\n",806 "</div>"807 ],808 "text/plain": [809 " SeniorCitizen tenure MonthlyCharges TotalCharges gender_Female \\\n",810 "0 0 1 29.85 29.85 1.0 \n",811 "1 0 34 56.95 1889.50 0.0 \n",812 "2 0 2 53.85 108.15 0.0 \n",813 "3 0 45 42.30 1840.75 0.0 \n",814 "4 0 2 70.70 151.65 1.0 \n",815 "... ... ... ... ... ... \n",816 "7038 0 24 84.80 1990.50 0.0 \n",817 "7039 0 72 103.20 7362.90 1.0 \n",818 "7040 0 11 29.60 346.45 1.0 \n",819 "7041 1 4 74.40 306.60 0.0 \n",820 "7042 0 66 105.65 6844.50 0.0 \n",821 "\n",822 " gender_Male Partner_No Partner_Yes Dependents_No Dependents_Yes \\\n",823 "0 0.0 0.0 1.0 1.0 0.0 \n",824 "1 1.0 1.0 0.0 1.0 0.0 \n",825 "2 1.0 1.0 0.0 1.0 0.0 \n",826 "3 1.0 1.0 0.0 1.0 0.0 \n",827 "4 0.0 1.0 0.0 1.0 0.0 \n",828 "... ... ... ... ... ... \n",829 "7038 1.0 0.0 1.0 0.0 1.0 \n",830 "7039 0.0 0.0 1.0 0.0 1.0 \n",831 "7040 0.0 0.0 1.0 0.0 1.0 \n",832 "7041 1.0 0.0 1.0 1.0 0.0 \n",833 "7042 1.0 1.0 0.0 1.0 0.0 \n",834 "\n",835 " ... StreamingMovies_Yes Contract_Month-to-month Contract_One year \\\n",836 "0 ... 0.0 1.0 0.0 \n",837 "1 ... 0.0 0.0 1.0 \n",838 "2 ... 0.0 1.0 0.0 \n",839 "3 ... 0.0 0.0 1.0 \n",840 "4 ... 0.0 1.0 0.0 \n",841 "... ... ... ... ... \n",842 "7038 ... 1.0 0.0 1.0 \n",843 "7039 ... 1.0 0.0 1.0 \n",844 "7040 ... 0.0 1.0 0.0 \n",845 "7041 ... 0.0 1.0 0.0 \n",846 "7042 ... 1.0 0.0 0.0 \n",847 "\n",848 " Contract_Two year PaperlessBilling_No PaperlessBilling_Yes \\\n",849 "0 0.0 0.0 1.0 \n",850 "1 0.0 1.0 0.0 \n",851 "2 0.0 0.0 1.0 \n",852 "3 0.0 1.0 0.0 \n",853 "4 0.0 0.0 1.0 \n",854 "... ... ... ... \n",855 "7038 0.0 0.0 1.0 \n",856 "7039 0.0 0.0 1.0 \n",857 "7040 0.0 0.0 1.0 \n",858 "7041 0.0 0.0 1.0 \n",859 "7042 1.0 0.0 1.0 \n",860 "\n",861 " PaymentMethod_Bank transfer (automatic) \\\n",862 "0 0.0 \n",863 "1 0.0 \n",864 "2 0.0 \n",865 "3 1.0 \n",866 "4 0.0 \n",867 "... ... \n",868 "7038 0.0 \n",869 "7039 0.0 \n",870 "7040 0.0 \n",871 "7041 0.0 \n",872 "7042 1.0 \n",873 "\n",874 " PaymentMethod_Credit card (automatic) PaymentMethod_Electronic check \\\n",875 "0 0.0 1.0 \n",876 "1 0.0 0.0 \n",877 "2 0.0 0.0 \n",878 "3 0.0 0.0 \n",879 "4 0.0 1.0 \n",880 "... ... ... \n",881 "7038 0.0 0.0 \n",882 "7039 1.0 0.0 \n",883 "7040 0.0 1.0 \n",884 "7041 0.0 0.0 \n",885 "7042 0.0 0.0 \n",886 "\n",887 " PaymentMethod_Mailed check \n",888 "0 0.0 \n",889 "1 1.0 \n",890 "2 1.0 \n",891 "3 0.0 \n",892 "4 0.0 \n",893 "... ... \n",894 "7038 1.0 \n",895 "7039 0.0 \n",896 "7040 0.0 \n",897 "7041 1.0 \n",898 "7042 0.0 \n",899 "\n",900 "[7032 rows x 45 columns]"901 ]902 },903 "execution_count": 6,904 "metadata": {},905 "output_type": "execute_result"906 }907 ],908 "source": [909 "X_final"910 ]911 },912 {913 "cell_type": "code",914 "execution_count": 7,915 "id": "d0bd3450-515d-42cd-bfc4-8b3a88e35b03",916 "metadata": {},917 "outputs": [],918 "source": [919 "X_train, X_test, y_train, y_test = train_test_split(\n",920 " X_final,\n",921 " y,\n",922 " test_size=0.2,\n",923 " random_state=42,\n",924 " stratify=y\n",925 ")\n"926 ]927 },928 {929 "cell_type": "code",930 "execution_count": 8,931 "id": "a6560d55-bea2-4778-b787-fb924c008b5c",932 "metadata": {},933 "outputs": [934 {935 "data": {936 "text/html": [937 "<style>#sk-container-id-1 {\n",938 " /* Definition of color scheme common for light and dark mode */\n",939 " --sklearn-color-text: black;\n",940 " --sklearn-color-line: gray;\n",941 " /* Definition of color scheme for unfitted estimators */\n",942 " --sklearn-color-unfitted-level-0: #fff5e6;\n",943 " --sklearn-color-unfitted-level-1: #f6e4d2;\n",944 " --sklearn-color-unfitted-level-2: #ffe0b3;\n",945 " --sklearn-color-unfitted-level-3: chocolate;\n",946 " /* Definition of color scheme for fitted estimators */\n",947 " --sklearn-color-fitted-level-0: #f0f8ff;\n",948 " --sklearn-color-fitted-level-1: #d4ebff;\n",949 " --sklearn-color-fitted-level-2: #b3dbfd;\n",950 " --sklearn-color-fitted-level-3: cornflowerblue;\n",951 "\n",952 " /* Specific color for light theme */\n",953 " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",954 " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",955 " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",956 " --sklearn-color-icon: #696969;\n",957 "\n",958 " @media (prefers-color-scheme: dark) {\n",959 " /* Redefinition of color scheme for dark theme */\n",960 " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",961 " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",962 " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",963 " --sklearn-color-icon: #878787;\n",964 " }\n",965 "}\n",966 "\n",967 "#sk-container-id-1 {\n",968 " color: var(--sklearn-color-text);\n",969 "}\n",970 "\n",971 "#sk-container-id-1 pre {\n",972 " padding: 0;\n",973 "}\n",974 "\n",975 "#sk-container-id-1 input.sk-hidden--visually {\n",976 " border: 0;\n",977 " clip: rect(1px 1px 1px 1px);\n",978 " clip: rect(1px, 1px, 1px, 1px);\n",979 " height: 1px;\n",980 " margin: -1px;\n",981 " overflow: hidden;\n",982 " padding: 0;\n",983 " position: absolute;\n",984 " width: 1px;\n",985 "}\n",986 "\n",987 "#sk-container-id-1 div.sk-dashed-wrapped {\n",988 " border: 1px dashed var(--sklearn-color-line);\n",989 " margin: 0 0.4em 0.5em 0.4em;\n",990 " box-sizing: border-box;\n",991 " padding-bottom: 0.4em;\n",992 " background-color: var(--sklearn-color-background);\n",993 "}\n",994 "\n",995 "#sk-container-id-1 div.sk-container {\n",996 " /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",997 " but bootstrap.min.css set `[hidden] { display: none !important; }`\n",998 " so we also need the `!important` here to be able to override the\n",999 " default hidden behavior on the sphinx rendered scikit-learn.org.\n",1000 " See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",1001 " display: inline-block !important;\n",1002 " position: relative;\n",1003 "}\n",1004 "\n",1005 "#sk-container-id-1 div.sk-text-repr-fallback {\n",1006 " display: none;\n",1007 "}\n",1008 "\n",1009 "div.sk-parallel-item,\n",1010 "div.sk-serial,\n",1011 "div.sk-item {\n",1012 " /* draw centered vertical line to link estimators */\n",1013 " background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",1014 " background-size: 2px 100%;\n",1015 " background-repeat: no-repeat;\n",1016 " background-position: center center;\n",1017 "}\n",1018 "\n",1019 "/* Parallel-specific style estimator block */\n",1020 "\n",1021 "#sk-container-id-1 div.sk-parallel-item::after {\n",1022 " content: \"\";\n",1023 " width: 100%;\n",1024 " border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",1025 " flex-grow: 1;\n",1026 "}\n",1027 "\n",1028 "#sk-container-id-1 div.sk-parallel {\n",1029 " display: flex;\n",1030 " align-items: stretch;\n",1031 " justify-content: center;\n",1032 " background-color: var(--sklearn-color-background);\n",1033 " position: relative;\n",1034 "}\n",1035 "\n",1036 "#sk-container-id-1 div.sk-parallel-item {\n",1037 " display: flex;\n",1038 " flex-direction: column;\n",1039 "}\n",1040 "\n",1041 "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",1042 " align-self: flex-end;\n",1043 " width: 50%;\n",1044 "}\n",1045 "\n",1046 "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",1047 " align-self: flex-start;\n",1048 " width: 50%;\n",1049 "}\n",1050 "\n",1051 "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",1052 " width: 0;\n",1053 "}\n",1054 "\n",1055 "/* Serial-specific style estimator block */\n",1056 "\n",1057 "#sk-container-id-1 div.sk-serial {\n",1058 " display: flex;\n",1059 " flex-direction: column;\n",1060 " align-items: center;\n",1061 " background-color: var(--sklearn-color-background);\n",1062 " padding-right: 1em;\n",1063 " padding-left: 1em;\n",1064 "}\n",1065 "\n",1066 "\n",1067 "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",1068 "clickable and can be expanded/collapsed.\n",1069 "- Pipeline and ColumnTransformer use this feature and define the default style\n",1070 "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",1071 "*/\n",1072 "\n",1073 "/* Pipeline and ColumnTransformer style (default) */\n",1074 "\n",1075 "#sk-container-id-1 div.sk-toggleable {\n",1076 " /* Default theme specific background. It is overwritten whether we have a\n",1077 " specific estimator or a Pipeline/ColumnTransformer */\n",1078 " background-color: var(--sklearn-color-background);\n",1079 "}\n",1080 "\n",1081 "/* Toggleable label */\n",1082 "#sk-container-id-1 label.sk-toggleable__label {\n",1083 " cursor: pointer;\n",1084 " display: block;\n",1085 " width: 100%;\n",1086 " margin-bottom: 0;\n",1087 " padding: 0.5em;\n",1088 " box-sizing: border-box;\n",1089 " text-align: center;\n",1090 "}\n",1091 "\n",1092 "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",1093 " /* Arrow on the left of the label */\n",1094 " content: \"▸\";\n",1095 " float: left;\n",1096 " margin-right: 0.25em;\n",1097 " color: var(--sklearn-color-icon);\n",1098 "}\n",1099 "\n",1100 "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",1101 " color: var(--sklearn-color-text);\n",1102 "}\n",1103 "\n",1104 "/* Toggleable content - dropdown */\n",1105 "\n",1106 "#sk-container-id-1 div.sk-toggleable__content {\n",1107 " max-height: 0;\n",1108 " max-width: 0;\n",1109 " overflow: hidden;\n",1110 " text-align: left;\n",1111 " /* unfitted */\n",1112 " background-color: var(--sklearn-color-unfitted-level-0);\n",1113 "}\n",1114 "\n",1115 "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",1116 " /* fitted */\n",1117 " background-color: var(--sklearn-color-fitted-level-0);\n",1118 "}\n",1119 "\n",1120 "#sk-container-id-1 div.sk-toggleable__content pre {\n",1121 " margin: 0.2em;\n",1122 " border-radius: 0.25em;\n",1123 " color: var(--sklearn-color-text);\n",1124 " /* unfitted */\n",1125 " background-color: var(--sklearn-color-unfitted-level-0);\n",1126 "}\n",1127 "\n",1128 "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",1129 " /* unfitted */\n",1130 " background-color: var(--sklearn-color-fitted-level-0);\n",1131 "}\n",1132 "\n",1133 "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",1134 " /* Expand drop-down */\n",1135 " max-height: 200px;\n",1136 " max-width: 100%;\n",1137 " overflow: auto;\n",1138 "}\n",1139 "\n",1140 "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",1141 " content: \"▾\";\n",1142 "}\n",1143 "\n",1144 "/* Pipeline/ColumnTransformer-specific style */\n",1145 "\n",1146 "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",1147 " color: var(--sklearn-color-text);\n",1148 " background-color: var(--sklearn-color-unfitted-level-2);\n",1149 "}\n",1150 "\n",1151 "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",1152 " background-color: var(--sklearn-color-fitted-level-2);\n",1153 "}\n",1154 "\n",1155 "/* Estimator-specific style */\n",1156 "\n",1157 "/* Colorize estimator box */\n",1158 "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",1159 " /* unfitted */\n",1160 " background-color: var(--sklearn-color-unfitted-level-2);\n",1161 "}\n",1162 "\n",1163 "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",1164 " /* fitted */\n",1165 " background-color: var(--sklearn-color-fitted-level-2);\n",1166 "}\n",1167 "\n",1168 "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",1169 "#sk-container-id-1 div.sk-label label {\n",1170 " /* The background is the default theme color */\n",1171 " color: var(--sklearn-color-text-on-default-background);\n",1172 "}\n",1173 "\n",1174 "/* On hover, darken the color of the background */\n",1175 "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",1176 " color: var(--sklearn-color-text);\n",1177 " background-color: var(--sklearn-color-unfitted-level-2);\n",1178 "}\n",1179 "\n",1180 "/* Label box, darken color on hover, fitted */\n",1181 "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",1182 " color: var(--sklearn-color-text);\n",1183 " background-color: var(--sklearn-color-fitted-level-2);\n",1184 "}\n",1185 "\n",1186 "/* Estimator label */\n",1187 "\n",1188 "#sk-container-id-1 div.sk-label label {\n",1189 " font-family: monospace;\n",1190 " font-weight: bold;\n",1191 " display: inline-block;\n",1192 " line-height: 1.2em;\n",1193 "}\n",1194 "\n",1195 "#sk-container-id-1 div.sk-label-container {\n",1196 " text-align: center;\n",1197 "}\n",1198 "\n",1199 "/* Estimator-specific */\n",1200 "#sk-container-id-1 div.sk-estimator {\n",