# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="CMAuDjpeZCEe"
# # Mount Drive

# + id="JdGxTuaDAbcA" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643717911009, "user_tz": -330, "elapsed": 12507, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="a62e5cf1-23c0-4bf1-a8a8-ccc9766daf47"
from google.colab import drive
drive.mount('/content/drive')

# %cd /content/drive/'My Drive/Transsight/QSR'
# %ls

# + [markdown] id="XRRCGmvMPAVf"
# # Libraries

# + id="rZYsy2jgOvni" executionInfo={"status": "ok", "timestamp": 1643718045302, "user_tz": -330, "elapsed": 430, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}}
import numpy as np
import pandas as pd
from datetime import timedelta
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
import re
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

import warnings
warnings.filterwarnings("ignore")

# + [markdown] id="lKEex1AtD1Mo"
# # Churn Prediction

# + [markdown] id="F_PsA3MeEbTq"
# ## Churn data

# + [markdown] id="sPCn74elE4LP"
# ### Complete data

# + colab={"base_uri": "https://localhost:8080/", "height": 128} id="wnRBQTjyEhMN" executionInfo={"status": "ok", "timestamp": 1643719546498, "user_tz": -330, "elapsed": 1319, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="b4ce649e-69da-4dbd-d87e-edb093d6124e"
churn_df = pd.read_csv("data/churn_data.csv")
print("Number of rows : ", len(churn_df))
churn_df.head(2)

# + [markdown] id="6TLa9urGHyEe"
# ### Basic Info

# + [markdown] id="GBAGSHxryXUw"
# #### Check basic info

# + [markdown] id="g6swr3PJHyEf"
# ##### Duplicates

# + colab={"base_uri": "https://localhost:8080/", "height": 49} executionInfo={"elapsed": 572, "status": "ok", "timestamp": 1643707934902, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}, "user_tz": -330} outputId="546d9a29-e9cc-472a-d7bc-3e4281fd4946" id="BWYnJpbgHyEf"
churn_df[churn_df.duplicated(keep=False)].head(4)

# + [markdown] id="3V6uCFi4HyEg"
# ##### Null values

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 21, "status": "ok", "timestamp": 1643707934903, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}, "user_tz": -330} outputId="067eef51-8d58-4eae-f95e-0296cfb5702f" id="VNuzyWf-HyEg"
count=churn_df.isnull().sum().sort_values(ascending=False)
ratio=count/len(churn_df)
pd.concat([count,ratio],axis=1,keys=['count','ratio'])

# + [markdown] id="-MAqO5W-wL9c"
# #### Statistics

# + colab={"base_uri": "https://localhost:8080/", "height": 297} id="JIQw2CyDwS20" executionInfo={"status": "ok", "timestamp": 1643707934905, "user_tz": -330, "elapsed": 21, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="4b1650c4-0ffc-46eb-eed8-3ce916955309"
churn_df.describe()

