# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns
%matplotlib inline 
pd.set_option('display.max_columns', None)
path = '../input/bankchurners-set2/BankChurners.csv'
df = pd.read_csv(path)
df.head()
df.info()
# dropping the last two columns
df.drop(df.columns[-2:], axis = 1, inplace = True)
df.head()
df.describe()
df.isnull().sum()
"""
**There are no null values in the dataset**
"""
df.columns
# dropping the clientnum column
df.drop('CLIENTNUM', axis = 1, inplace = True)
"""
## Data Preparation for model building
"""
"""
#### Target column
"""
target_col = pd.get_dummies(df['Attrition_Flag'], drop_first = True)

df['target'] = target_col

df.drop('Attrition_Flag', axis = 1, inplace = True)
"""
#### Gender
"""
df['Gender'].unique()
d = {
    'M': 0,
    'F': 1
}
df['Gender'] = df['Gender'].map(d)
"""
#### Marital_Status
"""
d = {
    'Unknown': 0,
    'Single': 1,
    'Married': 2,
    'Divorced': 3
}
df['Marital_Status'] = df['Marital_Status'].map(d)
"""
#### Education_Level
"""
df['Education_Level'].value_counts().index
d = {
    'Unknown': 0,
    'Uneducated': 1,
    'High School': 2,
    'College': 3,
    'Graduate': 4,
    'Post-Graduate': 5,
    'Doctorate': 6
}
df['Education_Level'] = df['Education_Level'].map(d)
"""
#### Income_Category
"""
d = {
    'Unknown': 0,
    'Less than $40K': 1,
    '$40K - $60K': 2,
    '$60K - $80K': 3,
    '$80K - $120K': 4,
    '$120K +': 5
}
df['Income_Category'] = df['Income_Category'].map(d)
"""
#### Card_Category
"""
df['Card_Category'].value_counts().index
d = {
    'Blue': 0,
    'Silver': 1,
    'Gold': 2,
    'Platinum': 3
}
df['Card_Category'] = df['Card_Category'].map(d)
"""
### Models
"""
from sklearn.model_selection import train_test_split
X = df.iloc[:, :-1]
y = df.iloc[:, -1]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
dec = DecisionTreeClassifier()
random = RandomForestClassifier()
naive_bayes = GaussianNB()
adaboost = AdaBoostClassifier() 
gradboost = GradientBoostingClassifier()
models = [
    dec, 
    random, 
    naive_bayes,
    adaboost,
    gradboost
]

for model in models:
    print(model)
    crossval = cross_val_score(model, X, y, cv = 5)
    print(crossval)
    print('mean is {}'.format(crossval.mean()))
    print("The standard deviation is {}".format(crossval.std()))
    print()
random.fit(X_train, y_train)
from sklearn.metrics import accuracy_score 
accuracy_score(y_test, random.predict(X_test))