<jupyter_start><jupyter_text>Problem StatementYour client is a large MNC and they have 9 broad verticals across the organisation. One of the problem your client is facing is around identifying the right people for promotion (only for manager position and below) and prepare them in time. Currently the process, they are following is:They first identify a set of employees based on recommendations/ past performanceSelected employees go through the separate training and evaluation program for each vertical. These programs are based on the required skill of each verticalAt the end of the program, based on various factors such as training performance, KPI completion (only employees with KPIs completed greater than 60% are considered) etc., employee gets promotionFor above mentioned process, the final promotions are only announced after the evaluation and this leads to delay in transition to their new roles. Hence, company needs your help in identifying the eligible candidates at a particular checkpoint so that they can expedite the entire promotion cycle.<jupyter_code># To suppress warnings
import warnings
warnings.filterwarnings("ignore")

# For setting working director
import os
os.chdir(r'C:\Users\<NAME>\Desktop\wns_hack')

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#machine learning libraries
from bayes_opt import BayesianOptimization
import xgboost as xgb
import lightgbm as lgb
from sklearn.metrics import accuracy_score, average_precision_score, f1_score, recall_score, roc_auc_score,confusion_matrix
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import scale

# variables
iter_no = 5
gp_params = {'alpha': 1e-5}
cv_splits = 8
seed = 42

data = pd.read_csv('train.csv')
data.head()
data.isna().sum().plot(kind='bar')
plt.show()

def treesCV(eta, gamma,max_depth,min_child_weight,subsample,colsample_bytree,n_estimators):
    #function for cross validation gradient boosted trees
    return cross_val_score(xgb.XGBRegressor(objective='binary:logistic',
    											tree_method = 'hist',
                                                learning_rate=max(eta,0),
                                                gamma=max(gamma,0),
                                                max_depth=int(max_depth),
                                                min_child_weight=int(min_child_weight),
                                                silent=True,
                                                subsample=max(min(subsample,1),0.0001),
                                                colsample_bytree=max(min(colsample_bytree,1),0.0001),
                                                n_estimators=int(n_estimators),
                                                seed=42,nthread=-1), X=X_train, y=y_train, scoring=None, cv=cv_splits, n_jobs=-1).mean()




def data_prep(data_df):

    #how to handle types
    data_df_num = data_df.select_dtypes(exclude=object)
    data_df_obj = data_df.select_dtypes(include=object)

    #how to handle nan
    data_df_num = data_df_num.fillna(data_df_num.mean())
    for column in data_df_num.columns:
        data_df_num[column] = scale(data_df_num[column])
    #get dummy variables
    data_df_obj = data_df_obj.fillna("UNKNOWN")
    data_df_obj = pd.get_dummies(data_df_obj, dummy_na=True)

    data_concat = pd.concat([data_df_num, data_df_obj],axis=1)

    return data_concat.drop(['department_nan','department_R&D','department_Finance','department_Operations',
                             'education_nan','education_Below Secondary',
                             'region_region_18','region_nan','region_region_10','region_region_12','region_region_24',
                             'region_region_31', 'region_region_33','region_region_1','region_region_13','region_region_14',
                             'region_region_15','region_region_27','region_region_30','region_region_8','region_region_3',
                             'recruitment_channel_sourcing','recruitment_channel_other','recruitment_channel_nan',
                             'gender_m','gender_nan','region_region_21','region_region_6', 'region_region_5'],axis=1) 

def treesLGBCV(eta, max_depth,min_child_weight,subsample,colsample_bytree,n_estimators):
    #function for cross validation gradient boosted trees
    return cross_val_score(lgb.LGBMRegressor(objective='binary',
    											boosting_type = 'goss',
                                                learning_rate=max(eta,0),
                                                max_depth=int(max_depth),
                                                min_child_weight=int(min_child_weight),
                                                silent=True,
                                                subsample=max(min(subsample,1),0.0001),
                                                colsample_bytree=max(min(colsample_bytree,1),0.0001),
                                                n_estimators=int(n_estimators),
                                                random_state=42,nthread=-1), X=X_train, y=y_train, scoring=None, cv=cv_splits, n_jobs=-1).mean()





# reading data
data_train = pd.read_csv('train.csv', sep=',',encoding="ISO-8859-1")
data_train['is_promoted'] = data_train['is_promoted'].astype('object') 
data_train = data_train.set_index("employee_id")
data_train = data_prep(data_train)

data_pred = pd.read_csv('test.csv', sep=',',encoding="ISO-8859-1")
data_pred = data_pred.set_index("employee_id")
data_pred = data_prep(data_pred)



data_train.head()
#train test split doesnt actually split
X_train, X_test, y_train, y_test = train_test_split(np.array(data_train.drop(['is_promoted'],axis=1)), np.array(data_train['is_promoted']), test_size=0, random_state=42)
X_test1 = np.array(data_pred)


#Bayesian Hyper parameter optimization of gradient boosted trees
treesBO = BayesianOptimization(treesCV,{'eta':(0.10,0.12),
                                        'gamma':(10,13),
                                        'max_depth':(540,580),
                                        'min_child_weight':(0.5,0.7),
                                        'subsample':(0.4,0.7),
                                        'colsample_bytree':(0.5,0.6),
                                        'n_estimators':(1600,1800)})
