{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "K1JbMys043S_", "outputId": "c60da538-342e-4d5d-b678-0acdcf4f1976" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mounted at /content/drive\n" ] } ], "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ] }, { "cell_type": "code", "source": [ "!pip install gradio" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IbZYtX6k45IY", "outputId": "c9221d56-1854-47ca-e1b9-41e4ee641859" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting gradio\n", " Downloading gradio-3.36.1-py3-none-any.whl (19.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.8/19.8 MB\u001b[0m \u001b[31m55.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting aiofiles (from gradio)\n", " Downloading aiofiles-23.1.0-py3-none-any.whl (14 kB)\n", "Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from gradio) (3.8.4)\n", "Requirement already satisfied: altair>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.2.2)\n", "Collecting fastapi (from gradio)\n", " Downloading fastapi-0.100.0-py3-none-any.whl (65 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m65.7/65.7 kB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting ffmpy (from gradio)\n", " Downloading ffmpy-0.3.0.tar.gz (4.8 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Collecting gradio-client>=0.2.7 (from gradio)\n", " Downloading gradio_client-0.2.7-py3-none-any.whl (288 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m288.4/288.4 kB\u001b[0m \u001b[31m28.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting httpx (from gradio)\n", " Downloading httpx-0.24.1-py3-none-any.whl (75 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.4/75.4 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting huggingface-hub>=0.14.0 (from gradio)\n", " Downloading huggingface_hub-0.16.4-py3-none-any.whl (268 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m21.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.2)\n", "Requirement already satisfied: markdown-it-py[linkify]>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.0.0)\n", "Requirement already satisfied: markupsafe in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.3)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n", "Collecting mdit-py-plugins<=0.3.3 (from gradio)\n", " Downloading mdit_py_plugins-0.3.3-py3-none-any.whl (50 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.5/50.5 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from gradio) (1.22.4)\n", "Collecting orjson (from gradio)\n", " Downloading orjson-3.9.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (138 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m138.7/138.7 kB\u001b[0m \u001b[31m16.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from gradio) (1.5.3)\n", "Requirement already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (from gradio) (8.4.0)\n", "Requirement already satisfied: pydantic in /usr/local/lib/python3.10/dist-packages (from gradio) (1.10.9)\n", "Collecting pydub (from gradio)\n", " Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", "Requirement already satisfied: pygments>=2.12.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.14.0)\n", "Collecting python-multipart (from gradio)\n", " Downloading python_multipart-0.0.6-py3-none-any.whl (45 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.7/45.7 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from gradio) (2.27.1)\n", "Collecting semantic-version (from gradio)\n", " Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n", "Collecting uvicorn>=0.14.0 (from gradio)\n", " Downloading uvicorn-0.22.0-py3-none-any.whl (58 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting websockets>=10.0 (from gradio)\n", " Downloading websockets-11.0.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (129 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.9/129.9 kB\u001b[0m \u001b[31m15.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair>=4.2.0->gradio) (0.4)\n", "Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair>=4.2.0->gradio) (4.3.3)\n", "Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair>=4.2.0->gradio) (0.12.0)\n", "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client>=0.2.7->gradio) (2023.6.0)\n", "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio-client>=0.2.7->gradio) (23.1)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from gradio-client>=0.2.7->gradio) (4.6.3)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.14.0->gradio) (3.12.2)\n", "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.14.0->gradio) (4.65.0)\n", "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py[linkify]>=2.0.0->gradio) (0.1.2)\n", "Collecting linkify-it-py<3,>=1 (from markdown-it-py[linkify]>=2.0.0->gradio)\n", " Downloading linkify_it_py-2.0.2-py3-none-any.whl (19 kB)\n", "INFO: pip is looking at multiple versions of mdit-py-plugins to determine which version is compatible with other requirements. This could take a while.\n", "Collecting mdit-py-plugins<=0.3.3 (from gradio)\n", " Downloading mdit_py_plugins-0.3.2-py3-none-any.whl (50 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.4/50.4 kB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Downloading mdit_py_plugins-0.3.1-py3-none-any.whl (46 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.5/46.5 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Downloading mdit_py_plugins-0.3.0-py3-none-any.whl (43 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.7/43.7 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Downloading mdit_py_plugins-0.2.8-py3-none-any.whl (41 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.0/41.0 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Downloading mdit_py_plugins-0.2.7-py3-none-any.whl (41 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.0/41.0 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Downloading mdit_py_plugins-0.2.6-py3-none-any.whl (39 kB)\n", " Downloading mdit_py_plugins-0.2.5-py3-none-any.whl (39 kB)\n", "INFO: pip is looking at multiple versions of mdit-py-plugins to determine which version is compatible with other requirements. This could take a while.\n", " Downloading mdit_py_plugins-0.2.4-py3-none-any.whl (39 kB)\n", " Downloading mdit_py_plugins-0.2.3-py3-none-any.whl (39 kB)\n", " Downloading mdit_py_plugins-0.2.2-py3-none-any.whl (39 kB)\n", " Downloading mdit_py_plugins-0.2.1-py3-none-any.whl (38 kB)\n", " Downloading mdit_py_plugins-0.2.0-py3-none-any.whl (38 kB)\n", "INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce runtime. See https://pip.pypa.io/warnings/backtracking for guidance. If you want to abort this run, press Ctrl + C.\n", " Downloading mdit_py_plugins-0.1.0-py3-none-any.whl (37 kB)\n", "Collecting markdown-it-py[linkify]>=2.0.0 (from gradio)\n", " Downloading markdown_it_py-3.0.0-py3-none-any.whl (87 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m87.5/87.5 kB\u001b[0m \u001b[31m8.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Downloading markdown_it_py-2.2.0-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.5/84.5 kB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas->gradio) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->gradio) (2022.7.1)\n", "Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (8.1.3)\n", "Collecting h11>=0.8 (from uvicorn>=0.14.0->gradio)\n", " Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (23.1.0)\n", "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (2.0.12)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (6.0.4)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (4.0.2)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (1.9.2)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (1.3.3)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->gradio) (1.3.1)\n", "Collecting starlette<0.28.0,>=0.27.0 (from fastapi->gradio)\n", " Downloading starlette-0.27.0-py3-none-any.whl (66 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.0/67.0 kB\u001b[0m \u001b[31m8.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (2023.5.7)\n", "Collecting httpcore<0.18.0,>=0.15.0 (from httpx->gradio)\n", " Downloading httpcore-0.17.3-py3-none-any.whl (74 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m74.5/74.5 kB\u001b[0m \u001b[31m6.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (3.4)\n", "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (1.3.0)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->gradio) (1.1.0)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->gradio) (0.11.0)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->gradio) (4.40.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->gradio) (1.4.4)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->gradio) (3.1.0)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->gradio) (1.26.16)\n", "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from httpcore<0.18.0,>=0.15.0->httpx->gradio) (3.7.0)\n", "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair>=4.2.0->gradio) (0.19.3)\n", "Collecting uc-micro-py (from linkify-it-py<3,>=1->markdown-it-py[linkify]>=2.0.0->gradio)\n", " Downloading uc_micro_py-1.0.2-py3-none-any.whl (6.2 kB)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.1->pandas->gradio) (1.16.0)\n", "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->httpcore<0.18.0,>=0.15.0->httpx->gradio) (1.1.1)\n", "Building wheels for collected packages: ffmpy\n", " Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for ffmpy: filename=ffmpy-0.3.0-py3-none-any.whl size=4694 sha256=833a310a70972f4ea4972633c44808b65fc42559afcf441a716536e3ceddfb1f\n", " Stored in directory: /root/.cache/pip/wheels/0c/c2/0e/3b9c6845c6a4e35beb90910cc70d9ac9ab5d47402bd62af0df\n", "Successfully built ffmpy\n", "Installing collected packages: pydub, ffmpy, websockets, uc-micro-py, semantic-version, python-multipart, orjson, markdown-it-py, h11, aiofiles, uvicorn, starlette, mdit-py-plugins, linkify-it-py, huggingface-hub, httpcore, httpx, fastapi, gradio-client, gradio\n", " Attempting uninstall: markdown-it-py\n", " Found existing installation: markdown-it-py 3.0.0\n", " Uninstalling markdown-it-py-3.0.0:\n", " Successfully uninstalled markdown-it-py-3.0.0\n", "Successfully installed aiofiles-23.1.0 fastapi-0.100.0 ffmpy-0.3.0 gradio-3.36.1 gradio-client-0.2.7 h11-0.14.0 httpcore-0.17.3 httpx-0.24.1 huggingface-hub-0.16.4 linkify-it-py-2.0.2 markdown-it-py-2.2.0 mdit-py-plugins-0.3.3 orjson-3.9.2 pydub-0.25.1 python-multipart-0.0.6 semantic-version-2.10.0 starlette-0.27.0 uc-micro-py-1.0.2 uvicorn-0.22.0 websockets-11.0.3\n" ] } ] }, { "cell_type": "code", "source": [ "import gradio as gr\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n", "from sklearn.ensemble import GradientBoostingClassifier\n", "from sklearn.impute import SimpleImputer\n", "from sklearn.compose import ColumnTransformer\n", "from imblearn.over_sampling import ADASYN\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.model_selection import cross_val_score\n", "from sklearn.metrics import confusion_matrix\n", "from sklearn.metrics import classification_report\n", "from sklearn.metrics import accuracy_score\n", "from sklearn.metrics import roc_auc_score\n", "from sklearn.metrics import roc_curve\n", "from sklearn.metrics import precision_score, recall_score, f1_score\n", "import sklearn.metrics as metrics\n", "\n", "from joblib import dump\n", "import os\n", "import warnings\n", "warnings.filterwarnings('ignore')" ], "metadata": { "id": "DuZ7ZW3E5Ge7" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "c_data = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/Gradio/Telco-Customer-Churn.csv')\n", "c_data.drop(['customerID','PhoneService','SeniorCitizen','StreamingMovies','StreamingTV'], axis=1, inplace=True)\n", "c_data.info()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nDm6h1Pl6tRd", "outputId": "14740557-c7d4-40fa-9efb-e9882d6c42ed" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "RangeIndex: 7043 entries, 0 to 7042\n", "Data columns (total 16 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 gender 7043 non-null object \n", " 1 Partner 7043 non-null object \n", " 2 Dependents 7043 non-null object \n", " 3 tenure 7043 non-null int64 \n", " 4 MultipleLines 7043 non-null object \n", " 5 InternetService 7043 non-null object \n", " 6 OnlineSecurity 7043 non-null object \n", " 7 OnlineBackup 7043 non-null object \n", " 8 DeviceProtection 7043 non-null object \n", " 9 TechSupport 7043 non-null object \n", " 10 Contract 7043 non-null object \n", " 11 PaperlessBilling 7043 non-null object \n", " 12 PaymentMethod 7043 non-null object \n", " 13 MonthlyCharges 7043 non-null float64\n", " 14 TotalCharges 7043 non-null object \n", " 15 Churn 7043 non-null object \n", "dtypes: float64(1), int64(1), object(14)\n", "memory usage: 880.5+ KB\n" ] } ] }, { "cell_type": "code", "source": [ "c_data['TotalCharges'] = pd.to_numeric(c_data['TotalCharges'], errors='coerce')\n", "c_data.info()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "w_SwTyvi5GYI", "outputId": "4e29588c-f81a-4d1e-d626-8a80b3f02ce9" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "RangeIndex: 7043 entries, 0 to 7042\n", "Data columns (total 16 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 gender 7043 non-null object \n", " 1 Partner 7043 non-null object \n", " 2 Dependents 7043 non-null object \n", " 3 tenure 7043 non-null int64 \n", " 4 MultipleLines 7043 non-null object \n", " 5 InternetService 7043 non-null object \n", " 6 OnlineSecurity 7043 non-null object \n", " 7 OnlineBackup 7043 non-null object \n", " 8 DeviceProtection 7043 non-null object \n", " 9 TechSupport 7043 non-null object \n", " 10 Contract 7043 non-null object \n", " 11 PaperlessBilling 7043 non-null object \n", " 12 PaymentMethod 7043 non-null object \n", " 13 MonthlyCharges 7043 non-null float64\n", " 14 TotalCharges 7032 non-null float64\n", " 15 Churn 7043 non-null object \n", "dtypes: float64(2), int64(1), object(13)\n", "memory usage: 880.5+ KB\n" ] } ] }, { "cell_type": "code", "source": [ "# Removing missing values\n", "c_data.dropna(inplace=True)\n", "c_data.duplicated().sum()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AuIXmyf_5GEm", "outputId": "dbe796ae-7870-4cde-c81d-d1abb3da6382" }, "execution_count": 6, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "26" ] }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "code", "source": [ "#le = LabelEncoder()\n", "#c_data['Churn'] = le.fit_transform(c_data['Churn'])\n", "#c_data.head()" ], "metadata": { "id": "EUI4hmnA8rcL" }, "execution_count": 7, "outputs": [] }, { "cell_type": "code", "source": [ "for col in c_data.columns:\n", " print(f\"Column '{col}' categories: {c_data[col].unique()}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2cyG_RbM_yX_", "outputId": "558cfe16-f354-4681-b973-a9809d7bc5e8" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Column 'gender' categories: ['Female' 'Male']\n", "Column 'Partner' categories: ['Yes' 'No']\n", "Column 'Dependents' categories: ['No' 'Yes']\n", "Column 'tenure' categories: [ 1 34 2 45 8 22 10 28 62 13 16 58 49 25 69 52 71 21 12 30 47 72 17 27\n", " 5 46 11 70 63 43 15 60 18 66 9 3 31 50 64 56 7 42 35 48 29 65 38 68\n", " 32 55 37 36 41 6 4 33 67 23 57 61 14 20 53 40 59 24 44 19 54 51 26 39]\n", "Column 'MultipleLines' categories: ['No phone service' 'No' 'Yes']\n", "Column 'InternetService' categories: ['DSL' 'Fiber optic' 'No']\n", "Column 'OnlineSecurity' categories: ['No' 'Yes' 'No internet service']\n", "Column 'OnlineBackup' categories: ['Yes' 'No' 'No internet service']\n", "Column 'DeviceProtection' categories: ['No' 'Yes' 'No internet service']\n", "Column 'TechSupport' categories: ['No' 'Yes' 'No internet service']\n", "Column 'Contract' categories: ['Month-to-month' 'One year' 'Two year']\n", "Column 'PaperlessBilling' categories: ['Yes' 'No']\n", "Column 'PaymentMethod' categories: ['Electronic check' 'Mailed check' 'Bank transfer (automatic)'\n", " 'Credit card (automatic)']\n", "Column 'MonthlyCharges' categories: [29.85 56.95 53.85 ... 63.1 44.2 78.7 ]\n", "Column 'TotalCharges' categories: [ 29.85 1889.5 108.15 ... 346.45 306.6 6844.5 ]\n", "Column 'Churn' categories: ['No' 'Yes']\n" ] } ] }, { "cell_type": "code", "source": [ "y = c_data['Churn'] # Target Variable\n", "X = c_data.drop('Churn', axis =1) # Independent Variable" ], "metadata": { "id": "7pL-2l8T8rU3" }, "execution_count": 9, "outputs": [] }, { "cell_type": "code", "source": [ "numeric_transformer = Pipeline(steps = [('imputer',SimpleImputer(strategy = 'mean')),('scaler',StandardScaler())])\n", "categorical_transformer = Pipeline(steps = [('imputer',SimpleImputer(strategy = 'most_frequent')),('one_hot_encoder',OneHotEncoder(handle_unknown='ignore', categories='auto', sparse=False))])" ], "metadata": { "id": "HWA2tbQBJU2e" }, "execution_count": 10, "outputs": [] }, { "cell_type": "code", "source": [ "data_numeric =['tenure','MonthlyCharges','TotalCharges']\n", "data_categorical =['gender','Partner','Dependents','MultipleLines','InternetService','OnlineSecurity','OnlineBackup','DeviceProtection','TechSupport','Contract','PaperlessBilling','PaymentMethod']\n", "preprocessor =ColumnTransformer(transformers =[('numeric',numeric_transformer,data_numeric),('categoric',categorical_transformer,data_categorical)])" ], "metadata": { "id": "ObJQIJ8RJxXl" }, "execution_count": 11, "outputs": [] }, { "cell_type": "code", "source": [ "# Identify numeric and non-numeric columns\n", "#num_cols = X.select_dtypes(include=[np.number]).columns.tolist()\n", "#cat_cols = X.select_dtypes(exclude=[np.number]).columns.tolist()\n", "\n", "#X_cat = X[cat_cols].copy()\n", "#X_num = X[num_cols].copy()\n" ], "metadata": { "id": "7T37-Hq78q95" }, "execution_count": 12, "outputs": [] }, { "cell_type": "code", "source": [ "# Creating imputer variables\n", "#numerical_imputer = SimpleImputer(strategy = \"mean\")\n", "#categorical_imputer = SimpleImputer(strategy = \"most_frequent\")\n", "\n", "\n", "\n", "# Define the column transformer\n", "#categorical_features = cat_cols\n", "#categorical_transformer = Pipeline(steps=[('ohc', OneHotEncoder(handle_unknown='ignore', categories='auto', sparse=False))])\n", "#preprocessor = ColumnTransformer(transformers=[('cat', categorical_transformer, cat_cols)])\n" ], "metadata": { "id": "Hx-SIs2K8q2M" }, "execution_count": 13, "outputs": [] }, { "cell_type": "code", "source": [ "# Fitting the Imputer\n", "#X_cat_imputed = categorical_imputer.fit_transform(X_cat)\n", "#X_num_imputed = numerical_imputer.fit_transform(X_num)" ], "metadata": { "id": "LhfvzFX-KnCe" }, "execution_count": 14, "outputs": [] }, { "cell_type": "code", "source": [ "#ohe = OneHotEncoder(handle_unknown='ignore')\n", "#X_cat_encoded = ohe.fit(X_cat_imputed)\n", "#X_cat_encoded = pd.DataFrame(ohe.transform(X_cat_imputed).toarray(),\n", "# columns=ohe.get_feature_names_out(cat_cols))\n", "\n" ], "metadata": { "id": "6N8dMlRuKm7k" }, "execution_count": 15, "outputs": [] }, { "cell_type": "code", "source": [ "#scaler = StandardScaler()\n", "#X_num_scaled = scaler.fit_transform(X_num_imputed)\n", "#X_num_sc = pd.DataFrame(X_num_scaled, columns = num_cols)\n", "\n" ], "metadata": { "id": "EtrHYrNcOvHM" }, "execution_count": 16, "outputs": [] }, { "cell_type": "code", "source": [ "#X_df = pd.concat([X_num_sc,X_cat_encoded], axis =1)" ], "metadata": { "id": "aATpxlXiTA83" }, "execution_count": 17, "outputs": [] }, { "cell_type": "code", "source": [ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=30)" ], "metadata": { "id": "cxIt0C7TZ91X" }, "execution_count": 18, "outputs": [] }, { "cell_type": "code", "source": [ "gbc = GradientBoostingClassifier(learning_rate = 0.1, max_depth = 5, n_estimators=50, random_state=30)\n", "gbc = Pipeline(steps =[('processor',preprocessor),('estimator',gbc)])\n", "model = gbc.fit(X_train,y_train)\n", "\n", "# make predictions on the test data\n", "y_pred = model.predict(X_test)\n", "\n", "# generate the classification report\n", "report = classification_report(y_test, y_pred)\n", "name = 'Gradient Boosting Classifier'\n", "# print the classification report\n", "print(f'{name} classification report:\\n{report}\\n')\n" ], "metadata": { "id": "DRT-nIuvOu9y", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "ca6a84f9-cf35-4bdb-aaa4-c0155128580c" }, "execution_count": 19, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Gradient Boosting Classifier classification report:\n", " precision recall f1-score support\n", "\n", " No 0.83 0.89 0.86 1031\n", " Yes 0.64 0.51 0.57 376\n", "\n", " accuracy 0.79 1407\n", " macro avg 0.74 0.70 0.72 1407\n", "weighted avg 0.78 0.79 0.78 1407\n", "\n", "\n" ] } ] }, { "cell_type": "code", "source": [ " # create a dictionary of a model to fit\n", " #models = {'Gradient Boosting Classifier': GradientBoostingClassifier(learning_rate = 0.1, max_depth = 5, n_estimators=50, random_state=30),}\n", " # iterate over the models and fit each one to the resampled training data\n", " #for name, model in models.items():\n", " # model.fit(X_train, y_train)\n", "\n", " # evaluate each model using cross-validation based on ROC-AUC\n", " #roc_auc_scores = {}\n", " #for name, model in models.items():\n", " # scores = cross_val_score(model, X_train, y_train, cv=5, scoring='roc_auc')\n", " # roc_auc_scores[name] = scores.mean()\n", "\n", " # print the ROC-AUC scores for each model\n", " #for name, score in roc_auc_scores.items():\n", " # print(f'{name}: {score}')\n", "\n", " # choose the model with the highest ROC-AUC score\n", " #best_model_name = max(roc_auc_scores, key=roc_auc_scores.get)\n", " #best_model = models[best_model_name]\n", " #print(f'Best model: {best_model_name}')\n", "\n", "\n", " # iterate over the models and make predictions on the test data for each one\n", " #for name, model in models.items():\n", " # fit the model to the resampled training data\n", " # model.fit(X_train, y_train)\n", " # make predictions on the test data\n", " # y_pred = model.predict(X_test)\n", " # generate the classification report\n", " # report = classification_report(y_test, y_pred)\n", " # print the classification report\n", " # print(f'{name} classification report:\\n{report}\\n')\n", "\n", "\n" ], "metadata": { "id": "XW_eIe9ROu2s" }, "execution_count": 20, "outputs": [] }, { "cell_type": "code", "source": [ "# set the destination path to the \"export\" directory\n", "destination = \".\"\n", "\n", "#best_model = gbc\n", "models = {\"best_model\": gbc}\n", "\n", "# loop through the models and save them using joblib.dump()\n", "for name, model in models.items():\n", " dump(model, os.path.join(destination, f\"{name}.joblib\"))\n" ], "metadata": { "id": "cVzILcaoOusD" }, "execution_count": 21, "outputs": [] }, { "cell_type": "code", "source": [ "!pip freeze > requirements.txt" ], "metadata": { "id": "HrsFQmUROufW" }, "execution_count": 22, "outputs": [] }, { "cell_type": "code", "source": [ "#for name, model in models.items():\n", "# dump(model, os.path.join(destination, f\"{name}.joblib\"))\n", "# if os.path.exists(os.path.join(destination, f\"{name}.joblib\")):\n", "# print(f\"{name} saved successfully!\")\n", "# else:\n", "# print(f\"{name} failed to save.\")" ], "metadata": { "id": "xsrbuX4JKmnR" }, "execution_count": 23, "outputs": [] } ] }