File size: 17,360 Bytes
663a1bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **Churn Predictor UI with Gradio**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"#\n",
"import gradio as gr\n",
"import pandas as pd\n",
"import pickle\n",
"from sklearn.pipeline import Pipeline\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.preprocessing import StandardScaler, LabelEncoder\n",
"from sklearn.impute import SimpleImputer\n",
"from imblearn.over_sampling import RandomOverSampler\n",
"from sklearn.preprocessing import FunctionTransformer\n",
"import joblib"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"xtrain= pd.read_csv('Xtrains.csv')\n",
"ytrain=pd.read_csv('Ytrains.csv')"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator DecisionTreeClassifier from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n",
"https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n",
" warnings.warn(\n",
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator RandomForestClassifier from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n",
"https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n",
" warnings.warn(\n"
]
}
],
"source": [
"# Loading Models\n",
"with open(\"model.pkl\", \"rb\") as f:\n",
"\tclf = pickle.load(f)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-2 {color: black;background-color: white;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>RandomForestClassifier(n_estimators=10)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">RandomForestClassifier</label><div class=\"sk-toggleable__content\"><pre>RandomForestClassifier(n_estimators=10)</pre></div></div></div></div></div>"
],
"text/plain": [
"RandomForestClassifier(n_estimators=10)"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"clf.fit(xtrain, ytrain.values.ravel())"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 621
},
"id": "5SkaWb6RRZ3i",
"outputId": "c686baee-fb96-4cf1-e008-ceee011ec160"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:219: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
" warnings.warn(\n",
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:222: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
" super().__init__(\n",
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:89: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
" warnings.warn(\n",
"C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:93: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
" super().__init__(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7903\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7903/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'text': 'Customer will not churn', 'entities': []}\n"
]
}
],
"source": [
"\n",
"tenure_labels = {\n",
" 0: \"3-6 months\",\n",
" 1: \"6-9 months\",\n",
" 2: \"9-12 months\",\n",
" 3: \"12-15 months\",\n",
" 4: \"15-18 months\",\n",
" 5: \"18-21 months\",\n",
" 6: \"21-24 months\",\n",
" 7: \"> 24 months\"\n",
"}\n",
"\n",
"# Reverse the mapping for predictions\n",
"tenure_values = {v: k for k, v in tenure_labels.items()}\n",
"\n",
"def predict(tenure, montant, freq_rech, revenue, arpu, freq, data_vol, on_net, orange, tigo, freq_top_pack, regularity):\n",
" \n",
" tenure_value = tenure_values[tenure]\n",
"\n",
" \n",
"\n",
" input_df = pd.DataFrame({\n",
" 'TENURE': [tenure_value],\n",
" 'MONTANT': [montant],\n",
" 'FREQUENCE_RECH': [freq_rech],\n",
" 'REVENUE': [revenue],\n",
" 'ARPU_SEGMENT': [arpu],\n",
" 'FREQUENCE': [freq],\n",
" 'DATA_VOLUME': [data_vol],\n",
" 'ON_NET': [on_net],\n",
" 'ORANGE': [orange],\n",
" 'TIGO': [tigo],\n",
" 'REGULARITY':[regularity],\n",
" 'FREQ_TOP_PACK': [freq_top_pack]\n",
" })\n",
"\n",
" prediction = clf.predict(input_df)\n",
"\n",
"\n",
"\n",
" churn_label = \"Customer will churn\" if prediction == 1 else \"Customer will not churn\"\n",
" result = {\n",
" 'text': churn_label, # Use the churn label as 'text'\n",
" 'entities': [] # You can leave 'entities' as an empty list if no entities need highlighting\n",
" }\n",
" print(result)\n",
" return result\n",
"\n",
"\n",
"\n",
"# Create a dropdown menu with labels\n",
"tenure_dropdown = gr.inputs.Dropdown(list(tenure_labels.values()), label=\"TENURE\")\n",
"\n",
"iface = gr.Interface(\n",
" fn=predict,\n",
" inputs=[\n",
" tenure_dropdown, # Dropdown instead of slider\n",
" #gr.inputs.Slider(minimum=1, maximum=7, label=\"TENURE\"),\n",
" gr.inputs.Slider(minimum=20, maximum=470000, label=\"MONTANT\"),\n",
" gr.inputs.Slider(minimum=1, maximum=131, label=\"FREQUENCE_RECH\"),\n",
" gr.inputs.Slider(minimum=1, maximum=530000, label=\"REVENUE\"),\n",
" gr.inputs.Slider(minimum=0, maximum=2453, label=\"ARPU_SEGMENT\"),\n",
" gr.inputs.Slider(minimum=1, maximum=91, label=\"FREQUENCE\"),\n",
" gr.inputs.Slider(minimum=1, maximum=1702309, label=\"DATA_VOLUME\"),\n",
" gr.inputs.Slider(minimum=0, maximum=51000, label=\"ON_NET\"),\n",
" gr.inputs.Slider(minimum=0, maximum=12040, label=\"ORANGE\"),\n",
" gr.inputs.Slider(minimum=0, maximum=4174, label=\"TIGO\"),\n",
" gr.inputs.Slider(minimum=0, maximum=624, label=\"FREQ_TOP_PACK\"),\n",
" gr.inputs.Slider(minimum=0, maximum=62, label=\"REGULARITY\")\n",
" ],\n",
" \n",
" outputs=output,\n",
" title=\"Team Paris Customer Churn Prediction App\",\n",
" description=\"Let's Get Started With Some Predictions!\"\n",
")\n",
"\n",
"iface.launch()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ia0pOjLxRdqx"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "i3bmV8-IRd5t"
},
"outputs": [],
"source": [
"\n",
"Nikoi Afutu\n",
"5:43 PM (0 minutes ago)\n",
"to me\n",
"\n",
"# Create a label encoder for 'TENURE'\n",
"tenure_encoder = LabelEncoder()\n",
"\n",
"# Define a function to make predictions\n",
"def predict_churn(tenure, montant, freq_rech, revenue, arp_segment, freq, data_vol, on_net, orange, tigo, zone1, zone2, regularity, freq_top_pack):\n",
" # Encode 'TENURE' into numerical values\n",
" tenure_encoded = tenure_encoder.transform([tenure])[0]\n",
"\n",
" # Create a DataFrame with the input features\n",
" input_data = pd.DataFrame({\n",
" 'TENURE': [tenure_encoded],\n",
" 'MONTANT': [montant],\n",
" 'FREQUENCY_RECH': [freq_rech],\n",
" 'REVENUE': [revenue],\n",
" 'ARP_SEGMENT': [arp_segment],\n",
" 'FREQUENCY': [freq],\n",
" 'DATA_VOLUME': [data_vol],\n",
" 'ON_NET': [on_net],\n",
" 'ORANGE': [orange],\n",
" 'TIGO': [tigo],\n",
" 'ZONE1': [zone1],\n",
" 'ZONE2': [zone2],\n",
" 'REGULARITY': [regularity],\n",
" 'FREQ_TOP_PACK': [freq_top_pack]\n",
" })\n",
"\n",
" # Make predictions using the loaded model\n",
" prediction = model.predict(input_data)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5sNawztlReBg"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kl8E75DUReSK"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kOAWiEayRbL0"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "twqCbs6fptdQ"
},
"source": [
"# Into pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KEWdvsgdyQLt"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|