<jupyter_start><jupyter_text>Hyperparameter Optimization For XGBoost using RandomizedSearchCV<jupyter_code>import pandas as pd
## Read the Dataset

df=pd.read_csv('Churn_Modelling.csv')
df.head()
## Correlation
import seaborn as sns
import matplotlib.pyplot as plt
#get correlations of each features in dataset
corrmat = df.corr()
top_corr_features = corrmat.index
plt.figure(figsize=(20,20))
#plot heat map
g=sns.heatmap(df[top_corr_features].corr(),annot=True,cmap="RdYlGn")
#Get the Independent and Dependent Features
X=df.iloc[:,3:13]
Y=df.iloc[:,13]
geography=pd.get_dummies(X['Geography'],drop_first=True)
geography.head()
gender=pd.get_dummies(X['Gender'],drop_first=True)
gender.head()
## Drop Categorical Features
X=X.drop(['Geography','Gender'],axis=1)
X.head()
X=pd.concat([X,geography,gender],axis=1)
X.head()
## Hyper Parameter Optimization

params={
 "learning_rate"    : [0.05, 0.10, 0.15, 0.20, 0.25, 0.30 ] ,
 "max_depth"        : [ 3, 4, 5, 6, 8, 10, 12, 15],
 "min_child_weight" : [ 1, 3, 5, 7 ],
 "gamma"            : [ 0.0, 0.1, 0.2 , 0.3, 0.4 ],
 "colsample_bytree" : [ 0.3, 0.4, 0.5 , 0.7 ]  
}
## Hyperparameter optimization using RandomizedSearchCV
import sklearn
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV
import xgboost

def timer(start_time=None):
    if not start_time:
        start_time = datetime.now()
        return start_time
    elif start_time:
        thour, temp_sec = divmod((datetime.now() - start_time).total_seconds(), 3600)
        tmin, tsec = divmod(temp_sec, 60)
        print('\n Time taken: %i hours %i minutes and %s seconds.' % (thour, tmin, round(tsec, 2)))
classifier=xgboost.XGBClassifier()
random_search=RandomizedSearchCV(classifier,param_distributions=params,n_iter=5,scoring='roc_auc',n_jobs=-1,cv=5,verbose=3)
from datetime import datetime
# Here we go
start_time = timer(None) # timing starts from this point for "start_time" variable
random_search.fit(X,Y)
timer(start_time) # timing ends here for "start_time" variable
X.head()
random_search.best_estimator_
random_search.best_params_
classifier=xgboost.XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
       colsample_bytree=0.5, gamma=0.4, learning_rate=0.1,
       max_delta_step=0, max_depth=6, min_child_weight=7, missing=None,
       n_estimators=100, n_jobs=1, nthread=None,
       objective='binary:logistic', random_state=0, reg_alpha=0,
       reg_lambda=1, scale_pos_weight=1, seed=None, silent=True,
       subsample=1)
from sklearn.model_selection import cross_val_score
score=cross_val_score(classifier,X,Y,cv=10)
score
score.mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1351043643263757, 'fraction_numerical': 0.03643263757115749, 'mean_word_length': 5.476658476658477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '169936', 'license': 'MIT', 'n_tokens_mistral': 1036, 'n_tokens_neox': 972, 'n_words': 199, 'path': 'Hyper parameter Optimization For XGBoost.ipynb', 'repo_name': 'adarshkuthuru/Machine-Learning-Algorithms', 'size': '2635'}