XGboost有关参数及其使用(PYTHON)
来源:互联网 发布:移动有网络机顶盒吗 编辑:程序博客网 时间:2024/05/22 00:28
首先介绍一下数据集:
1:生成数据集:可以用来分类任务,可以用来回归任务,可以用来聚类任务,用于流形学习的,用于因子分解任务的
用于分类任务和聚类任务的:这些函数产生样本特征向量矩阵以及对应的类别标签集合
make_blobs:多类单标签数据集,为每个类分配一个或多个正太分布的点集
make_classification:多类单标签数据集,为每个类分配一个或多个正太分布的点集,提供了为数据添加噪声的方式,包括维度相关性,无效特征以及冗余特征等
make_gaussian-quantiles:将一个单高斯分布的点集划分为两个数量均等的点集,作为两类
make_hastie-10-2:产生一个相似的二元分类数据集,有10个维度
make_circle和make_moom产生二维二元分类数据集来测试某些算法的性能,可以为数据集添加噪声,可以为二元分类器产生一些球形判决界面的数据
#生成球形判决界面的数据from sklearn.datasets.samples_generator import make_circlesX,labels=make_circles(n_samples=200,noise=0.2,factor=0.2,random_state=1)print("X.shape:",X.shape)print("labels:",set(labels))#生成用于分类的数据集from sklearn.datasets.samples_generator import make_classificationX,labels=make_classification(n_samples=200,n_features=2,n_redundant=0,n_informative=2, random_state=1,n_clusters_per_class=2)rng=np.random.RandomState(2)X+=2*rng.uniform(size=X.shape)#生成多类单标签数据集import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets.samples_generator import make_blobscenter=[[1,1],[-1,-1],[1,-1]]cluster_std=0.3X,labels=make_blobs(n_samples=200,centers=center,n_features=2, cluster_std=cluster_std,random_state=0)print('X.shape',X.shape)print("labels",set(labels))unique_lables=set(labels)
- 自带数据集
#乳腺癌数据集load-barest-cancer():简单经典的用于二分类任务的数据集#波士顿房价数据集:load-boston():经典的用于回归任务的数据集#糖尿病数据集:load-diabetes():经典的用于回归认为的数据集,值得注意的是,这10个特征中的每个特征都已经被处理成0均值,方差归一化的特征值,#体能训练数据集:load-linnerud():经典的用于多变量回归任务的数据集,其内部包含两个小数据集:Excise是对3个训练变量的20次观测(体重,腰围,脉搏),physiological是对3个生理学变量的20次观测(引体向上,仰卧起坐,立定跳远)
XGBoost有两大类接口:XGBoost原生接口 和 scikit-learn接口 ,并且XGBoost能够实现 分类 和 回归 两种任务。使用python中机器学习库中的XGboost一般有三种类型的参数大致如下介绍:
基于sklearn的代码如下:
from sklearn.datasets import load_breast_cancerimport xgboost as xgbfrom xgboost import plot_importancefrom matplotlib import pyplot as pltfrom sklearn.model_selection import train_test_split# read in the iris databc= load_breast_cancer()X = bc.datay = bc.targetX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)# 训练模型model = xgb.XGBClassifier(max_depth=5, learning_rate=0.1, n_estimators=160, silent=True, objective='multi:softmax')model.fit(X_train, y_train)# 对测试集进行预测ans = model.predict(X_test)# 计算准确率cnt1 = 0cnt2 = 0for i in range(len(y_test)): if ans[i] == y_test[i]: cnt1 += 1 else: cnt2 += 1print("Accuracy: %.2f %% " % (100 * cnt1 / (cnt1 + cnt2)))# 显示重要特征plot_importance(model)plt.show()
基于XGBoost原生接口的回归
import xgboost as xgbfrom xgboost import plot_importancefrom matplotlib import pyplot as pltfrom sklearn.model_selection import train_test_split# 读取文件原始数据data = []labels = []labels2 = []with open("regressionData.csv", encoding='UTF-8') as fileObject: for line in fileObject: line_split = line.split(',') data.append(line_split[10:]) labels.append(line_split[8])X = []for row in data: row = [float(x) for x in row] X.append(row)y = [float(x) for x in labels]# XGBoost训练过程X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)params = { 'booster': 'gbtree', 'objective': 'reg:gamma', 'gamma': 0.1, 'max_depth': 5, 'lambda': 3, 'subsample': 0.7, 'colsample_bytree': 0.7, 'min_child_weight': 3, 'silent': 1, 'eta': 0.1, 'seed': 1000, 'nthread': 4,}dtrain = xgb.DMatrix(X_train, y_train)num_rounds = 300plst = params.items()model = xgb.train(plst, dtrain, num_rounds)# 对测试集进行预测dtest = xgb.DMatrix(X_test)ans = model.predict(dtest)# 显示重要特征plot_importance(model)plt.show()
一: General parameters
该参数参数控制在提升(boosting)过程中使用哪种booster,常用的booster有树模型(tree)和线性模型(linear model)
1. booster [default=gbtree]
有两中模型可以选择gbtree和gblinear。gbtree使用基于树的模型进行提升计算,gblinear使用线性模型进行提升计算。缺省值为gbtree。
silent [default=0]
取0时表示打印出运行时信息,取1时表示以缄默方式运行,不打印运行时信息。缺省值为0。nthread
XGBoost运行时的线程数。缺省值是当前系统可以获得的最大线程数。num_pbuffer
预测缓冲区大小,通常设置为训练实例的数目。缓冲用于保存最后一步提升的预测结果,无需人为设置。num_feature
Boosting过程中用到的特征维数,设置为特征个数。XGBoost会自动设置,无需人为设置。
二:Booster parameters
此参数要看我们使用的是哪种booster
Parameters for Tree Booster
eta [default=0.3]
为了防止过拟合,更新过程中用到的收缩步长。在每次提升计算之后,算法会直接获得新特征的权重。 eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3 。
取值范围为:[0,1]gamma [default=0]
minimum loss reduction required to make a further partition on a leaf node of the tree. the larger, the more conservative the algorithm will be.
取值范围为:[0,∞]max_depth [default=6]
数的最大深度。缺省值为6。
取值范围为:[1,∞]min_child_weight [default=1]
孩子节点中最小的样本权重和。如果一个叶子节点的样本权重和小于min_child_weight则拆分过程结束。在现行回归模型中,这个参数是指建立每个模型所需要的最小样本数。该成熟越大算法越conservative。
取值范围为:[0,∞]max_delta_step [default=0]
我们允许每个树的权重被估计的值。如果它的值被设置为0,意味着没有约束;如果它被设置为一个正值,它能够使得更新的步骤更加保守。通常这个参数是没有必要的,但是如果在逻辑回归中类极其不平衡这时候他有可能会起到帮助作用。把它范围设置为1-10之间也许能控制更新。
取值范围为:[0,∞]subsample [default=1]
用于训练模型的子样本占整个样本集合的比例。如果设置为0.5则意味着XGBoost将随机的从整个样本集合中随机的抽取出50%的子样本建立树模型,这能够防止过拟合。
取值范围为:(0,1]colsample_bytree [default=1]
在建立树时对特征采样的比例。缺省值为1。
取值范围为:(0,1]
Parameter for Linear Booster
- lambda 默认值为0 L2 正则的惩罚系数
- alpha 默认值为0 L1 正则的惩罚系数
- lambda_bias 在偏置上的L2正则。缺省值为0(在L1上没有偏置项的正则,因为L1时偏置不重要)。
三: Task parameters
控制学习的场景,例如在回归问题中会使用不同的参数控制排序
1. seed [ default=0 ] 随机数的种子。缺省值为0。
2. base_score [ default=0.5 ]
所有实例的初始化预测分数,全局偏置;为了足够的迭代次数,改变这个值将不会有太大的影响。
3. eval_metric [ default according to objective ]
校验数据所需要的评价指标,不同的目标函数将会有缺省的评价指标(rmse for regression, and error for classification, mean average precision for ranking)。
用户可以添加多种评价指标,对于Python用户要以list传递参数对给程序,而不是map参数list参数不会覆盖’eval_metric’。
可供的选择如下:
(1) “rmse”: root mean square error
(2)“logloss”: negative log-likelihood
(3)“error”: Binary classification error rate. It is calculated as #(wrong cases)/# (all cases). For the predictions, the evaluation will regard the instances with prediction value larger than 0.5 as positive instances, and the others as negative instances.
(4)“merror”: Multiclass classification error rate.
(5)“mlogloss”: Multiclass logloss.
(6)“auc”: Area under the curve for ranking evaluation.
(7)“ndcg”:Normalized Discounted Cumulative Gain
(8)“map”:Mean average precision
(9)“ndcg@n”,”map@n”: n can be assigned as an integer to cut off the top positions in the lists for evaluation.
(10)“ndcg-“,”map-“,”ndcg@n-“,”map@n-“: In XGBoost, NDCG andMAP will evaluate the score of a list without any positive samples as 1. By adding (11)“-” in the evaluation metric XGBoostwill evaluate these score as 0 to be consistent under some conditions. training repeatively
4. 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。
- XGboost有关参数及其使用(PYTHON)
- xgboost+python参数介绍的简单使用
- Xgboost在Python库中的参数介绍 及其调参过程
- XGBoost:在Python中使用XGBoost
- XGBoost:在Python中使用XGBoost
- XGBoost:在Python中使用XGBoost
- XGBoost参数调优完全指南(附Python代码)
- XGBoost参数调优完全指南(附Python代码)
- XGBoost参数调优完全指南(附Python代码)
- XGBoost参数调优完全指南(附Python代码)
- 在Python中使用XGBoost
- 在Python中使用XGBoost
- XGBoost原理及在Python中使用XGBoost
- XGBoost参数
- xgboost 参数
- XGBoost参数
- XGBoost调参demo(Python)
- XGBoost中参数调优的完整指南(含Python-3.X代码)
- HDU
- 软件工程(C语言实践篇)学习心得总结
- 11-13 struts学习
- 广度优先的连通域计算
- SIM卡不识或者掉卡简单分析
- XGboost有关参数及其使用(PYTHON)
- 10个最佳的大数据处理编程语言
- 6.12
- 交叉编译(3)——交叉编译工具
- shell参数判断
- 使用maven将项目中的test代码打包进jar中
- 字符串函数模拟实现
- java批量修改本地文件名
- django-----转储MySQL数据库