import numpy as np
import pandas as pd
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn import cross_validation,metrics
from sklearn.grid_search import GridSearchCV
def modelFit(alg,X,y,useTrainCV=True,cvFolds=5,early_stopping_rounds=50):
if useTrainCV:
xgbParams = alg.get_xgb_params()
xgTrain = xgb.DMatrix(X,label=y)
cvresult = xgb.cv(xgbParams,xgTrain,num_boost_round=alg.get_params()['n_estimators'],nfold=cvFolds,stratified=True,metrics={'mlogloss'},early_stopping_rounds=early_stopping_rounds,seed=0,callbacks=[xgb.callback.print_evaluation(show_stdv=False),xgb.callback.early_stop(3)])
print cvresult
alg.set_params(n_estimators=cvresult.shape[0])
# Fit the algorithm