# ---
# 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 numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
# -

# ## Data
# Data were taken from https://www.kaggle.com/pavansubhasht/ibm-hr-analytics-attrition-dataset and was synthetically added field ``GroupType``
# ### GroupType 
#  - standart - the bulk of the company's employees
#  - reserve - the most important employees for the company that the company will retain
#  - ballaste - that part of the staff that you can get rid of

data = pd.read_csv("../data/data_.csv")

data.head()

data.dtypes

data.describe()

# +
data.loc[data.GroupType == 'standart', 'GroupType'] = 0
data.loc[data.GroupType == 'reserve', 'GroupType'] = 1
data.loc[data.GroupType == 'ballast', 'GroupType'] = 2

data.loc[data.Gender == 'Female', 'Gender'] = 0
data.loc[data.Gender == 'Male', 'Gender'] = 1

data.loc[data.OverTime == 'Yes', 'OverTime'] = 1
data.loc[data.OverTime == 'No', 'OverTime'] = 0


# +
def cat_plot(df, feature_name, target_name, palettemap): 
    fig, [axis0,axis1] = plt.subplots(1,2,figsize=(10,5))
    df[feature_name].value_counts().plot.pie(autopct='%1.1f%%',ax=axis0)
    sns.countplot(x=feature_name, hue=target_name, data=df,
                  palette=palettemap,ax=axis1)
    plt.show()
    
group_type = {0: "black", 1: "orange", 2: "orange"} # Color map for visualization
# -

cat_plot(data, 'JobLevel', 'GroupType', group_type)

cat_plot(data, 'RelationshipSatisfaction', 'GroupType', group_type)

cat_plot(data, 'JobSatisfaction', 'GroupType', group_type)

cat_plot(data, 'PercentSalaryHike', 'GroupType', group_type)

cat_plot(data, 'OverTime', 'GroupType', group_type)

colormap = plt.cm.viridis
plt.figure(figsize=(17,17))
plt.title('Correlation between Features', y=1.05, size = 45)
sns.heatmap(data.corr(),
            linewidths=0.1, 
            vmax=1.0, 
            square=True, 
            cmap=colormap, 
            linecolor='white', 
            annot=True)

X_train = data.drop(['GroupType', 'Over18','Attrition', 'Department', 'EducationField', 'JobRole', 'MaritalStatus', 'BusinessTravel'], axis=1)
y_train = data['GroupType']

rforest_checker = RandomForestClassifier(random_state = 0)
rforest_checker.fit(X_train, y_train)
importances_df = pd.DataFrame(rforest_checker.feature_importances_, columns=['Feature_Importance'],
                              index=X_train.columns)
importances_df.sort_values(by=['Feature_Importance'], ascending=False, inplace=True)
print(importances_df)

X_train = X_train.drop(['StandardHours', 'EmployeeCount'], axis=1)

params_dtree = [{'min_samples_split': [5, 10, 15, 20],
                 'min_samples_leaf': [1, 2, 3],
                 'max_features': ['auto', 'log2']}]
grid_dtree = GridSearchCV(estimator = DecisionTreeClassifier(criterion = 'gini', 
                                                             random_state = 0),
                            param_grid = params_dtree,
                            scoring = 'accuracy',
                            cv = 10,
                            n_jobs=-1)
grid_dtree = grid_dtree.fit(X_train, y_train)
best_acc_dtree = grid_dtree.best_score_
best_params_dtree = grid_dtree.best_params_

params_rforest = [{'n_estimators': [200, 300],
                   'max_depth': [5, 7, 10],
                   'min_samples_split': [2, 4]}]
grid_rforest = GridSearchCV(estimator = RandomForestClassifier(criterion = 'gini', 
                                                               random_state = 0, n_jobs=-1),
                            param_grid = params_rforest,
                            scoring = 'accuracy',
                            cv = 10,
                            n_jobs=-1)
grid_rforest = grid_rforest.fit(X_train, y_train)
best_acc_rforest = grid_rforest.best_score_
best_params_rforest = grid_rforest.best_params_

# +
# params_ksvm = [{'C': [0.1, 1, 10], 'kernel': ['linear']},
#                {'C': [0.1, 1, 10], 'kernel': ['rbf'],
#                 'gamma': [0.1, 0.2, 0.3, 0.4]},
#                {'C': [0.1, 1, 10], 'kernel': ['poly'],
#                 'degree': [1, 2, 3],
#                 'gamma': [0.1, 0.2, 0.3, 0.4]}]
# grid_ksvm = GridSearchCV(estimator = SVC(random_state = 0),
#                          param_grid = params_ksvm,
#                          scoring = 'accuracy',
#                          cv = 10,
#                          n_jobs=-1)
# grid_ksvm = grid_ksvm.fit(X_train, y_train)
# best_acc_ksvm = grid_ksvm.best_score_
# best_params_ksvm = grid_ksvm.best_params_
# -

parameter_mlp = [{
    'hidden_layer_sizes': [(50,50,50), (50,100,50), (100,)],
    'activation': ['tanh', 'relu'],
    'solver': ['sgd', 'adam'],
    'alpha': [0.0001, 0.05],
    'learning_rate': ['constant','adaptive'],
}]
grid_mlp = GridSearchCV(estimator = MLPClassifier(hidden_layer_sizes=(25,),max_iter=500),
                        param_grid = parameter_mlp,
                        scoring = 'accuracy',
                        cv = 10,
                        n_jobs=-1)
grid_mlp = grid_mlp.fit(X_train, y_train)  
best_acc_mlp = grid_mlp.best_score_
best_params_mlp = grid_mlp.best_params_

grid_score_dict = {'Best Score': [best_acc_dtree, best_acc_rforest, best_acc_mlp],
                   'Optimized Parameters': [best_params_dtree, best_params_rforest, best_params_mlp],
                  }
pd.DataFrame(grid_score_dict, index=['Decision Tree','Random Forest', 'Multi-layer Perceptron'])

StarcoderdataJupyterScriptsDedupFiltered