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

# ## Import libraries

import numpy as np
import pandas as pd

# ## Load the data

data = pd.read_csv('BankChurners.csv', index_col='CLIENTNUM')

# Deleting the last two columns
data = data.iloc[:,:-2]

data.head()

data.shape

# There is no missing value
100*data.isnull().sum()

# Statistical resume
data.describe().T

# Categorical features
columns = (data.dtypes == 'object')
object_cols = list(columns[columns].index)
print('Categorical variables:')
print(object_cols)

# More imports
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

# ## Exploratory Data Analysis

# Let's explore the data
sns.countplot(x='Attrition_Flag',hue='Gender',data=data,palette='pastel')

plt.title('Dependent count by Target')
sns.countplot(x='Dependent_count',hue='Attrition_Flag',data=data,palette='pastel')
# 2 and 3 dependent count is the most common

plt.figure(figsize=(8,5))
plt.title('Education Level Count by Target')
sns.countplot(x='Education_Level',hue='Attrition_Flag',data=data,palette='pastel')
# Most customers are in the level of graduate

plt.title('Marital Status Count by Target')
sns.countplot(x='Marital_Status',hue='Attrition_Flag',data=data,palette='pastel')
# Most customers are married or single

plt.figure(figsize=(8,5))
plt.title('Income Category Count by Target')
income_order = sorted(data['Income_Category'].unique())
sns.countplot(x='Income_Category',hue='Attrition_Flag',order=income_order,data=data,palette='pastel')
# And they receive less than 40k

plt.title('Card Category Count')
sns.countplot(x='Card_Category',hue='Attrition_Flag',data=data,palette='pastel')

# ## Modeling the Data

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

label_encoder = LabelEncoder()

# Transforming the card category variable
for n in data:
    data.loc[(data['Card_Category'] == 'Gold') | (data['Card_Category'] == 'Platinum')
             , 'Card_Category'] = 'Other'
data.Card_Category.value_counts()

for col in object_cols:
    data[col] = label_encoder.fit_transform(data[col])

data.head()

# ## Spliting the Data

X = data.drop('Attrition_Flag',axis=1)
y = data['Attrition_Flag']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# First let's choose some of the more common models for cross-validation
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV, cross_val_score, StratifiedKFold, learning_curve
from sklearn import metrics
from sklearn.metrics import classification_report,confusion_matrix

kfold = StratifiedKFold(n_splits=10)

# +
random_state = 42
classifiers = []
classifiers.append(SVC(random_state=random_state))
classifiers.append(DecisionTreeClassifier(random_state=random_state))
classifiers.append(RandomForestClassifier(random_state=random_state))
classifiers.append(LogisticRegression(random_state = random_state))

cv_results = []
for classifier in classifiers:
    cv_results.append(cross_val_score(classifier, X_train, y = y_train, scoring = "accuracy", cv = kfold, n_jobs=4))

cv_means = []
cv_std = []

for cv_result in cv_results:
    cv_means.append(cv_result.mean())
    cv_std.append(cv_result.std())

cv_res = pd.DataFrame({'CrossValMeans':cv_means,'CrossValErrors':cv_std,'Algorithm':['SVC','Decision Tree', 'Random Forest', 'Logistic Regression']})

g = sns.barplot('CrossValMeans','Algorithm',data = cv_res, palette='pastel',orient = 'h',**{'xerr':cv_std})
g.set_xlabel('Mean Accuracy')
g = g.set_title('Cross validation scores')
# -

cv_means

# The Random Forest perform better for this data
rfc = RandomForestClassifier(n_estimators=300,max_features=5,max_leaf_nodes=250,random_state=42)

# ### Training the Model

rfc.fit(X_train, y_train)

# ### Predictions

rfc_pred = rfc.predict(X_test)

# ### Evaluating the Model

from sklearn.metrics import classification_report,confusion_matrix,roc_auc_score

print(classification_report(y_test,rfc_pred))

print(roc_auc_score(rfc_pred,y_test))

# +
# Our model was capable to explain more than 95% of the attrition flag
# -

# ## Saving the Model

import joblib

filename = 'Bank_Churners_model.joblib'

joblib.dump(rfc, filename)

StarcoderdataJupyterScriptsDedupFiltered