xgboost+python参数介绍的简单使用

本文介绍了xgboost的参数控制过拟合、模型复杂度、处理不平衡数据等,包括max_depth、min_child_weight、gamma等重要参数。同时,讲解了eta、num_round、scale_pos_weight等对模型性能的影响。还提供了xgboost的基本使用方法,并给出了Kaggle竞赛中的Python代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网参数介绍(英文版)

http://xgboost.readthedocs.io/en/latest/how_to/param_tuning.html
http://xgboost.readthedocs.io/en/latest/parameter.html

中文部分翻译版

http://blog.youkuaiyun.com/zc02051126/article/details/46711047

1. xgboost的参数介绍

  1. 控制过拟合
    • 直接控制模型的复杂度
      • max_depth, min_child_weight, gamma
    • 增大产生树的随机性
      • subsample, colsample_bytree
      • eta, num_round
  2. 处理不平衡的数据集
    • 预测的排序(AUC)
      • scale_pos_weight
    • 预测可靠性
      • max_delta_step
  3. 参数分别介绍
    • booster: [default=gbtree],可选gbtree和gblinear,gbtree使用基于树的模型进行提升计算,gblinear使用线性模型进行提升计算
    • silent: [default=0], 是否打印运行时信息,0为打印
    • nthread: [默认为支持的最大线程数], 运行时的线程数
    • num_pbuffer: [自动生成,不需要用户自己设置], 预测数量,一般是输入样本数
    • num_feature: [自动生成,不需要用户自己设置], 特征维数
    • eta: [default=0.3],取值范围[0,1],学习率,迭代的步长比例
    • gamma: [default=0],取值范围[0,$\infty$],损失阈值
    • max_depth: [default=6], 取值范围[0,$\infty$],树的最大深度
    • min_child_weight: [default=1], 取值范围[0,$\infty$],拆分节点权重和阈值,如果节点的样本权重和小于该阈值,就不再进行拆分
    • max_delta_step: [default=0],取值范围[0,$\infty$],每棵树的最大权重估计,0为没有限制
    • subsample: [default=1],取值范围(0,1],随机选取一定比例的样本来训练树
    • colsample_bytree: [default=1],取值范围(0,1],选取构造树的特征比例
    • colsample_bylevel: [default=1],取值范围(0,1],每个层分裂的节点数
    • lambda: [default=0],L2 正则的惩罚系数
    • alpha: [default=0],L1 正则的惩罚系数
    • tree_method: string,[default=’auto’],xgboost构建树的算法,‘auto’‘exact’‘approx’‘hist’
    • lambda_bias: 在偏置上的L2正则
    • sketch_eps: [default=0.03],只在approximate greedy algorithm上使用
    • scale_pos_weight: [default=1],用来控制正负样本的比例
    • updater: [default=’grow_colmaker,prune’],提供模块化的方式来构建树,一般不需要由用户设置
    • refresh_leaf: [default=1],刷新参数,如果为1,刷新叶子和树节点,否则只刷新树节点
    • process_type: [default=’default’],提升的方式
    • grow_policy: string [default=’depthwise’],控制新增节点的方式,‘depthwise’,分裂离根节点最近的节点,‘lossguide’,分裂损失函数变化最大的节点
    • max_leaves: [default=0],增加的最大节点数,只和lossguide’ grow policy相关
    • max_bins: [default=256],只和tree_method的‘hist’相关
    • objective: [default=reg:linear], 定义学习任务及相应的学习目标,可选的目标函数如下:
      • “reg:linear”, 线性回归。
      • “reg:logistic”, 逻辑回归。
      • “binary:logistic”, 二分类的逻辑回归问题,输出为概率。
      • “binary:logitraw”, 二分类的逻辑回归问题,输出的结果为wTx。
      • “count:poisson”, 计数问题的poisson回归,输出结果为poisson分布。
        在poisson回归中,max_delta_step的缺省值为0.7。(used to safeguard optimization)
      • “multi:softmax”, 让XGBoost采用softmax目标函数处理多分类问题,同时需要设置参数num_class(类别个数)
      • “multi:softprob”, 和softmax一样,但是输出的是ndata * nclass的向量,可以将该向量reshape成ndata行nclass列的矩阵。没行数据表示样本所属于每个类别的概率。
      • “rank:pairwise” –set XGBoost to do ranking task by minimizing the pairwise loss
        base_score [ default=0.5 ]
        the initial prediction score of all instances, global bias
    • eval_metric: [默认和objective相关],校验数据所需要的评价指标,不同的目标函数将会有缺省的评价指标(rmse for regression, and error for classification, mean average precision for ranking),用户可以添加多种评价指标,对于Python用户要以list传递参数对给程序,而不是map参数list参数不会覆盖
      • ’eval_metric’,可选参数如下:
        • “rmse”: root mean square error,均方根误差
        • “logloss”: negative log-likelihood,对数似然
        • “error”: Binary classification error rate,二值误差率,计算方法为误分样本/总样本
        • “merror”: Multiclass classification error rate,多分类误差率,计算方法同上
        • “auc”: Area under the curve for ranking evaluation.
        • “ndcg”:Normalized Discounted Cumulative Gain
        • “map”:Mean average precision
        • “ndcg@n”,”map@n”: n can be assigned as an integer to cut off the top positions in the lists for evaluation.
        • “ndcg-“,”map-“,”ndcg@n-“,”map@n-“: In XGBoost, NDCG and MAP will evaluate the score of a list without any positive samples as 1. By adding “-” in the evaluation metric XGBoost will evaluate these score as 0 to be consistent under some conditions.
          training repeatively
      • seed: [default=0], 随机数的种子。缺省值为0

