在sacikit-learn中,GradientBoostingClassifier为GBDT的分类类, 而GradientBoostingRegressor为GBDT的回归类。两者的参数类型完全相同,当然有些参数比如损失函数loss的可选择项并不相同。这些参数中,我们把重要参数分为两类,第一类是Boosting框架的重要参数,第二类是弱学习器即CART回归树的重要参数。
下面我们就从这两个方面来介绍这些参数的使用。
2. GBDT类库boosting框架参数首先,我们来看boosting框架相关的重要参数。由于GradientBoostingClassifier和GradientBoostingRegressor的参数绝大部分相同,我们下面会一起来讲,不同点会单独指出。
1) n_estimators: 也就是弱学习器的最大迭代次数,或者说最大的弱学习器的个数。一般来说n_estimators太小,容易欠拟合,n_estimators太大,又容易过拟合,一般选择一个适中的数值。默认是100。在实际调参的过程中,我们常常将n_estimators和下面介绍的参数learning_rate一起考虑。
2) learning_rate: 即每个弱学习器的权重缩减系数νν&#xff0c;也称作步长&#xff0c;在原理篇的正则化章节我们也讲到了&#xff0c;加上了正则化项&#xff0c;我们的强学习器的迭代公式为fk(x)&#61;fk−1(x)&#43;νhk(x)fk(x)&#61;fk−1(x)&#43;νhk(x)。νν的取值范围为0<ν≤10<ν≤1。对于同样的训练集拟合效果&#xff0c;较小的νν意味着我们需要更多的弱学习器的迭代次数。通常我们用步长和迭代最大次数一起来决定算法的拟合效果。所以这两个参数n_estimators和learning_rate要一起调参。一般来说&#xff0c;可以从一个小一点的νν开始调参&#xff0c;默认是1。
3) subsample: 即我们在原理篇的正则化章节讲到的子采样&#xff0c;取值为(0,1]。注意这里的子采样和随机森林不一样&#xff0c;随机森林使用的是放回抽样&#xff0c;而这里是不放回抽样。如果取值为1&#xff0c;则全部样本都使用&#xff0c;等于没有使用子采样。如果取值小于1&#xff0c;则只有一部分样本会去做GBDT的决策树拟合。选择小于1的比例可以减少方差&#xff0c;即防止过拟合&#xff0c;但是会增加样本拟合的偏差&#xff0c;因此取值不能太低。推荐在[0.5, 0.8]之间&#xff0c;默认是1.0&#xff0c;即不使用子采样。
4) init: 即我们的初始化的时候的弱学习器&#xff0c;拟合对应原理篇里面的f0(x)f0(x)&#xff0c;如果不输入&#xff0c;则用训练集样本来做样本集的初始化分类回归预测。否则用init参数提供的学习器做初始化分类回归预测。一般用在我们对数据有先验知识&#xff0c;或者之前做过一些拟合的时候&#xff0c;如果没有的话就不用管这个参数了。
5) loss: 即我们GBDT算法中的损失函数。分类模型和回归模型的损失函数是不一样的。
对于分类模型&#xff0c;有对数似然损失函数"deviance"和指数损失函数"exponential"两者输入选择。默认是对数似然损失函数"deviance"。在原理篇中对这些分类损失函数有详细的介绍。一般来说&#xff0c;推荐使用默认的"deviance"。它对二元分离和多元分类各自都有比较好的优化。而指数损失函数等于把我们带到了Adaboost算法。
对于回归模型&#xff0c;有均方差"ls", 绝对损失"lad", Huber损失"huber"和分位数损失“quantile”。默认是均方差"ls"。一般来说&#xff0c;如果数据的噪音点不多&#xff0c;用默认的均方差"ls"比较好。如果是噪音点较多&#xff0c;则推荐用抗噪音的损失函数"huber"。而如果我们需要对训练集进行分段预测的时候&#xff0c;则采用“quantile”。
6) alpha&#xff1a;这个参数只有GradientBoostingRegressor有&#xff0c;当我们使用Huber损失"huber"和分位数损失“quantile”时&#xff0c;需要指定分位数的值。默认是0.9&#xff0c;如果噪音点较多&#xff0c;可以适当降低这个分位数的值。
3. GBDT类库弱学习器参数这里我们再对GBDT的类库弱学习器的重要参数做一个总结。由于GBDT使用了CART回归决策树&#xff0c;因此它的参数基本来源于决策树类&#xff0c;也就是说&#xff0c;和DecisionTreeClassifier和DecisionTreeRegressor的参数基本类似。如果你已经很熟悉决策树算法的调参&#xff0c;那么这一节基本可以跳过。不熟悉的朋友可以继续看下去。
1) 划分时考虑的最大特征数max_features: 可以使用很多种类型的值&#xff0c;默认是"None",意味着划分时考虑所有的特征数&#xff1b;如果是"log2"意味着划分时最多考虑log2Nlog2N个特征&#xff1b;如果是"sqrt"或者"auto"意味着划分时最多考虑N−−√N个特征。如果是整数&#xff0c;代表考虑的特征绝对数。如果是浮点数&#xff0c;代表考虑特征百分比&#xff0c;即考虑&#xff08;百分比xN&#xff09;取整后的特征数。其中N为样本总特征数。一般来说&#xff0c;如果样本特征数不多&#xff0c;比如小于50&#xff0c;我们用默认的"None"就可以了&#xff0c;如果特征数非常多&#xff0c;我们可以灵活使用刚才描述的其他取值来控制划分时考虑的最大特征数&#xff0c;以控制决策树的生成时间。
2) 决策树最大深度max_depth: 默认可以不输入&#xff0c;如果不输入的话&#xff0c;决策树在建立子树的时候不会限制子树的深度。一般来说&#xff0c;数据少或者特征少的时候可以不管这个值。如果模型样本量多&#xff0c;特征也多的情况下&#xff0c;推荐限制这个最大深度&#xff0c;具体的取值取决于数据的分布。常用的可以取值10-100之间。
3) 内部节点再划分所需最小样本数min_samples_split: 这个值限制了子树继续划分的条件&#xff0c;如果某节点的样本数少于min_samples_split&#xff0c;则不会继续再尝试选择最优特征来进行划分。 默认是2.如果样本量不大&#xff0c;不需要管这个值。如果样本量数量级非常大&#xff0c;则推荐增大这个值。
4) 叶子节点最少样本数min_samples_leaf: 这个值限制了叶子节点最少的样本数&#xff0c;如果某叶子节点数目小于样本数&#xff0c;则会和兄弟节点一起被剪枝。 默认是1,可以输入最少的样本数的整数&#xff0c;或者最少样本数占样本总数的百分比。如果样本量不大&#xff0c;不需要管这个值。如果样本量数量级非常大&#xff0c;则推荐增大这个值。
5&#xff09;叶子节点最小的样本权重和min_weight_fraction_leaf&#xff1a;这个值限制了叶子节点所有样本权重和的最小值&#xff0c;如果小于这个值&#xff0c;则会和兄弟节点一起被剪枝。 默认是0&#xff0c;就是不考虑权重问题。一般来说&#xff0c;如果我们有较多样本有缺失值&#xff0c;或者分类树样本的分布类别偏差很大&#xff0c;就会引入样本权重&#xff0c;这时我们就要注意这个值了。
6) 最大叶子节点数max_leaf_nodes: 通过限制最大叶子节点数&#xff0c;可以防止过拟合&#xff0c;默认是"None”&#xff0c;即不限制最大的叶子节点数。如果加了限制&#xff0c;算法会建立在最大叶子节点数内最优的决策树。如果特征不多&#xff0c;可以不考虑这个值&#xff0c;但是如果特征分成多的话&#xff0c;可以加以限制&#xff0c;具体的值可以通过交叉验证得到。
7) 节点划分最小不纯度min_impurity_split: 这个值限制了决策树的增长&#xff0c;如果某节点的不纯度(基于基尼系数&#xff0c;均方差)小于这个阈值&#xff0c;则该节点不再生成子节点。即为叶子节点 。一般不推荐改动默认值1e-7。&#xff08;现在此参数已启用&#xff0c;改用
min_impurity_decrease
&#xff09;
4. GBDT调参实例这里我们用一个二元分类的例子来讲解下GBDT的调参。这部分参考了这个Github上的数据调参过程Parameter_Tuning_GBM_with_Example。
调参方法&#xff1a;使用网格搜索&#xff0c;有些参数需要单独使用网格搜索调节&#xff0c;有些参数需要一起调节。
首先我们从步长(learning rate)和迭代次数(n_estimators)入手。一般来说,开始选择一个较小的步长来网格搜索最好的迭代次数。这里&#xff0c;我们将步长初始值设置为0.1。对于迭代次数进行网格搜索如下&#xff1a;
param_test1 &#61; {&#39;n_estimators&#39;:range(20,81,10)}
gsearch1 &#61; GridSearchCV(estimator &#61; GradientBoostingClassifier(learning_rate&#61;0.1, min_samples_split&#61;300,min_samples_leaf&#61;20,max_depth&#61;8,max_features&#61;&#39;sqrt&#39;, subsample&#61;0.8,random_state&#61;10), param_grid &#61; param_test1, scoring&#61;&#39;roc_auc&#39;,iid&#61;False,cv&#61;5)
gsearch1.fit(X,y)
gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_
输出如下&#xff0c;可见最好的迭代次数是60。
([mean: 0.81285, std: 0.01967, params: {&#39;n_estimators&#39;: 20},mean: 0.81438, std: 0.01947, params: {&#39;n_estimators&#39;: 30},mean: 0.81451, std: 0.01933, params: {&#39;n_estimators&#39;: 40},mean: 0.81618, std: 0.01848, params: {&#39;n_estimators&#39;: 50},mean: 0.81751, std: 0.01736, params: {&#39;n_estimators&#39;: 60},mean: 0.81547, std: 0.01900, params: {&#39;n_estimators&#39;: 70},mean: 0.81299, std: 0.01860, params: {&#39;n_estimators&#39;: 80}],{&#39;n_estimators&#39;: 60},0.8175146087398375)
找到了一个合适的迭代次数&#xff0c;现在我们开始对决策树进行调参。首先我们对决策树最大深度max_depth和内部节点再划分所需最小样本数min_samples_split进行网格搜索
param_test2 &#61; {&#39;max_depth&#39;:range(3,14,2), &#39;min_samples_split&#39;:range(100,801,200)}
gsearch2 &#61; GridSearchCV(estimator &#61; GradientBoostingClassifier(learning_rate&#61;0.1, n_estimators&#61;60, min_samples_leaf&#61;20, max_features&#61;&#39;sqrt&#39;, subsample&#61;0.8, random_state&#61;10), param_grid &#61; param_test2, scoring&#61;&#39;roc_auc&#39;,iid&#61;False, cv&#61;5)
gsearch2.fit(X,y)
gsearch2.grid_scores_, gsearch2.best_params_, gsearch2.best_score_
输出如下&#xff0c;可见最好的最大树深度是7&#xff0c;内部节点再划分所需最小样本数是300。
([mean: 0.81199, std: 0.02073, params: {&#39;min_samples_split&#39;: 100, &#39;max_depth&#39;: 3},mean: 0.81267, std: 0.01985, params: {&#39;min_samples_split&#39;: 300, &#39;max_depth&#39;: 3},mean: 0.81238, std: 0.01937, params: {&#39;min_samples_split&#39;: 500, &#39;max_depth&#39;: 3},mean: 0.80925, std: 0.02051, params: {&#39;min_samples_split&#39;: 700, &#39;max_depth&#39;: 3},mean: 0.81846, std: 0.01843, params: {&#39;min_samples_split&#39;: 100, &#39;max_depth&#39;: 5},mean: 0.81630, std: 0.01810, params: {&#39;min_samples_split&#39;: 300, &#39;max_depth&#39;: 5},mean: 0.81315, std: 0.01898, params: {&#39;min_samples_split&#39;: 500, &#39;max_depth&#39;: 5},mean: 0.81262, std: 0.02090, params: {&#39;min_samples_split&#39;: 700, &#39;max_depth&#39;: 5},mean: 0.81807, std: 0.02004, params: {&#39;min_samples_split&#39;: 100, &#39;max_depth&#39;: 7},mean: 0.82137, std: 0.01733, params: {&#39;min_samples_split&#39;: 300, &#39;max_depth&#39;: 7},mean: 0.81703, std: 0.01773, params: {&#39;min_samples_split&#39;: 500, &#39;max_depth&#39;: 7},mean: 0.81383, std: 0.02327, params: {&#39;min_samples_split&#39;: 700, &#39;max_depth&#39;: 7},mean: 0.81107, std: 0.02178, params: {&#39;min_samples_split&#39;: 100, &#39;max_depth&#39;: 9},mean: 0.80944, std: 0.02612, params: {&#39;min_samples_split&#39;: 300, &#39;max_depth&#39;: 9},mean: 0.81476, std: 0.01973, params: {&#39;min_samples_split&#39;: 500, &#39;max_depth&#39;: 9},mean: 0.81601, std: 0.02576, params: {&#39;min_samples_split&#39;: 700, &#39;max_depth&#39;: 9},mean: 0.81091, std: 0.02227, params: {&#39;min_samples_split&#39;: 100, &#39;max_depth&#39;: 11},mean: 0.81309, std: 0.02696, params: {&#39;min_samples_split&#39;: 300, &#39;max_depth&#39;: 11},mean: 0.81713, std: 0.02379, params: {&#39;min_samples_split&#39;: 500, &#39;max_depth&#39;: 11},mean: 0.81347, std: 0.02702, params: {&#39;min_samples_split&#39;: 700, &#39;max_depth&#39;: 11},mean: 0.81444, std: 0.01813, params: {&#39;min_samples_split&#39;: 100, &#39;max_depth&#39;: 13},mean: 0.80825, std: 0.02291, params: {&#39;min_samples_split&#39;: 300, &#39;max_depth&#39;: 13},mean: 0.81923, std: 0.01693, params: {&#39;min_samples_split&#39;: 500, &#39;max_depth&#39;: 13},mean: 0.81382, std: 0.02258, params: {&#39;min_samples_split&#39;: 700, &#39;max_depth&#39;: 13}],{&#39;max_depth&#39;: 7, &#39;min_samples_split&#39;: 300},0.8213724275914632)
由于决策树深度7是一个比较合理的值&#xff0c;我们把它定下来&#xff0c;对于内部节点再划分所需最小样本数min_samples_split&#xff0c;我们暂时不能一起定下来&#xff0c;因为这个还和决策树其他的参数存在关联。下面我们再对内部节点再划分所需最小样本数min_samples_split和叶子节点最少样本数min_samples_leaf一起调参。
param_test3 &#61; {&#39;min_samples_split&#39;:range(800,1900,200), &#39;min_samples_leaf&#39;:range(60,101,10)}
gsearch3 &#61; GridSearchCV(estimator &#61; GradientBoostingClassifier(learning_rate&#61;0.1, n_estimators&#61;60,max_depth&#61;7,max_features&#61;&#39;sqrt&#39;, subsample&#61;0.8, random_state&#61;10), param_grid &#61; param_test3, scoring&#61;&#39;roc_auc&#39;,iid&#61;False, cv&#61;5)
gsearch3.fit(X,y)
gsearch3.grid_scores_, gsearch3.best_params_, gsearch3.best_score_
输出结果如下&#xff0c;可见这个min_samples_split在边界值&#xff0c;还有进一步调试小于边界60的必要。由于这里只是例子&#xff0c;所以大家可以自己下来用包含小于60的网格搜索来寻找合适的值。
([mean: 0.81828, std: 0.02251, params: {&#39;min_samples_split&#39;: 800, &#39;min_samples_leaf&#39;: 60},mean: 0.81731, std: 0.02344, params: {&#39;min_samples_split&#39;: 1000, &#39;min_samples_leaf&#39;: 60},mean: 0.82220, std: 0.02250, params: {&#39;min_samples_split&#39;: 1200, &#39;min_samples_leaf&#39;: 60},mean: 0.81447, std: 0.02125, params: {&#39;min_samples_split&#39;: 1400, &#39;min_samples_leaf&#39;: 60},mean: 0.81495, std: 0.01626, params: {&#39;min_samples_split&#39;: 1600, &#39;min_samples_leaf&#39;: 60},mean: 0.81528, std: 0.02140, params: {&#39;min_samples_split&#39;: 1800, &#39;min_samples_leaf&#39;: 60},mean: 0.81590, std: 0.02517, params: {&#39;min_samples_split&#39;: 800, &#39;min_samples_leaf&#39;: 70},mean: 0.81573, std: 0.02207, params: {&#39;min_samples_split&#39;: 1000, &#39;min_samples_leaf&#39;: 70},mean: 0.82021, std: 0.02521, params: {&#39;min_samples_split&#39;: 1200, &#39;min_samples_leaf&#39;: 70},mean: 0.81512, std: 0.01995, params: {&#39;min_samples_split&#39;: 1400, &#39;min_samples_leaf&#39;: 70},mean: 0.81395, std: 0.02081, params: {&#39;min_samples_split&#39;: 1600, &#39;min_samples_leaf&#39;: 70},mean: 0.81587, std: 0.02082, params: {&#39;min_samples_split&#39;: 1800, &#39;min_samples_leaf&#39;: 70},mean: 0.82064, std: 0.02698, params: {&#39;min_samples_split&#39;: 800, &#39;min_samples_leaf&#39;: 80},mean: 0.81490, std: 0.02475, params: {&#39;min_samples_split&#39;: 1000, &#39;min_samples_leaf&#39;: 80},mean: 0.82009, std: 0.02568, params: {&#39;min_samples_split&#39;: 1200, &#39;min_samples_leaf&#39;: 80},mean: 0.81850, std: 0.02226, params: {&#39;min_samples_split&#39;: 1400, &#39;min_samples_leaf&#39;: 80},mean: 0.81855, std: 0.02099, params: {&#39;min_samples_split&#39;: 1600, &#39;min_samples_leaf&#39;: 80},mean: 0.81666, std: 0.02249, params: {&#39;min_samples_split&#39;: 1800, &#39;min_samples_leaf&#39;: 80},mean: 0.81960, std: 0.02437, params: {&#39;min_samples_split&#39;: 800, &#39;min_samples_leaf&#39;: 90},mean: 0.81560, std: 0.02235, params: {&#39;min_samples_split&#39;: 1000, &#39;min_samples_leaf&#39;: 90},mean: 0.81936, std: 0.02542, params: {&#39;min_samples_split&#39;: 1200, &#39;min_samples_leaf&#39;: 90},mean: 0.81362, std: 0.02254, params: {&#39;min_samples_split&#39;: 1400, &#39;min_samples_leaf&#39;: 90},mean: 0.81429, std: 0.02417, params: {&#39;min_samples_split&#39;: 1600, &#39;min_samples_leaf&#39;: 90},mean: 0.81299, std: 0.02262, params: {&#39;min_samples_split&#39;: 1800, &#39;min_samples_leaf&#39;: 90},mean: 0.82000, std: 0.02511, params: {&#39;min_samples_split&#39;: 800, &#39;min_samples_leaf&#39;: 100},mean: 0.82209, std: 0.01816, params: {&#39;min_samples_split&#39;: 1000, &#39;min_samples_leaf&#39;: 100},mean: 0.81821, std: 0.02337, params: {&#39;min_samples_split&#39;: 1200, &#39;min_samples_leaf&#39;: 100},mean: 0.81922, std: 0.02377, params: {&#39;min_samples_split&#39;: 1400, &#39;min_samples_leaf&#39;: 100},mean: 0.81545, std: 0.02221, params: {&#39;min_samples_split&#39;: 1600, &#39;min_samples_leaf&#39;: 100},mean: 0.81704, std: 0.02509, params: {&#39;min_samples_split&#39;: 1800, &#39;min_samples_leaf&#39;: 100}],{&#39;min_samples_leaf&#39;: 60, &#39;min_samples_split&#39;: 1200},0.8222032996697154)
我们调了这么多参数了&#xff0c;终于可以都放到GBDT类里面去看看效果了。现在我们用新参数拟合数据&#xff1a;
gbm1 &#61; GradientBoostingClassifier(learning_rate&#61;0.1, n_estimators&#61;60,max_depth&#61;7, min_samples_leaf &#61;60, min_samples_split &#61;1200, max_features&#61;&#39;sqrt&#39;, subsample&#61;0.8, random_state&#61;10)
gbm1.fit(X,y)
y_pred &#61; gbm1.predict(X)
y_predprob &#61; gbm1.predict_proba(X)[:,1]
print "Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred)
print "AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob)
输出如下&#xff1a;
Accuracy : 0.984
AUC Score (Train): 0.908099
对比我们最开始完全不调参的拟合效果&#xff0c;可见精确度稍有下降&#xff0c;主要原理是我们使用了0.8的子采样&#xff0c;20%的数据没有参与拟合。
现在我们再对最大特征数max_features进行网格搜索
param_test4 &#61; {&#39;max_features&#39;:range(7,20,2)}
gsearch4 &#61; GridSearchCV(estimator &#61; GradientBoostingClassifier(learning_rate&#61;0.1, n_estimators&#61;60,max_depth&#61;7, min_samples_leaf &#61;60, min_samples_split &#61;1200, subsample&#61;0.8, random_state&#61;10), param_grid &#61; param_test4, scoring&#61;&#39;roc_auc&#39;,iid&#61;False, cv&#61;5)
gsearch4.fit(X,y)
gsearch4.grid_scores_, gsearch4.best_params_, gsearch4.best_score_
输出如下&#xff1a;
([mean: 0.82220, std: 0.02250, params: {&#39;max_features&#39;: 7},mean: 0.82241, std: 0.02421, params: {&#39;max_features&#39;: 9},mean: 0.82108, std: 0.02302, params: {&#39;max_features&#39;: 11},mean: 0.82064, std: 0.01900, params: {&#39;max_features&#39;: 13},mean: 0.82198, std: 0.01514, params: {&#39;max_features&#39;: 15},mean: 0.81355, std: 0.02053, params: {&#39;max_features&#39;: 17},mean: 0.81877, std: 0.01863, params: {&#39;max_features&#39;: 19}],{&#39;max_features&#39;: 9},0.822412506351626)
现在我们再对子采样的比例进行网格搜索&#xff1a;
param_test5 &#61; {&#39;subsample&#39;:[0.6,0.7,0.75,0.8,0.85,0.9]}
gsearch5 &#61; GridSearchCV(estimator &#61; GradientBoostingClassifier(learning_rate&#61;0.1, n_estimators&#61;60,max_depth&#61;7, min_samples_leaf &#61;60, min_samples_split &#61;1200, max_features&#61;9, random_state&#61;10), param_grid &#61; param_test5, scoring&#61;&#39;roc_auc&#39;,iid&#61;False, cv&#61;5)
gsearch5.fit(X,y)
gsearch5.grid_scores_, gsearch5.best_params_, gsearch5.best_score_
输出如下&#xff1a;
([mean: 0.81828, std: 0.02392, params: {&#39;subsample&#39;: 0.6},mean: 0.82344, std: 0.02708, params: {&#39;subsample&#39;: 0.7},mean: 0.81673, std: 0.02196, params: {&#39;subsample&#39;: 0.75},mean: 0.82241, std: 0.02421, params: {&#39;subsample&#39;: 0.8},mean: 0.82285, std: 0.02446, params: {&#39;subsample&#39;: 0.85},mean: 0.81738, std: 0.02236, params: {&#39;subsample&#39;: 0.9}],{&#39;subsample&#39;: 0.7},0.8234378969766262)
现在我们基本已经得到我们所有调优的参数结果了。这时我们可以减半步长&#xff0c;最大迭代次数加倍来增加我们模型的泛化能力。再次拟合我们的模型&#xff1a;
gbm2 &#61; GradientBoostingClassifier(learning_rate&#61;0.05, n_estimators&#61;120,max_depth&#61;7, min_samples_leaf &#61;60, min_samples_split &#61;1200, max_features&#61;9, subsample&#61;0.7, random_state&#61;10)
gbm2.fit(X,y)
y_pred &#61; gbm2.predict(X)
y_predprob &#61; gbm2.predict_proba(X)[:,1]
print "Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred)
print "AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob)
输出如下&#xff1a;
Accuracy : 0.984
AUC Score (Train): 0.905324
可以看到AUC分数比起之前的版本稍有下降&#xff0c;这个原因是我们为了增加模型泛化能力&#xff0c;为防止过拟合而减半步长&#xff0c;最大迭代次数加倍&#xff0c;同时减小了子采样的比例&#xff0c;从而减少了训练集的拟合程度。
下面我们继续将步长缩小5倍&#xff0c;最大迭代次数增加5倍&#xff0c;继续拟合我们的模型&#xff1a;
gbm3 &#61; GradientBoostingClassifier(learning_rate&#61;0.01, n_estimators&#61;600,max_depth&#61;7, min_samples_leaf &#61;60, min_samples_split &#61;1200, max_features&#61;9, subsample&#61;0.7, random_state&#61;10)
gbm3.fit(X,y)
y_pred &#61; gbm3.predict(X)
y_predprob &#61; gbm3.predict_proba(X)[:,1]
print "Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred)
print "AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob)
输出如下&#xff0c;可见减小步长增加迭代次数可以在保证泛化能力的基础上增加一些拟合程度。
Accuracy : 0.984
AUC Score (Train): 0.908581
最后我们继续步长缩小一半&#xff0c;最大迭代次数增加2倍&#xff0c;拟合我们的模型&#xff1a;
gbm4 &#61; GradientBoostingClassifier(learning_rate&#61;0.005, n_estimators&#61;1200,max_depth&#61;7, min_samples_leaf &#61;60, min_samples_split &#61;1200, max_features&#61;9, subsample&#61;0.7, random_state&#61;10)
gbm4.fit(X,y)
y_pred &#61; gbm4.predict(X)
y_predprob &#61; gbm4.predict_proba(X)[:,1]
print "Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred)
print "AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob)
输出如下&#xff0c;此时由于步长实在太小&#xff0c;导致拟合效果反而变差&#xff0c;也就是说&#xff0c;步长不能设置的过小。
Accuracy : 0.984
AUC Score (Train): 0.908232
以上就是GBDT调参的一个总结&#xff0c;希望可以帮到朋友们。
#coding&#61;utf-8
#coding:utf-8
#-*- coding:utf-8 -*-import pandas as pd
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import cross_validation, metrics
from sklearn.grid_search import GridSearchCVimport matplotlib.pylab as plttrain&#61; pd.read_csv(&#39;train_modified.csv&#39;)
target&#61;&#39;Disbursed&#39;# Disbursed的值就是二元分类的输出
IDcol&#61; &#39;ID&#39;
print (train[&#39;Disbursed&#39;].value_counts())x_columns&#61; [x for x in train.columns if x not in [target, IDcol]]
X&#61; train[x_columns]
y&#61; train[&#39;Disbursed&#39;]#print (X,y)#默认参数
#Accuracy : 0.9856
#AUC Score (Train): 0.862264gbm0&#61; GradientBoostingClassifier(random_state&#61;10)
gbm0.fit(X,y)
y_pred&#61; gbm0.predict(X)
y_predprob&#61; gbm0.predict_proba(X)[:,1]print (y_predprob)
print ("Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred))
print ("AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob))#调参模型1
#经过网格搜索最佳参数后&#xff0c;看看效果&#xff0c;迭代次数200是随意调整的&#xff0c;将60改为200 &#xff0c;得到了目前最好的结果&#xff1b;
#Accuracy : 0.9854
#AUC Score (Train): 0.911756
gbm1 &#61; GradientBoostingClassifier(learning_rate&#61;0.1, n_estimators&#61;200,max_depth&#61;7, min_samples_leaf &#61;60,min_samples_split &#61;1200, max_features&#61;&#39;sqrt&#39;, subsample&#61;0.8, random_state&#61;10)
gbm1.fit(X,y)
y_pred &#61; gbm1.predict(X)
y_predprob &#61; gbm1.predict_proba(X)[:,1]
print ("Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred))
print ("AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob))#调参模型2
#现在我们基本已经得到我们所有调优的参数结果了。这时我们可以减半步长&#xff0c;最大迭代次数加倍来增加我们模型的泛化能力。再次拟合我们的模型&#xff1a;
#可以看到AUC分数比起之前的版本稍有下降&#xff0c;这个原因是我们为了增加模型泛化能力&#xff0c;
#为防止过拟合而减半步长&#xff0c;最大迭代次数加倍&#xff0c;同时减小了子采样的比例&#xff0c;从而减少了训练集的拟合程度。
#Accuracy : 0.9854
#AUC Score (Train): 0.882529
gbm2 &#61; GradientBoostingClassifier(learning_rate&#61;0.05, n_estimators&#61;120,max_depth&#61;7, min_samples_leaf &#61;60,min_samples_split &#61;1200, max_features&#61;9, subsample&#61;0.7, random_state&#61;10)
gbm2.fit(X,y)
y_pred &#61; gbm2.predict(X)
y_predprob &#61; gbm2.predict_proba(X)[:,1]
print ("Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred))
print ("AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob))#调参模型3
#下面我们继续将步长缩小5倍&#xff0c;最大迭代次数增加5倍&#xff0c;继续拟合我们的模型&#xff1a;
#可见减小步长增加迭代次数可以在保证泛化能力的基础上增加一些拟合程度
#Accuracy : 0.9854
#AUC Score (Train): 0.884223
gbm3 &#61; GradientBoostingClassifier(learning_rate&#61;0.01, n_estimators&#61;600,max_depth&#61;7, min_samples_leaf &#61;60,min_samples_split &#61;1200, max_features&#61;9, subsample&#61;0.7, random_state&#61;10)
gbm3.fit(X,y)
y_pred &#61; gbm3.predict(X)
y_predprob &#61; gbm3.predict_proba(X)[:,1]
print ("Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred))
print ("AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob))#调参模型4
#最后我们继续步长缩小一半&#xff0c;最大迭代次数增加2倍&#xff0c;拟合我们的模型&#xff1a;
#输出如下&#xff0c;此时由于步长实在太小&#xff0c;导致拟合效果反而变差&#xff0c;也就是说&#xff0c;步长不能设置的过小。
#Accuracy : 0.9854
#AUC Score (Train): 0.883996
gbm4 &#61; GradientBoostingClassifier(learning_rate&#61;0.005, n_estimators&#61;1200,max_depth&#61;7, min_samples_leaf &#61;60,min_samples_split &#61;1200, max_features&#61;9, subsample&#61;0.7, random_state&#61;10)
gbm4.fit(X,y)
y_pred &#61; gbm4.predict(X)
y_predprob &#61; gbm4.predict_proba(X)[:,1]
print ("Accuracy : %.4g" % metrics.accuracy_score(y.values, y_pred))
print ("AUC Score (Train): %f" % metrics.roc_auc_score(y, y_predprob))
参考博客 &#xff1a;https://www.cnblogs.com/pinard/p/6143927.html
感谢作者&#xff0c;博客非常详尽&#xff0c;极力推荐&#xff01;