Press "Enter" to skip to content

Python中用XGBoost和scikit-learn进行随机梯度增强

 

集成决策树的一种简单技术涉及在训练数据集的子样本上训练树。可以采用训练数据中行的子集来训练称为袋装的单个树。在计算每个分割点时,如果还使用了训练数据的行的子集,则这称为随机森林。这些技术也可以在称为随机梯度增强的技术中用于梯度树增强模型。

 

在本文中,您将发现随机梯度增强以及如何使用XGBoost和Python中的scikit-learn来调整采样参数。阅读这篇文章后,您将知道:

在数据子样本上训练树的原理以及如何将其用于梯度增强。
如何使用scikit-learn调整XGBoost中基于行的子采样。
如何在XGBoost中按树和拆分点调整基于列的子采样。

随机梯度提升

 

梯度增强是一个贪婪的过程。将新的决策树添加到模型中,以更正现有模型的残差。使用贪婪搜索过程创建每个决策树,以选择最能最小化目标函数的分割点。这可能会导致树一次又一次使用相同的属性,甚至使用相同的分割点。

 

套袋是一种创建决策树集合的技术,每个决策树都来自训练数据中不同的随机行子集。效果是,由于样本的随机性允许创建略有不同的树木,因此从树的集合中获得了更好的性能,从而为集合的预测增加了方差。随机森林通过在选择分割点时对要素(列)进行二次采样,从而进一步扩大了这一步骤,从而进一步增加了树木的整体差异。这些相同的技术可以用于梯度提升中决策树的构建中,这种变化称为随机梯度提升。通常使用训练数据的激进子样本,例如40%到80%。

 

教程概述

 

在本教程中,我们将研究不同的二次采样技术在梯度增强中的作用。我们将调整Python的XGBoost库所支持的三种不同的随机梯度增强方式,特别是:

创建每棵树时,对数据集中的行进行二次采样。
创建每棵树时对数据集中的列进行二次采样。
创建每个树时,数据集中每个拆分的列的子采样。

问题描述:Otto数据集

 

在本教程中,我们将使用“奥托集团产品分类挑战”数据集。该数据集可从Kaggle免费获得(您需要注册到Kaggle才能下载此数据集)。您可以从“数据”页面下载训练数据集train.csv.zip并将解压缩后的train.csv文件放入您的工作目录中。该数据集描述了61,000多种产品的93个混淆细节,这些产品分为10个产品类别(例如,时尚,电子产品等)。输入属性是某种不同事件的计数。目标是对新产品做出预测,将其作为10个类别中每一个类别的概率数组,并使用多类对数损失(也称为交叉熵)对模型进行评估。该竞赛已于2015年5月完成,并且由于示例数量不多,问题难度大,几乎不需要数据准备(除了将字符串类变量编码为整数)的事实,该数据集对于XGBoost还是一个很大的挑战。

 

在XGBoost中调整行二次采样

 

行二次抽样涉及选择训练数据集的随机样本而不进行替换。可以在subsample参数的XGBoost类的scikit-learn包装器中指定行子采样。默认值为1.0,该值不进行二次采样。我们可以使用scikit-learn中内置的网格搜索功能来评估从0.1到1.0的不同子样本值对Otto数据集的影响。

 

[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]

 

子样本有9个变体,每个模型将使用10倍交叉验证进行评估,这意味着需要训练和测试9×10或90个模型。

 

下面提供了完整的代码清单。

 

# XGBoost on Otto dataset, tune subsample  
from pandas import read_csv  
from xgboost import XGBClassifier  
from sklearn.model_selection import GridSearchCV  
from sklearn.model_selection import StratifiedKFold  
from sklearn.preprocessing import LabelEncoder  
import matplotlib  
matplotlib.use('Agg')  
from matplotlib import pyplot  
# load data  
data = read_csv('train.csv')  
datadataset = data.values  
# split data into X and y  
X = dataset[:,0:94]  
y = dataset[:,94]  
# encode string class values as integers  
label_encoded_y = LabelEncoder().fit_transform(y)  
# grid search  
model = XGBClassifier()  
subsample = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]  
param_grid = dict(subsamplesubsample=subsample)  
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)  
grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold)  
grid_result = grid_search.fit(X, label_encoded_y)  
# summarize results  
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))  
means = grid_result.cv_results_['mean_test_score']  
stds = grid_result.cv_results_['std_test_score']  
params = grid_result.cv_results_['params']  
for mean, stdev, param in zip(means, stds, params):  
 print("%f (%f) with: %r" % (mean, stdev, param))  
