XGBoost Demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/macb007/article/details/79236128
import numpy as np
import pandas as pd
import xgboost as xgb
from sklearn.cross_validation import train_test_split
import os
import csv

#from xgboost.sklearn import XGBClassifier
#from sklearn import cross_validation, metrics   #Additional scklearn functions
#from sklearn.grid_search import GridSearchCV   #Perforing grid search
#
#import matplotlib.pylab as plt
#from matplotlib.pylab import rcParams

#记录程序运行时间
import time
start_time = time.time()

#读入数据
current_dir = os.path.abspath('.')
train = pd.read_csv("train_data.csv")
tests = pd.read_csv("test_data.csv")

params={
'booster':'gbtree',
'objective': 'reg:linear', #多分类的问题
'gamma':0.1,  # 用于控制是否后剪枝的参数,越大越保守,一般0.10.2这样子。
'max_depth':12, # 构建树的深度,越大越容易过拟合
'lambda':2,  # 控制模型复杂度的权重值的L2正则化项参数,参数越大,模型越不容易过拟合。
'subsample':0.7, # 随机采样训练样本
'colsample_bytree':0.7, # 生成树时进行的列采样
'min_child_weight':3,
# 这个参数默认是 1,是每个叶子里面 h 的和至少是多少,对正负样本不均衡时的 0-1 分类而言
#,假设 h  0.01 附近,min_child_weight  1 意味着叶子节点中最少需要包含 100 个样本。
#这个参数非常影响结果,控制叶子节点中二阶导的和的最小值,该参数值越小,越容易 overfitting'silent':0 ,#设置成1则没有运行信息输出,最好是设置为0.
'eta': 0.007, # 如同学习率
'seed':1000,
# 'nthread':7,# cpu 线程数
#'eval_metric': 'auc'
}

plst = list(params.items())
num_rounds =5000 # 迭代次数

train_xy,val = train_test_split(train, test_size = 0.3,random_state=1)
#random_state is of big influence for val-auc
y = train_xy.y
X = train_xy.drop(['y'],axis=1)
val_y = val.y
val_X = val.drop(['y'],axis=1)

xgb_val = xgb.DMatrix(val_X,label=val_y)
xgb_train = xgb.DMatrix(X, label=y)
xgb_test = xgb.DMatrix(tests)
print(type(X))
# print(type(xgb_test))
time.sleep(10000)

watchlist = [(xgb_train, 'train'),(xgb_val, 'val')]

# training model
# early_stopping_rounds 当设置的迭代次数较大时,early_stopping_rounds 可在一定的迭代次数内准确率没有提升就停止训练
model = xgb.train(plst, xgb_train, num_rounds, watchlist,early_stopping_rounds=100)

model.save_model('./xgb.model') # 用于存储训练出的模型
print ("best best_ntree_limit",model.best_ntree_limit)

print ("跑到这里了model.predict")
preds = model.predict(xgb_test,ntree_limit=model.best_ntree_limit)
preds_list=preds.tolist()
preds_list_=[]
for item in preds_list:
    preds_list_.append([item])
# print(preds_list_)
# print(preds_list)
# print(len(preds_list),type(preds_list))

# np.savetxt('xgb_submission.csv',np.c_[range(1,len(tests)+1),preds],delimiter=',',header='ID,y',comments='',fmt='%d')
with open(os.path.join(current_dir, 'result.csv'), 'w', encoding='utf8', newline='') as csvfile:
    writer = csv.writer(csvfile)
#先写入columns_name
    writer.writerow(["pre_y"])
        #写入多行用writerows
    writer.writerows(preds_list_)
#输出运行时长
cost_time = time.time()-start_time
print ("xgboost success!",'\n',"cost time:",cost_time,"(s)")
展开阅读全文

没有更多推荐了,返回首页