2. xgboost的基本使用方法

import xgboost as xgb
# 在这里设置需要的参数
gbm = xgb.XGBClassifier(max_depth=3, n_estimators=300, learning_rate=0.05)
# 传入训练集
gbm = fit(train_X, train_y)
# 预测
predictions = gbm.predict(test_X)

Kaggle竞赛上一个例子

https://www.kaggle.com/cbrogan/xgboost-example-python/code/code

# This script shows you how to make a submission using a few
# useful Python libraries.
# It gets a public leaderboard score of 0.76077.
# Maybe you can tweak it and do better...?

import pandas as pd
import xgboost as xgb
from sklearn.preprocessing import LabelEncoder
import numpy as np

# Load the data
train_df = pd.read_csv('../input/train.csv', header=0)
test_df = pd.read_csv('../input/test.csv', header=0)

# We'll impute missing values using the median for numeric columns and the most
# common value for string columns.
# This is based on some nice code by 'sveitser' at http://stackoverflow.com/a/25562948
from sklearn.base import TransformerMixin
class DataFrameImputer(TransformerMixin):
    def fit(self, X, y=None):
        self.fill = pd.Series([X[c].value_counts().index[0]
            if X[c].dtype == np.dtype('O') else X[c].median() for c in X],
            index=X.columns)
        return self
    def transform(self, X, y=None):
        return X.fillna(self.fill)

feature_columns_to_use = ['Pclass','Sex','Age','Fare','Parch']
nonnumeric_columns = ['Sex']

# Join the features from train and test together before imputing missing values,
# in case their distribution is slightly different
big_X = train_df[feature_columns_to_use].append(test_df[feature_columns_to_use])
big_X_imputed = DataFrameImputer().fit_transform(big_X)

# XGBoost doesn't (yet) handle categorical features automatically, so we need to change
# them to columns of integer values.
# See http://scikit-learn.org/stable/modules/preprocessing.html#preprocessing for more
# details and options
le = LabelEncoder()
for feature in nonnumeric_columns:
    big_X_imputed[feature] = le.fit_transform(big_X_imputed[feature])