# plot  
pyplot.errorbar(subsample, means, yerr=stds)  
pyplot.title("XGBoost subsample vs Log Loss")  
pyplot.xlabel('subsample')  
pyplot.ylabel('Log Loss')  
pyplot.savefig('subsample.png')

 

运行此示例将打印最佳配置以及每个测试配置的日志丢失。

 

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

 

我们可以看到,获得的最佳结果是0.3,或者使用30%的训练数据集样本训练树。

 

Best: -0.000647 using {'subsample': 0.3}  
-0.001156 (0.000286) with: {'subsample': 0.1}  
-0.000765 (0.000430) with: {'subsample': 0.2}  
-0.000647 (0.000471) with: {'subsample': 0.3}  
-0.000659 (0.000635) with: {'subsample': 0.4}  
-0.000717 (0.000849) with: {'subsample': 0.5}  
-0.000773 (0.000998) with: {'subsample': 0.6}  
-0.000877 (0.001179) with: {'subsample': 0.7}  
-0.001007 (0.001371) with: {'subsample': 0.8}  
-0.001239 (0.001730) with: {'subsample': 1.0}

 

我们可以绘制这些均值和标准偏差对数损失值,以更好地了解性能如何随子样本值变化。

 

我们可以看到确实有30%的人具有最佳的平均表现,但是我们也可以看到,随着比率的增加,表现的差异会明显增加。有趣的是,所有子样本值的平均性能都优于不进行子抽样的平均性能(子样本= 1.0)。

 

在XGBoost中按树调整列二次采样

 

我们还可以在增强模型中创建每个决策树之前,创建要使用的特征(或列)的随机样本。在scikit-learn的XGBoost包装器中,这由colsample_bytree参数控制。默认值为1.0,表示在每个决策树中使用所有列。我们可以在0.1到1.0之间评估colsample_bytree的值,以0.1为增量。

 

[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]

 

完整实例如下:

 

# XGBoost on Otto dataset, tune colsample_bytree  
from pandas import read_csv  
from xgboost import XGBClassifier  
from sklearn.model_selection import GridSearchCV  
from sklearn.model_selection import StratifiedKFold  
from sklearn.preprocessing import LabelEncoder  
import matplotlib  
matplotlib.use('Agg')  
from matplotlib import pyplot  
# load data  
data = read_csv('train.csv')  
datadataset = data.values  
# split data into X and y  
X = dataset[:,0:94]  
y = dataset[:,94]  
# encode string class values as integers  
label_encoded_y = LabelEncoder().fit_transform(y)  
# grid search  
model = XGBClassifier()  
colsample_bytree = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]  
param_grid = dict(colsample_bytreecolsample_bytree=colsample_bytree)  
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)  
grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold)  
grid_result = grid_search.fit(X, label_encoded_y)  
# summarize results  
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))  
means = grid_result.cv_results_['mean_test_score']  
stds = grid_result.cv_results_['std_test_score']  
params = grid_result.cv_results_['params']  
for mean, stdev, param in zip(means, stds, params):  
 print("%f (%f) with: %r" % (mean, stdev, param))  
# plot  
pyplot.errorbar(colsample_bytree, means, yerr=stds)  
pyplot.title("XGBoost colsample_bytree vs Log Loss")  
pyplot.xlabel('colsample_bytree')  
pyplot.ylabel('Log Loss')  
pyplot.savefig('colsample_bytree.png')

 

运行此示例将打印最佳配置以及每个测试配置的日志丢失。

 

注意:由于算法或评估程序的随机性,或数值精度的差异,您的结果可能会有所不同。

 

我们可以看到,模型的最佳性能是colsample_bytree = 1.0。这表明该问题进行二次采样不会增加价值。

 