# + [markdown] id="BwaLVIAfHyEh"
# #### Unique values

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643719030971, "user_tz": -330, "elapsed": 392, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="3fc3b44c-2d39-4e5a-82bd-1b5cd30a9245" id="XXHAucXqHyEj"
churn_df.nunique()

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="rb6Sf3CxWDqK" executionInfo={"status": "ok", "timestamp": 1643707936293, "user_tz": -330, "elapsed": 924, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="13ea094c-b51f-4c2f-db4e-a87e2b490746"
plt.figure(figsize=(10, 5))
sns.countplot(x="Churn", data=churn_df, palette="husl")

# + [markdown] id="yeQiNwSOEiNu"
# ### Data Splitting

# + colab={"base_uri": "https://localhost:8080/", "height": 111} id="_1HIwhzInlgt" executionInfo={"status": "ok", "timestamp": 1643719551863, "user_tz": -330, "elapsed": 402, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="bbbcac11-7218-4e3e-b823-41e315f03e6d"
train_df = churn_df.loc[churn_df.Data_type =='train']
val_test_df = churn_df.loc[(churn_df.Data_type =='val')|(churn_df.Data_type =='test')]
val_df, test_df = train_test_split(val_test_df, test_size=0.5, random_state=0)
train_df.head(2)

# + colab={"base_uri": "https://localhost:8080/", "height": 111} id="4G1AyGuOUfvb" executionInfo={"status": "ok", "timestamp": 1643719564693, "user_tz": -330, "elapsed": 420, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="7d5eb734-0210-4d6d-ead3-3f9eaec64d34"
val_df.head(2)

# + colab={"base_uri": "https://localhost:8080/", "height": 111} id="LvmpT3cTUjsz" executionInfo={"status": "ok", "timestamp": 1643719579618, "user_tz": -330, "elapsed": 861, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="d7444ad1-8f8d-481c-b321-e6f5532c363c"
test_df.head(2)


# + [markdown] id="G9qqcgdxEk8d"
# ### Data preprocessing pipeline

# + id="qhNm_J6eWIhO" executionInfo={"status": "ok", "timestamp": 1643719589987, "user_tz": -330, "elapsed": 473, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}}
def data_preprocessing(df, label=None):
    return((df.drop([label, 'Transaction_date', 'Data_type'], axis=1), df[label]) if label else df.drop([label], axis=1))


# + colab={"base_uri": "https://localhost:8080/", "height": 111} id="QKZwb8nDob1L" executionInfo={"status": "ok", "timestamp": 1643719591871, "user_tz": -330, "elapsed": 14, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="e5e2fd42-2830-4ff1-fd6f-eed3923f4406"
x_train, y_train = data_preprocessing(train_df, 'Churn')
x_train.head(2)

# + [markdown] id="_3nKO45SEppb"
# ### Training and Evaluation

# + colab={"base_uri": "https://localhost:8080/"} id="Fnw81gFMnYkb" executionInfo={"status": "ok", "timestamp": 1643719688785, "user_tz": -330, "elapsed": 32763, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="8ef298be-040d-4383-9c29-a3edc702eddb"
x_train, y_train = data_preprocessing(train_df, 'Churn')
x_val, y_val = data_preprocessing(val_df, 'Churn')
x_test, y_test = data_preprocessing(test_df, 'Churn')

rf = RandomForestClassifier(random_state=0)
rf.fit(x_train, y_train);

print('********************Evaluation on Validation data********************')
y_pred = rf.predict(x_val)
print(classification_report(y_val, y_pred))

print('\n********************Evaluation on Test data********************')
y_pred = rf.predict(x_test)
print(classification_report(y_test, y_pred))

# + [markdown] id="Dh8o1wjT5_Xx"
# ### Channel Vulnerability based on churn

# + [markdown] id="-70jjKyNEzlC"
# #### Prepare data

# + colab={"base_uri": "https://localhost:8080/", "height": 111} id="aSMfYGbnC3Zu" executionInfo={"status": "ok", "timestamp": 1643715088651, "user_tz": -330, "elapsed": 1957, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="720a8268-9cfd-4af9-ea2f-bdc479d3c548"
trans_df = pd.read_csv('data/TransactionData - masked.csv')
trans_df.head(2)

# + colab={"base_uri": "https://localhost:8080/"} id="2DBk2OsyC1j_" executionInfo={"status": "ok", "timestamp": 1643715098329, "user_tz": -330, "elapsed": 456, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="ad40506b-bdce-4e9c-d54e-e50b86d6211c"
previous_data_size = len(trans_df)
trans_df = trans_df.loc[trans_df.TotalAmount > 0]

trans_df.drop('city', axis=1, inplace=True)
trans_df.dropna(inplace=True)
current_data_size = len(trans_df)

print('Number of dropped rows : ', previous_data_size - current_data_size)
print('Ratio of dropped rows : ', (previous_data_size - current_data_size)/previous_data_size)
print("Final data size : ", len(trans_df))

# + id="loUPLsX-s22p" executionInfo={"status": "ok", "timestamp": 1643715100177, "user_tz": -330, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}}
trans_df['Churn'] = trans_df['CustomerID'].map(churn_df.set_index('CustomerID')['Churn'])

# + [markdown] id="Urmu9_5yE71h"
# #### Channel vulnerability on based on churn ratio

# + colab={"base_uri": "https://localhost:8080/", "height": 669} id="7MkgGb5S6xWn" executionInfo={"status": "ok", "timestamp": 1643715142044, "user_tz": -330, "elapsed": 456, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="b0c0ba90-719d-469c-dd21-475f21b65072"
churn_vul_df = trans_df.assign(Churn_count=trans_df['Churn']).groupby(['channelId', 'Churn']).agg({'Churn_count': 'count'}).reset_index()
churn_vul_df['Churn_ratio'] = churn_vul_df.Churn_count / trans_df.CustomerID.nunique()
churn_vul_df

# + [markdown] id="AdKsuUQCFCmd"
# ### Channel vulnerability plot

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="eYwToVaT5gkD" executionInfo={"status": "ok", "timestamp": 1643715314641, "user_tz": -330, "elapsed": 1620, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="f136fce1-e066-4ba6-9380-acdeade54a1e"
plt.figure(figsize=(15, 5))
sns.barplot(x="channelId", y='Churn_ratio', data=churn_vul_df.loc[churn_vul_df.Churn == 'Yes'], palette="husl")

# + [markdown] id="nmkssoM4FHYv"
# ### Churn prediction per channel

# + colab={"base_uri": "https://localhost:8080/", "height": 354} id="Tx8opFKT453S" executionInfo={"status": "ok", "timestamp": 1643715109331, "user_tz": -330, "elapsed": 2145, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "04701221540373798870"}} outputId="f6b47e86-5f3e-4294-87ea-0b809d354261"
plt.figure(figsize=(15, 5))
sns.countplot(x="channelId", data=trans_df, palette="husl", hue='Churn')

# + id="aiuzdFIX9EjG"


StarcoderdataJupyterScriptsDedupFiltered