<jupyter_start><jupyter_text>Problem StatementThe task is to predict whether a potential promotee at checkpoint in the test set will be promoted or not after the evaluation process.<jupyter_code>import pandas as pd
import numpy as np
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import cross_validation, metrics
from sklearn.grid_search import GridSearchCV

import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 12, 4
train_data='D:/My Personal Documents/Learnings/Data Science/Data Sets/WNS Analytics/train_LZdllcl.csv'
test_data='D:/My Personal Documents/Learnings/Data Science/Data Sets/WNS Analytics/test_2umaH9m.csv'
train_result='D:/My Personal Documents/Learnings/Data Science/Data Sets/WNS Analytics/train_result.csv'
train=pd.read_csv(train_data)
test=pd.read_csv(test_data)
test_results = pd.read_csv(train_result)
target = 'is_promoted'
IDcol = 'employee_id'
train['source']='train'
test['source']='test'
data = pd.concat([train, test],ignore_index=True)
print (train.shape, test.shape, data.shape)
data.info()
data.apply(lambda x: sum(x.isnull()))
data.apply(lambda x: len(x.unique()))
plt.figure(figsize=(12,6))
sb.countplot(x='department', hue='education',data=data)
sb.countplot(x='is_promoted',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='department',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='education',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='awards_won?',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='no_of_trainings',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='gender',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='previous_year_rating',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='KPIs_met >80%',data=data)
plt.figure(figsize=(12,6))
sb.countplot(x='is_promoted', hue='recruitment_channel',data=data)
def region_decode(region):
    if(region in ('region_2','region_22','region_7')):
        return 'region_A'
    elif(region in ('region_4','region_13','region_15')):
        return 'region_B'
    elif(region in ('region_28','region_26','region_23','region_27','region_31','region_17','region_25','region_16')):
        return 'region_C'
    else:
        return 'region_D'
data['region']=data['region'].apply(region_decode)
plt.figure(figsize=(12,6))
sb.distplot(data['avg_training_score'])
data.previous_year_rating[data.previous_year_rating.isnull()]=3.0
data.education[data.education.isnull()]="Unknown"
def score_trans(score):
    if(score<55):
        return 1
    elif(score>=55 and score<65):
        return 2
    elif(score >=65 and score<75):
        return 3
    elif(score>=75 and score<85):
        return 4
    elif(score>=85 and score<90):
        return 5
    else:
        return 6
    
data.head()
data['avg_training_score']=data['avg_training_score'].apply(score_trans)
def f_trans(col1,col2,col3):
    if(col1==1 or col2==1 or col3==5.0):
        return 1
    else:
        return 0
data['top_performer'] = data.apply(lambda x: f_trans(x['KPIs_met >80%'], x['awards_won?'],x['previous_year_rating']), axis=1)
data=pd.get_dummies(data,columns=['department','education','gender','recruitment_channel','region'])
data.groupby('top_performer').employee_id.count()
data.head()
train=data[data.source=='train']
train.drop('source',axis=1,inplace=True)
test=data[data.source=='test']
test.drop(['source','is_promoted'],axis=1,inplace=True)
def modelfit(alg, dtrain, dtest, predictors,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
    
    if useTrainCV:
        xgb_param = alg.get_xgb_params()
        xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)
        xgtest = xgb.DMatrix(dtest[predictors].values)
        cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
            metrics='auc', early_stopping_rounds=early_stopping_rounds)
        alg.set_params(n_estimators=cvresult.shape[0])
    
    #Fit the algorithm on the data
    alg.fit(dtrain[predictors], dtrain[target],eval_metric='auc')
        
    #Predict training set:
    dtrain_predictions = alg.predict(dtrain[predictors])
    dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
        
    #Print model report:
    print ("\nModel Report")
    print ("Accuracy : %.4g" % metrics.accuracy_score(dtrain[target].values, dtrain_predictions))
    print ("AUC Score (Train): %f" % metrics.roc_auc_score(dtrain[target], dtrain_predprob))
    
#     Predict on testing data:
    dtest['predprob'] = alg.predict_proba(dtest[predictors])[:,1]
    results = test_results.merge(dtest[['employee_id','predprob']], on='employee_id')
    print ('AUC Score (Test): %f' % metrics.roc_auc_score(results[target], results['predprob']))
                
    feat_imp = pd.Series(alg.booster().get_fscore()).sort_values(ascending=False)
    feat_imp.plot(kind='bar', title='Feature Importances')
    plt.ylabel('Feature Importance Score')
predictors = [x for x in train.columns if x not in [target, IDcol]]
xgb1 = XGBClassifier(
        learning_rate =0.1,
        n_estimators=400,
        max_depth=10,
        min_child_weight=1,
        gamma=0,
        subsample=0.8,
        colsample_bytree=0.8,
        objective= 'binary:logistic',
        nthread=4,
        scale_pos_weight=1,
        seed=27)
modelfit(xgb1, train, test, predictors)
#Grid seach on subsample and max_features
#Choose all predictors except target & IDcols
param_test1 = {
    'max_depth':list(range(3,10,2)),
    'min_child_weight':list(range(1,6,2))
}
gsearch1 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=400, max_depth=10,
                                        min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8,
                                        objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27), 
                       param_grid = param_test1, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch1.fit(train[predictors],train[target])
gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_
pred=gsearch1.predict(test[predictors])
s=pd.Series(pred.tolist()).astype(int)
s.to_csv('D:/My Personal Documents/Learnings/Data Science/Data Sets/WNS Analytics/s13.csv')
s.value_counts()
#Grid seach on subsample and max_features
#Choose all predictors except target & IDcols
param_test6 = {
    'reg_alpha':[1e-5, 1e-2, 0.1, 1, 100]
}
gsearch6 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=177, max_depth=4,
                                        min_child_weight=6, gamma=0.1, subsample=0.8, colsample_bytree=0.8,
                                        objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
                       param_grid = param_test6, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch6.fit(train[predictors],train[target])
gsearch6.grid_scores_, gsearch6.best_params_, gsearch6.best_score_
#Grid seach on subsample and max_features
#Choose all predictors except target & IDcols
param_test5 = {
    'subsample':[i/100.0 for i in range(75,90,5)],
    'colsample_bytree':[i/100.0 for i in range(75,90,5)]
}
gsearch5 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=177, max_depth=4,
                                        min_child_weight=6, gamma=0, subsample=0.8, colsample_bytree=0.8,
                                        objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
                       param_grid = param_test5, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch5.fit(train[predictors],train[target])

gsearch5.grid_scores_, gsearch5.best_params_, gsearch5.best_score_
#Grid seach on subsample and max_features
#Choose all predictors except target & IDcols
param_test2 = {
    'max_depth':[4,5,6],
    'min_child_weight':[4,5,6]
}
gsearch2 = GridSearchCV(estimator = XGBClassifier( learning_rate=0.1, n_estimators=140, max_depth=5,
                                        min_child_weight=2, gamma=0, subsample=0.8, colsample_bytree=0.8,
                                        objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
                       param_grid = param_test2, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch2.fit(train[predictors],train[target])
gsearch2.grid_scores_, gsearch5.best_params_, gsearch5.best_score_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14013557737260401, 'fraction_numerical': 0.03237494156147733, 'mean_word_length': 4.474728087012156, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '152446', 'license': 'FTL', 'n_tokens_mistral': 3125, 'n_tokens_neox': 2954, 'n_words': 499, 'path': 'notebooks/WNS Analytics-XGBOOST.ipynb', 'repo_name': 'amrendra1002/ML-DL', 'size': '8556'}