Best: -0.001239 using {'colsample_bytree': 1.0}  
-0.298955 (0.002177) with: {'colsample_bytree': 0.1}  
-0.092441 (0.000798) with: {'colsample_bytree': 0.2}  
-0.029993 (0.000459) with: {'colsample_bytree': 0.3}  
-0.010435 (0.000669) with: {'colsample_bytree': 0.4}  
-0.004176 (0.000916) with: {'colsample_bytree': 0.5}  
-0.002614 (0.001062) with: {'colsample_bytree': 0.6}  
-0.001694 (0.001221) with: {'colsample_bytree': 0.7}  
-0.001306 (0.001435) with: {'colsample_bytree': 0.8}  
-0.001239 (0.001730) with: {'colsample_bytree': 1.0}

 

绘制结果,我们可以看到模型平稳段的性能(至少在此比例下),值为0.5到1.0。

 

通过拆分在XGBoost中调整列二次采样

 

不必为每个树对列进行一次子采样,我们可以在决策树的每个拆分中对它们进行子采样。原则上,这是随机森林中使用的方法。我们可以在scikit-learn的XGBoost包装器类的colsample_bylevel参数中设置每个拆分所使用的列样本的大小。和以前一样,我们将比率从10%更改为默认值100%。

 

下面提供了完整的代码清单。

 

# XGBoost on Otto dataset, tune colsample_bylevel  
from pandas import read_csv  
from xgboost import XGBClassifier  
from sklearn.model_selection import GridSearchCV  
from sklearn.model_selection import StratifiedKFold  
from sklearn.preprocessing import LabelEncoder  
import matplotlib  
matplotlib.use('Agg')  
from matplotlib import pyplot  
# load data  
data = read_csv('train.csv')  
datadataset = data.values  
# split data into X and y  
X = dataset[:,0:94]  
y = dataset[:,94]  
# encode string class values as integers  
label_encoded_y = LabelEncoder().fit_transform(y)  
# grid search  
model = XGBClassifier()  
colsample_bylevel = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]  
param_grid = dict(colsample_bylevelcolsample_bylevel=colsample_bylevel)  
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)  
grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold)  
grid_result = grid_search.fit(X, label_encoded_y)  
# summarize results  
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))  
means = grid_result.cv_results_['mean_test_score']  
stds = grid_result.cv_results_['std_test_score']  
params = grid_result.cv_results_['params']  
for mean, stdev, param in zip(means, stds, params):  
 print("%f (%f) with: %r" % (mean, stdev, param))  
# plot  
pyplot.errorbar(colsample_bylevel, means, yerr=stds)  
pyplot.title("XGBoost colsample_bylevel vs Log Loss")  
pyplot.xlabel('colsample_bylevel')  
pyplot.ylabel('Log Loss')  
pyplot.savefig('colsample_bylevel.png')

 

运行此示例将打印最佳配置以及每个测试配置的日志丢失。

 

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

 

我们可以看到,通过将colsample_bylevel设置为70%可获得最佳结果,导致(倒置)对数损失为-0.001062,这比将每棵树的列采样设置为100%时看到的-0.001239好。

 

如果每棵树的结果建议使用100%的列,则建议不要放弃列二次采样,而应尝试按拆分的列二次采样。

 

Best: -0.001062 using {'colsample_bylevel': 0.7}  
-0.159455 (0.007028) with: {'colsample_bylevel': 0.1}  
-0.034391 (0.003533) with: {'colsample_bylevel': 0.2}  
-0.007619 (0.000451) with: {'colsample_bylevel': 0.3}  
-0.002982 (0.000726) with: {'colsample_bylevel': 0.4}  
-0.001410 (0.000946) with: {'colsample_bylevel': 0.5}  
-0.001182 (0.001144) with: {'colsample_bylevel': 0.6}  
-0.001062 (0.001221) with: {'colsample_bylevel': 0.7}  
-0.001071 (0.001427) with: {'colsample_bylevel': 0.8}  
-0.001239 (0.001730) with: {'colsample_bylevel': 1.0}

 

我们可以绘制每个colsample_bylevel变化的性能。结果表明,在此比例下的值为0.3后,方差相对较低,并且性能似乎处于平稳状态。

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注