#Import libraries:
import pandas as pd
import numpy as np
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import cross_validation, metrics   #Additional     scklearn functions
from sklearn.grid_search import GridSearchCV   #Perforing grid search
from sklearn.externals import joblib

import matplotlib.pylab as plt
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 12, 4#配置画图参数

train = pd.read_csv('Data/train-gao.csv')
target = 'Kind'
IDcol = 'ID'


#建立XGBoost models 并进行交叉验证
def modelfit(alg, dtrain, 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)
        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['Kind'],eval_metric='auc')

#Predict training set:
    dtrain_predictions = alg.predict(dtrain[predictors])
    dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
    
#Save modle    
    joblib.dump(alg, 'xgboost.model')
    print "-----save xgboost------"
#Print model report:
    print "\nModel Report"
    print "Accuracy : %.4g" % metrics.accuracy_score(dtrain['Kind'].values, dtrain_predictions)#评估模型准确率的函数
    print "AUC Score (Train): %f" % metrics.roc_auc_score(dtrain['Kind'], dtrain_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')
    print cvresult
#Choose all predictors except target & IDcols
predictors = [x for x in train.columns if x not in [target,IDcol]]





def trainModel():


if __name__ == '__main__':
    xgb_model = XGBClassifier(
        learning_rate =0.3,   # 学习率
        n_estimators=90,  # 基分类器个数
        max_depth=5,  # 最大树深
        min_child_weight=1,  
        gamma=0,
        subsample=0.6,
        colsample_bytree=0.9,
        objective= 'binary:logistic',
        nthread=4,
        scale_pos_weight=1,
        seed=27)
    # modelfit(xgb1, train, predictors)

