# 常规参数

  • booster
    • gbtree 树模型做为基分类器(默认)
    • gbliner 线性模型做为基分类器
  • silent
    • silent=0时,不输出中间过程(默认)
    • silent=1时,输出中间过程
  • nthread
    • nthread=-1时,使用全部CPU进行并行运算(默认)
    • nthread=1时,使用1个CPU进行运算。
    • scale_pos_weight
        • 正样本的权重,在二分类任务中,当正负样本比例失衡时,设置正样本的权重,模型效果更好。例如,当正负样本比例为1:10时,scale_pos_weight=10。

        # 模型参数

        • n_estimatores
          • 含义:总共迭代的次数,即决策树的个数
          • 调参:
        • early_stopping_rounds
          • 含义:在验证集上,当连续n次迭代,分数没有提高后,提前终止训练。
          • 调参:防止overfitting。
        • max_depth
          • 含义:树的深度,默认值为6,典型值3-10。
          • 调参:值越大,越容易过拟合;值越小,越容易欠拟合。
        • min_child_weight
          • 含义:默认值为1,。
          • 调参:值越大,越容易欠拟合;值越小,越容易过拟合(值较大时,避免模型学习到局部的特殊样本)。
        • subsample
          • 含义:训练每棵树时,使用的数据占全部训练集的比例。默认值为1,典型值为0.5-1。
          • 调参:防止overfitting。
        • colsample_bytree
          • 含义:训练每棵树时,使用的特征占全部特征的比例。默认值为1,典型值为0.5-1。
          • 调参:防止overfitting。

        # 学习任务参数

        • learning_rate
          • 含义:学习率,控制每次迭代更新权重时的步长,默认0.3。
          • 调参:值越小,训练越慢。
          • 典型值为0.01-0.2。
        • objective 目标函数
          • 回归任务
            • reg:linear (默认)
            • reg:logistic
          • 二分类
            • binary:logistic     概率 
            • binary:logitraw   类别
          • 多分类
            • multi:softmax  num_class=n   返回类别
            • multi:softprob   num_class=n  返回概率
          • rank:pairwise
        • eval_metric
          • 回归任务(默认rmse)
            • rmse--均方根误差
            • mae--平均绝对误差
          • 分类任务(默认error)
            • auc--roc曲线下面积
            • error--错误率(二分类)
            • merror--错误率(多分类)
            • logloss--负对数似然函数(二分类)
            • mlogloss--负对数似然函数(多分类)

        • gamma
          • 惩罚项系数,指定节点分裂所需的最小损失函数下降值。
          • 调参:
        • alpha
          • L1正则化系数,默认为1
        • lambda
          • L2正则化系数,默认为1


        # 代码主要函数:

        • 载入数据:load_digits()
        • 数据拆分:train_test_split()
        • 建立模型:XGBClassifier()
        • 模型训练:fit()
        • 模型预测:predict()
        • 性能度量:accuracy_score()
        • 特征重要性:plot_importance()


          1 # -*- coding: utf-8 -*-
          2 """
          3 ###############################################################################
          4 # 作者:wanglei5205
          5 # 邮箱:wanglei5205@126.com
          6 # 代码:http://github.com/wanglei5205
          7 # 博客:http://cnblogs.com/wanglei5205
          8 # 目的:学习xgboost的XGBClassifier函数
          9 # 官方API文档:http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.training
         10 ###############################################################################
         11 """
         12 ### load module
         13 import matplotlib.pyplot as plt
         14 from sklearn import datasets
         15 from sklearn.model_selection import train_test_split
         16 from sklearn.metrics import accuracy_score
         17 from xgboost import XGBClassifier
         18 from xgboost import plot_importance
         19 
         20 ### load datasets
         21 digits = datasets.load_digits()
         22 
         23 ### data analysis
         24 print(digits.data.shape)
         25 print(digits.target.shape)
         26 
         27 ### data split
         28 x_train,x_test,y_train,y_test = train_test_split(digits.data,
         29                                                  digits.target,
         30                                                  test_size = 0.3,
         31                                                  random_state = 33)
         32 ### fit model for train data
         33 model = XGBClassifier(learning_rate=0.1,
         34                       n_estimators=1000,         # 树的个数--1000棵树建立xgboost
         35                       max_depth=6,               # 树的深度
         36                       min_child_weight = 1,      # 叶子节点最小权重
         37                       gamma=0.,                  # 惩罚项中叶子结点个数前的参数
         38                       subsample=0.8,             # 随机选择80%样本建立决策树
         39                       colsample_btree=0.8,       # 随机选择80%特征建立决策树
         40                       objective='multi:softmax', # 指定损失函数
         41                       scale_pos_weight=1,        # 解决样本个数不平衡的问题
         42                       random_state=27            # 随机数
         43                       )
         44 model.fit(x_train,
         45           y_train,
         46           eval_set = [(x_test,y_test)],
         47           eval_metric = "mlogloss",
         48           early_stopping_rounds = 10,
         49           verbose = True)
         50 
         51 ### plot feature importance
         52 fig,ax = plt.subplots(figsize=(15,15))
         53 plot_importance(model,
         54                 height=0.5,
         55                 ax=ax,
         56                 max_num_features=64)
         57 plt.show()
         58 
         59 ### make prediction for test data
         60 y_pred = model.predict(x_test)
         61 
         62 ### model evaluate
         63 accuracy = accuracy_score(y_test,y_pred)
         64 print("accuarcy: %.2f%%" % (accuracy*100.0))
         65 """
         66 95.74%
         67 """

        相关文章:

        • 2022-12-23
        • 2022-12-23
        • 2021-09-26
        • 2021-06-19
        • 2021-07-21
        • 2022-12-23
        • 2021-06-23
        • 2022-12-23
        猜你喜欢
        • 2022-12-23
        • 2022-12-23
        • 2021-08-01
        • 2022-12-23
        • 2022-12-23
        • 2022-12-23
        相关资源
        相似解决方案