# Prepare the inputs for the model
train_X = big_X_imputed[0:train_df.shape[0]].as_matrix()
test_X = big_X_imputed[train_df.shape[0]::].as_matrix()
train_y = train_df['Survived']

# You can experiment with many other options here, using the same .fit() and .predict()
# methods; see http://scikit-learn.org
# This example uses the current build of XGBoost, from https://github.com/dmlc/xgboost
gbm = xgb.XGBClassifier(max_depth=3, n_estimators=300, learning_rate=0.05).fit(train_X, train_y)
predictions = gbm.predict(test_X)

# Kaggle needs the submission to have a certain format;
# see https://www.kaggle.com/c/titanic-gettingStarted/download/gendermodel.csv
# for an example of what it's supposed to look like.
submission = pd.DataFrame({ 'PassengerId': test_df['PassengerId'],
                            'Survived': predictions })
submission.to_csv("submission.csv", index=False)
### 使用 XGBoost 和随机森林进行集成预测的方法及实现 #### 方法概述 XGBoost 是一种基于梯度提升的机器学习框架,而随机森林是一种基于决策树集合的算法。两者虽然有不同的理论基础,但在实际应用中可以结合起来形成更强大的集成模型。通过组合这两种方法,可以在保持高精度的同时提高模型的鲁棒性和泛化能力。 常见的集成方式有投票法、堆叠法(Stacking)、平均法等。以下是具体实现过程: --- #### 实现代码示例 以下是一个 Python 的实现案例,展示如何结合 XGBoost 和随机森林进行预测: ```python import numpy as np from sklearn.ensemble import RandomForestClassifier from xgboost import XGBClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, f1_score from sklearn.datasets import make_classification # 数据生成 X, y = make_classification(n_samples=1000, n_features=20, random_state=42) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 定义随机森林模型 rf_model = RandomForestClassifier(random_state=42) rf_model.fit(X_train, y_train) rf_predictions = rf_model.predict_proba(X_test)[:, 1] # 定义 XGBoost 模型 xgb_model = XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42) xgb_model.fit(X_train, y_train) xgb_predictions = xgb_model.predict_proba(X_test)[:, 1] # 平均法集成 final_predictions_avg = (rf_predictions + xgb_predictions) / 2 # 投票法集成 final_predictions_vote = [] for i in range(len(rf_predictions)): avg_prob = (rf_predictions[i] + xgb_predictions[i]) / 2 final_predictions_vote.append(1 if avg_prob >= 0.5 else 0) # 计算准确性 accuracy_rf = accuracy_score(y_test, rf_model.predict(X_test)) accuracy_xgb = accuracy_score(y_test, xgb_model.predict(X_test)) accuracy_final_avg = accuracy_score(y_test, (final_predictions_avg >= 0.5).astype(int)) accuracy_final_vote = accuracy_score(y_test, final_predictions_vote) print(f"Random Forest Accuracy: {accuracy_rf:.4f}") print(f"XGBoost Accuracy: {accuracy_xgb:.4f}") print(f"Averaging Ensemble Accuracy: {accuracy_final_avg:.4f}") print(f"Voting Ensemble Accuracy: {accuracy_final_vote:.4f}") ``` 上述代码展示了两种简单的集成策略:**平均法**和**投票法**。最终可以通过比较不同集成方法的效果来选择最优方案[^1]。 --- #### 参数调优建议 为了进一步优化集成效果,可以调整以下几个方面: 1. **随机森林超参数**: 调整 `n_estimators`、`max_depth`、`min_samples_split` 等参数以获得更好的性能。 2. **XGBoost参数**: 设置合适的 `learning_rate`、`gamma`、`subsample` 等参数,从而平衡偏差与方差[^4]。 3. **权重分配**: 如果发现某一模型表现显著优于另一个,则可以为其赋予更高的权重[^2]。 --- #### 结果分析 通过对两个独立模型的结果取平均值或采用多数表决的方式,能够有效减少单个模型可能存在的过拟合风险,并增强整体系统的稳定性[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值