您的位置:首页 > 编程语言

xgboost代码示例

2017-05-20 19:28 253 查看
之前写过很久了,怕新更新的xgboost不再适用,重新调试了一下代码,可运行,但数据得换成自己的,xgboost,都应该知道它的威力了,这里不再多说,欢迎一起讨论!

# coding=utf-8
import pandas as pd
import xgboost as xgb
from sklearn import metrics
import matplotlib.pylab as plt
from sklearn.model_selection import train_test_split

# 训练模型并预测出结果
def train_model(train_xy, test_xy, random_seed):
test_ID = test_xy.ID
test_y = test_xy.Kind
test_xy = test_xy.drop(['ID'], axis=1)     # 去掉ID
test_xy = test_xy.drop(['Kind'], axis=1)   # 去掉类标
dtest = xgb.DMatrix(test_xy)

train_y = train_xy.Kind
train_xy = train_xy.drop(['ID'], axis=1)
# train_xy = train_xy.drop(['Kind'], axis=1)
# val用于验证,作为一个训练过程监视
train, val = train_test_split(train_xy, test_size=0.2, random_state=random_seed)
y = train.Kind
X = train.drop(['Kind'], axis=1)
val_y = val.Kind
val_x = val.drop(['Kind'], axis=1)

params = {
'booster': 'gbtree',  # gbtree used
'objective': 'binary:logistic',
'early_stopping_rounds': 50,
'scale_pos_weight': 0.63,  # 正样本权重
'eval_metric': 'auc',
'gamma': 0,
'max_depth': 5,
# 'lambda': 550,
'subsample': 0.6,
'colsample_bytree': 0.9,
'min_child_weight': 1,
'eta': 0.02,
'seed': random_seed,
'nthread': 3,
'silent': 0
}
dtrain = xgb.DMatrix(X, label=y)
dval = xgb.DMatrix(val_x, label=val_y)
watchlist = [(dval, 'val'), (dtrain, 'train')]
model = xgb.train(params, dtrain, num_boost_round=50, evals=watchlist)

# 对测试集进行预测(以上部分之所以划分成验证集,可以用来调参)
predict_y = model.predict(dtest, ntree_limit=model.best_ntree_limit) # 预测结果为概率
# print(predict_y)
print "AUC Score (Train): %f" % metrics.roc_auc_score(test_y, predict_y)

# 输出特征重要性
fea_importance = model.get_fscore()
print(fea_importance)

if __name__ == '__main__':
train_xy = pd.read_csv("Data/train-gao.csv")
test_xy = pd.read_csv("Data/test-gao.csv")
train_model(train_xy, test_xy, 12)


output:

[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=4
[0] val-auc:0.833333    train-auc:0.934871
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[1] val-auc:0.858974    train-auc:0.960322
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=4
[2] val-auc:0.871795    train-auc:0.970573
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[3] val-auc:0.961538    train-auc:0.977466
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=3
[4] val-auc:0.940171    train-auc:0.978172
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=4
[5] val-auc:0.92735 train-auc:0.986479
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[6] val-auc:0.931624    train-auc:0.986214
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[7] val-auc:0.931624    train-auc:0.987982
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[8] val-auc:0.931624    train-auc:0.989396
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[9] val-auc:0.931624    train-auc:0.989572
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[10]    val-auc:0.931624    train-auc:0.990279
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=5
[11]    val-auc:0.931624    train-auc:0.990103
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[12]    val-auc:0.92735 train-auc:0.99187
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[13]    val-auc:0.92735 train-auc:0.99134
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 14 extra nodes, 0 pruned nodes, max_depth=4
[14]    val-auc:0.935897    train-auc:0.990456
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[15]    val-auc:0.931624    train-auc:0.989572
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[16]    val-auc:0.931624    train-auc:0.989219
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[17]    val-auc:0.92735 train-auc:0.988689
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[18]    val-auc:0.92735 train-auc:0.989749
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[19]    val-auc:0.931624    train-auc:0.990279
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[20]    val-auc:0.935897    train-auc:0.990103
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=3
[21]    val-auc:0.931624    train-auc:0.989572
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[22]    val-auc:0.935897    train-auc:0.989926
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=4
[23]    val-auc:0.931624    train-auc:0.990986
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[24]    val-auc:0.92735 train-auc:0.990456
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[25]    val-auc:0.923077    train-auc:0.990986
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=5
[26]    val-auc:0.923077    train-auc:0.99134
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[27]    val-auc:0.92735 train-auc:0.991163
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[28]    val-auc:0.931624    train-auc:0.99187
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=5
[29]    val-auc:0.944444    train-auc:0.99293
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[30]    val-auc:0.940171    train-auc:0.9924
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=3
[31]    val-auc:0.940171    train-auc:0.991693
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[32]    val-auc:0.944444    train-auc:0.991693
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[33]    val-auc:0.935897    train-auc:0.993284
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[34]    val-auc:0.940171    train-auc:0.99293
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=5
[35]    val-auc:0.948718    train-auc:0.994168
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[36]    val-auc:0.944444    train-auc:0.993991
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[37]    val-auc:0.948718    train-auc:0.994168
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[38]    val-auc:0.940171    train-auc:0.993991
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 12 extra nodes, 0 pruned nodes, max_depth=4
[39]    val-auc:0.944444    train-auc:0.994168
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[40]    val-auc:0.940171    train-auc:0.993991
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=3
[41]    val-auc:0.948718    train-auc:0.994521
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[42]    val-auc:0.948718    train-auc:0.995051
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=3
[43]    val-auc:0.948718    train-auc:0.995051
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=4
[44]    val-auc:0.948718    train-auc:0.994875
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=4
[45]    val-auc:0.948718    train-auc:0.994698
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[46]    val-auc:0.952991    train-auc:0.995581
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 10 extra nodes, 0 pruned nodes, max_depth=3
[47]    val-auc:0.952991    train-auc:0.995405
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=4
[48]    val-auc:0.952991    train-auc:0.995405
[19:25:44] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 8 extra nodes, 0 pruned nodes, max_depth=3
[49]    val-auc:0.948718    train-auc:0.995405
AUC Score (Train): 0.847222
{'copper': 3, 'ca': 24, 'carbohydrate': 19, 'thiamine': 9, 'zinc': 5, 'protein': 8, 'vta': 4, 'vtc': 2, 'vte': 6, 'na': 17, 'vitamine': 4, 'yansuan': 15, 'fat': 42, 'carotene': 9, 'mg': 2, 'k': 13, 'mn': 3, 'calories': 16, 'p': 4, 'riboflavin': 5, 'iron': 7, 'cholesterol': 29, 'se': 8}
[Finished in 2.2s]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: