Stacking调参总结
1. 回归
训练了两个回归器,GBDT和Xgboost,用这两个回归器做stacking
使用之前已经调好参的训练器
gbdt_nxf = GradientBoostingRegressor(learning_rate=0.06,n_estimators=250,
min_samples_split=700,min_samples_leaf=70,max_depth=6,
max_features='sqrt',subsample=0.8,random_state=75)
xgb_nxf = XGBRegressor(learning_rate=0.06,max_depth=6,n_estimators=200,random_state=75)
事先建好stacking要用到的矩阵
from sklearn.model_selection import KFold,StratifiedKFold
kf = StratifiedKFold(n_splits=5,random_state=75,shuffle=True) from sklearn.metrics import r2_score train_proba = np.zeros((len(gbdt_train_data),2))
train_proba = pd.DataFrame(train_proba)
train_proba.columns = ['gbdt_nxf','xgb_nxf'] test_proba = np.zeros((len(gbdt_test_data),2))
test_proba = pd.DataFrame(test_proba)
test_proba.columns = ['gbdt_nxf','xgb_nxf']
reg_names = ['gbdt_nxf','xgb_nxf'] for i,reg in enumerate([gbdt_nxf,xgb_nxf]):
pred_list = []
col = reg_names[i]
for train_index,val_index in kf.split(gbdt_train_data,gbdt_train_label):
x_train = gbdt_train_data.loc[train_index,:].values
y_train = gbdt_train_label[train_index]
x_val = gbdt_train_data.loc[val_index,:].values
y_val = gbdt_train_label[val_index] reg.fit(x_train,y_train)
y_vali = reg.predict(x_val)
train_proba.loc[val_index,col] = y_vali
print('%s cv r2 %s'%(col,r2_score(y_val,y_vali))) y_testi = reg.predict(gbdt_test_data.values)
pred_list.append(y_testi)
test_proba.loc[:,col] = np.mean(np.array(pred_list),axis=0)

r2值最高为0.79753,效果还不是特别的好
然后用五折交叉验证,每折都预测整个测试集,得到五个预测的结果,求平均,就是新的预测集;而训练集就是五折中任意四折预测该折的训练集得到的标签的集合
因为有两个训练器,GBDT和Xgboost,所以我们得到了两列的train_proba

最后对新的训练集和测试集做回归,得到我们的结果
#使用逻辑回归做stacking
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
scalar = StandardScaler()
# train_proba = train_proba.values
# test_proba = test_proba.values scalar.fit(train_proba)
train_proba = scalar.transform(train_proba)
test_proba = scalar.transform(test_proba) lr = LogisticRegression(tol=0.0001,C=0.5,random_state=24,max_iter=10) kf = StratifiedKFold(n_splits=5,random_state=75,shuffle=True)
r2_list = []
pred_list = []
for train_index,val_index in kf.split(train_proba,gbdt_train_label):#训练集的标签还是一开始真实的训练集的标签
x_train = train_proba[train_index]
y_train = gbdt_train_label[train_index]
x_val = train_proba[val_index]
y_val = gbdt_train_label[val_index] lr.fit(x_train,y_train)
y_vali = lr.predict(x_val)
print('lr stacking cv r2 %s'%(r2_score(y_val,y_vali))) r2_list.append(r2_score(y_val,y_vali)) y_testi = lr.predict(test_proba)
pred_list.append(y_testi) print(lr.coef_,lr.n_iter_)#过拟合很严重

2. 分类
经过对每个单模型进行调参之后,我们可以把这些模型进行 stacking 集成。
如上图所示,我们将数据集分成均匀的5部分进行交叉训练,使用其中的4部分训练,之后将训练好的模型对剩下的1部分进行预测,同时预测测试集;经过5次cv之后,我们可以得到训练集每个样本的预测值,同时得到测试集的5个预测值,我们将测试集的5个测试集进行平均。有多少个基模型,我们会得到几组不同的预测值;最后使用一个模型对上一步得到预测结果再进行训练预测,得到stacking结果。stacking模型一般使用线性模型。
stacking 有点像神经网络,基模型就像底层的神经网络对输入数据进行特征的提取,如下图所示:

首先我们先定义一个DataFrame 格式数据结构荣来存储中间预测结果:
train_proba = np.zeros((len(train), 6))
train_proba = pd.DataFrame(train_proba)
train_proba.columns = ['rf','ada','etc','gbc','sk_xgb','sk_lgb'] test_proba = np.zeros((len(test), 6))
test_proba = pd.DataFrame(test_proba)
test_proba.columns = ['rf','ada','etc','gbc','sk_xgb','sk_lgb']
定义基模型,交叉训练预测
rf = RandomForestClassifier(n_estimators=700, max_depth=13, min_samples_split=30,\
min_weight_fraction_leaf=0.0, random_state=24, verbose=0) ada = AdaBoostClassifier(n_estimators=450, learning_rate=0.1, random_state=24) gbc = GradientBoostingClassifier(learning_rate=0.08,n_estimators=150,max_depth=9,
min_samples_leaf=70,min_samples_split=900,
max_features='sqrt',subsample=0.8,random_state=10) etc = ExtraTreesClassifier(n_estimators=290, max_depth=12, min_samples_split=30,random_state=24) sk_xgb = XGBClassifier(learning_rate=0.05,n_estimators=400,
min_child_weight=20,max_depth=3,subsample=0.8, colsample_bytree=0.8,
reg_lambda=1., random_state=10) sk_lgb = LGBMClassifier(num_leaves=31,max_depth=3,learing_rate=0.03,n_estimators=600,
subsample=0.8, colsample_bytree=0.9, objective='binary',
min_child_weight=0.001, subsample_freq=1, min_child_samples=10,
reg_alpha=0.0, reg_lambda=0.0, random_state=10, n_jobs=-1,
silent=True, importance_type='split') kf = StratifiedKFold(n_splits=5,random_state=233,shuffle=True) clf_name = ['rf','ada','etc','gbc','sk_xgb','sk_lgb']
for i,clf in enumerate([rf,ada,etc,gbc,sk_xgb,sk_lgb]):
pred_list = []
col = clf_name[i]
for train_index, val_index in kf.split(train,label):
X_train = train.loc[train_index,:].values
y_train = label[train_index]
X_val = train.loc[val_index,:].values
y_val = label[val_index] clf.fit(X_train, y_train)
y_vali = clf.predict_proba(X_val)[:,1]
train_proba.loc[val_index,col] = y_vali
print("%s cv auc %s" % (col, roc_auc_score(y_val, y_vali))) y_testi = clf.predict_proba(test.values)[:,1]
pred_list.append(y_testi) test_proba.loc[:,col] = np.mean(np.array(pred_list),axis=0)
使用逻辑回归做最后的stacking
scaler = StandardScaler()
train_proba = train_proba.values
test_proba = test_proba.values scaler.fit(train_proba)
train_proba = scaler.transform(train_proba)
test_proba = scaler.transform(test_proba) lr = LogisticRegression(tol=0.0001, C=0.5, random_state=24, max_iter=10) kf = StratifiedKFold(n_splits=5,random_state=244,shuffle=True)
auc_list = []
pred_list = []
for train_index, val_index in kf.split(train_proba,label):
X_train = train_proba[train_index]
y_train = label[train_index]
X_val = train_proba[val_index]
y_val = label[val_index] lr.fit(X_train, y_train)
y_vali = lr.predict_proba(X_val)[:,1]
print("lr stacking cv auc %s" % (roc_auc_score(y_val, y_vali))) auc_list.append(roc_auc_score(y_val, y_vali)) y_testi = lr.predict_proba(test_proba)[:,1]
pred_list.append(y_testi) print(lr.coef_, lr.n_iter_)
最终各个基模型和stacking模型的 auc 得分如下图所示:

分别为 0.8415,0.8506,0.8511,0.8551,0.8572,0.8580,0.8584。
Stacking调参总结的更多相关文章
- scikit-learn随机森林调参小结
在Bagging与随机森林算法原理小结中,我们对随机森林(Random Forest, 以下简称RF)的原理做了总结.本文就从实践的角度对RF做一个总结.重点讲述scikit-learn中RF的调参注 ...
- scikit-learn 梯度提升树(GBDT)调参小结
在梯度提升树(GBDT)原理小结中,我们对GBDT的原理做了总结,本文我们就从scikit-learn里GBDT的类库使用方法作一个总结,主要会关注调参中的一些要点. 1. scikit-learn ...
- word2vec参数调整 及lda调参
一.word2vec调参 ./word2vec -train resultbig.txt -output vectors.bin -cbow 0 -size 200 -window 5 -neg ...
- scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)
scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...
- 基于pytorch的CNN、LSTM神经网络模型调参小结
(Demo) 这是最近两个月来的一个小总结,实现的demo已经上传github,里面包含了CNN.LSTM.BiLSTM.GRU以及CNN与LSTM.BiLSTM的结合还有多层多通道CNN.LSTM. ...
- 漫谈PID——实现与调参
闲话: 作为一个控制专业的学生,说起PID,真是让我又爱又恨.甚至有时候会觉得我可能这辈子都学不会pid了,但是经过一段时间的反复琢磨,pid也不是很复杂.所以在看懂pid的基础上,写下这篇文章,方便 ...
- hyperopt自动调参
hyperopt自动调参 在传统机器学习和深度学习领域经常需要调参,调参有些是通过通过对数据和算法的理解进行的,这当然是上上策,但还有相当一部分属于"黑盒" hyperopt可以帮 ...
- 调参必备---GridSearch网格搜索
什么是Grid Search 网格搜索? Grid Search:一种调参手段:穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性,表现最好的参数就是最终的结果.其原理就像是在数组里找最 ...
- random froest 调参
https://blog.csdn.net/wf592523813/article/details/86382037 https://blog.csdn.net/xiayto/article/deta ...
随机推荐
- Javascript合并表格相同内容单元格示例
效果图: HTML代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- redis数据持久化的两种方式
1,AOF AOF持久化以日志的形式记录服务器所处理的每一个写.删除操作,查询操作不会记录,以文本的方式append记录,可以打开文件看到详细的操作记录.(相同数量的数据集而言,AOF文件通常要大于R ...
- ubuntu16.04下安装opencv3.4.1及其扩展模块
1.源文件下载 opencv-3.4.1.tar.gz(https://github.com/opencv/opencv/releases) opencv_contrib-3.4.1.tar.gz(h ...
- [No0000179]改善C#程序的建议2:C#中dynamic的正确用法
dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...
- [No0000168]Excle只允许用户输入纯文本,禁止用户修改单元格样式、格式等
背景:自己的模板给别人,让他填完信息上传到系统里,但别人经常不按模板的格式来填写,导致无法程序自动化.能不能在模板上把格式锁住,只允许输入纯文本,但不能改格式? 方法: 步骤一,创建你要的模板 其中, ...
- angularjs 异步请求无法更新数据
angularjs 有个问题就是第二次ajax请求数据再次赋值给 $scope.data,需要更新视图数据的时候,却不能更改视图数据,这个是因为angularjs的$watch不能监听到JS对$sco ...
- 通用网关接口 ruby perl web页面 文本处理 脚本语言
小结: 1.只要可以对标准输入输出进行操作,那么无论任何语言都可以编写CGI程序. <代码的未来> 在Ruby诞生的1993年,互联网还没有现在这样普及,因此Ruby也不是一开始就面向We ...
- URL地址中的#
1.#的涵义 代表网页中的一个位置.井号后面的字符,就是该位置的标识符.比如, http://www.baidu.com/index.html#one 就代表网页index.html的one位置.浏览 ...
- 【编译原理】c++实现自下而上语法分析及中间代码(四元式)生成
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- java之旅_高级教程_java泛型
摘自:http://www.runoob.com/java/java-generics.html JAVA泛型 java泛型(generics)是JDK5中引入的新特性,泛型提供了编译时类型安全检测机 ...