treesBO.maximize(n_iter=iter_no, **gp_params)
tree_best = treesBO.res['max']



tree_best

#Bayesian Hyper parameter optimization of gradient boosted trees
treesBO_LGB = BayesianOptimization(treesLGBCV,{'eta':(0.10,0.12),
                                        'max_depth':(540,580),
                                        'min_child_weight':(0.5,0.7),
                                        'subsample':(0.4,0.7),
                                        'colsample_bytree':(0.5,0.6),
                                        'n_estimators':(1600,1800)})
treesBO_LGB.maximize(n_iter=iter_no, **gp_params)
treeLGB_best = treesBO_LGB.res['max']



#train tree with best paras
trees_model = xgb.XGBRegressor(objective='binary:logistic',
                                tree_method = 'hist',
                                seed=42,
                                learning_rate=max(tree_best['max_params']['eta'],0),
                                gamma=max(tree_best['max_params']['gamma'],0),
                                max_depth=int(tree_best['max_params']['max_depth']),
                                min_child_weight=int(tree_best['max_params']['min_child_weight']),
                                silent=True,
                                subsample=max(min(tree_best['max_params']['subsample'],1),0.0001),
                                colsample_bytree=max(min(tree_best['max_params']['colsample_bytree'],1),0.0001),
                                n_estimators=int(tree_best['max_params']['n_estimators']),nthread=-1)
trees_model.fit(X_train, y_train)
y_hat1 = trees_model.predict(np.array(X_test1))


#train tree with best paras
trees_model_lgb = lgb.LGBMRegressor(objective='binary',
                                boosting_type = 'goss',
                                random_state=42,
                                learning_rate=max(treeLGB_best['max_params']['eta'],0),
                                max_depth=int(treeLGB_best['max_params']['max_depth']),
                                min_child_weight=int(treeLGB_best['max_params']['min_child_weight']),
                                silent=True,
                                subsample=max(min(treeLGB_best['max_params']['subsample'],1),0.0001),
                                colsample_bytree=max(min(treeLGB_best['max_params']['colsample_bytree'],1),0.0001),
                                n_estimators=int(treeLGB_best['max_params']['n_estimators']),nthread=-1)
trees_model_lgb.fit(X_train, y_train)
y_hat2 = trees_model_lgb.predict(np.array(X_test1))


y_hat1
y_hat2
#yhat=np.where(y_hat1>=.5,1,0)
#train test split doesnt actually split
X_train, X_test, y_train, y_test = train_test_split(np.array(data_train.drop(['is_promoted'],axis=1)), np.array(data_train['is_promoted']), test_size=0.2, random_state=42)
X_test1 = data_pred
#'''
def run_lgb(train_X, train_y, val_X, val_y):
    params = {
    "objective" : "binary",
    "metric" : "auc",
    "num_leaves" : 40,
    "learning_rate" : 0.005,
    "bagging_fraction" : 0.6,
    "feature_fraction" : 0.6,
    "bagging_frequency" : 6,
    "bagging_seed" : 42,
    "verbosity" : -1,
    "seed": 42
    }

    lgtrain = lgb.Dataset(train_X, label = train_y)
    lgval = lgb.Dataset(val_X, label = val_y)
    evals_result = {}
    model = lgb.train(params, lgtrain, 5000, valid_sets = [lgtrain, lgval], early_stopping_rounds = 100, 
              verbose_eval = 150, evals_result = evals_result)
    return model, evals_result
#'''

#print("LGBM performance")
model_lgbm, evals_result = run_lgb(X_train, y_train, X_test, y_test)

print("LightGBM Training Completed...")
#print("################################################")
y_hat2 = model_lgbm.predict(np.array(X_test1))

'''
cb_model = CatBoostClassifier(iterations=500,
                            # objective = 'binary',
                             learning_rate=0.05,
                             depth=10,
                             eval_metric='AUC',
                             random_seed = 42,
                             bagging_temperature = 0.2,
                             od_type='Iter',
                             metric_period = 50,
                             od_wait=20)
'''
#cb_model.fit(X_train, y_train,
#             eval_set=(X_test, y_test),
#             use_best_model=True,
#             verbose=True)
#pred_test_cat = (cb_model.predict_proba(np.array(X_test1)))
yhats = y_hat1 + y_hat2
#yhats = (pred_test_xgb*.5+y_hat2*.3+pred_test_cat[:,1])
yhat = np.where(yhats>.5,1,0)

submission = pd.DataFrame({'employee_id':np.array(data_pred.reset_index("employee_id")['employee_id']),'is_promoted':yhat})

#write to file for submission
submission.to_csv('submission.csv',sep=',', index=False)
submission.head()
trees_model.feature_importances_
submission.is_promoted.value_counts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11186744290192566, 'fraction_numerical': 0.023466188983430364, 'mean_word_length': 2.335125448028674, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '339095', 'license': 'MIT', 'n_tokens_mistral': 3472, 'n_tokens_neox': 3234, 'n_words': 574, 'path': 'xgb_lgb_bo_best_features__2_.ipynb', 'repo_name': 'hyperparam-shafin/wns_hackathon', 'size': '11174'}