python stacking_详解 Stacking 的 python 实现[通俗易懂]

1.什么是stackingstacking就是当用初始训练数据学习出若干个基学习器后,将这几个学习器的预测结果作为新的训练集,来学习一个新的学习器。2.代码:例如我们用RandomForestClassifier,ExtraTreesClassifier,GradientBoostingClassifier作为第一层学习器:#Ourlevel0classifiersclfs…

大家好,又见面了,我是你们的朋友全栈君。

1. 什么是 stacking

stacking 就是当用初始训练数据学习出若干个基学习器后,将这几个学习器的预测结果作为新的训练集,来学习一个新的学习器。

5905f19c4df6

2. 代码:

例如我们用 RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier 作为第一层学习器:

# Our level 0 classifiers

clfs = [

RandomForestClassifier(n_estimators = n_trees, criterion = ‘gini’),

ExtraTreesClassifier(n_estimators = n_trees * 2, criterion = ‘gini’),

GradientBoostingClassifier(n_estimators = n_trees),

]

接着要训练第一层学习器,并得到第二层学习器所需要的数据,这里会用到 k 折交叉验证。

1. 先用初始训练集训练 clf,并得到第二层的训练数据 blend_train:

第 j 个学习器,共经过 nfolds 次交叉验证,每一次会得到当前验证集角标上的预测值,nfolds 之后得到和初始训练集一样大小的集合:

blend_train[cv_index, j] = clf.predict(X_cv)

5905f19c4df6

2. 再用 clf 对 test 集进行预测,来得到第二层的测试数据 blend_test:

即每个第一层学习器在每次 fold 时,用学习器对初识测试集进行预测,n 次之后,对所有结果取平均值,得到第 j 个学习器在测试集上的预测结果:

blend_test_j[:, i] = clf.predict(X_test)

blend_test[:, j] = blend_test_j.mean(1)

5905f19c4df6

这样第一层的每个学习器,都会得到一列训练数据和一列测试数据为第二层的学习器所用。

# For each classifier, we train the number of fold times (=len(skf))

for j, clf in enumerate(clfs):

print ‘Training classifier [%s]’ % (j)

blend_test_j = np.zeros((X_test.shape[0], len(skf))) # Number of testing data x Number of folds , we will take the mean of the predictions later

for i, (train_index, cv_index) in enumerate(skf):

print ‘Fold [%s]’ % (i)

# This is the training and validation set

X_train = X_dev[train_index]

Y_train = Y_dev[train_index]

X_cv = X_dev[cv_index]

Y_cv = Y_dev[cv_index]

clf.fit(X_train, Y_train)

# This output will be the basis for our blended classifier to train against,

# which is also the output of our classifiers

blend_train[cv_index, j] = clf.predict(X_cv)

blend_test_j[:, i] = clf.predict(X_test)

# Take the mean of the predictions of the cross validation set

blend_test[:, j] = blend_test_j.mean(1)

3. 接着用 blend_train, Y_dev 去训练第二层的学习器 LogisticRegression:

# Start blending!

bclf = LogisticRegression()

bclf.fit(blend_train, Y_dev)

blend_train = np.zeros((X_dev.shape[0], len(clfs))),这个集合是有几个学习器就有几列:

5905f19c4df6

4. 再用 bclf 来预测测试集 blend_test,并得到 score:

# Predict now

Y_test_predict = bclf.predict(blend_test)

score = metrics.accuracy_score(Y_test, Y_test_predict)

print ‘Accuracy = %s’ % (score)

blend_test = np.zeros((X_test.shape[0], len(clfs))),也是有几个学习器就有几列:

5905f19c4df6

整体流程简图如下:

5905f19c4df6

推荐阅读 历史技术博文链接汇总

http://www.jianshu.com/p/28f02bb59fe5

也许可以找到你想要的:

[入门问题][TensorFlow][深度学习][强化学习][神经网络][机器学习][自然语言处理][聊天机器人]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126552.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月8日 下午12:00
下一篇 2022年4月8日 下午12:20


相关推荐

  • SVN提示update更新成功,但是本地文件却没有更新

    SVN提示update更新成功,但是本地文件却没有更新

    2021年10月20日
    45
  • shiro框架是什么_中国历史知识框架

    shiro框架是什么_中国历史知识框架Shiro 1.权限管理概述2.Shiro权限框架  2.1概念  2.2ApacheShiro与SpringSecurity区别3.Shiro认证  3.1基于ini认证  3.2自定义Realm–认证4.Shiro授权  4.1基于ini授权  4.2自定义realm–授权5.项目集成shiro认证-授权注意点  5.1认证  5.2授权  5.3注解@RequiresPermissions()  5.4标签式权限验证6.S

    2025年10月9日
    5
  • hexo博客主题推荐_wordpress社交主题

    hexo博客主题推荐_wordpress社交主题crystalBlog上篇hexo博客搭建及主题优化(一)主题优化二21.网站log设置主题目录下的_config.yml配置文件中:#配置网站favicon和网站LOGO##本地#favicon:/favicon.png#logo:/medias/logo.png#此处我用的CDN,也可以使用本地文件favicon:https://cdn.jsdelivr.net/gh/guixinchn/image/blog/favicon.pnglogo:https://cdn

    2022年8月30日
    7
  • htons()函数用法

    htons()函数用法转自 http www cnblogs com CBDoctor archive 2012 10 28 2743109 html 在 C C 写网络程序的时候 往往会遇到字节的网络顺序和主机顺序的问题 这是就可能用到 htons ntohl ntohs htons 这 4 个函数 网络字节顺序与本地字节顺序之间的转换函数 nbsp nbsp nbsp nbsp nbsp nbsp htonl H

    2026年1月21日
    1
  • linux卸载命令_linux卸载服务命令

    linux卸载命令_linux卸载服务命令linux上使用rpm安装的一些软件,该如何进行卸载呢?卸载步骤:1、先使用rpm-qa|grep软件包名称例如卸载mysql:rpm-qa|grepmysql2、使用rpm-e–nodeps文件包名称rpm-e–nodepsmysql-5.0.77-4.el5_6.6rpm-e–nodepslibdbi-dbd-mysql-0.8.1a-1.2.2rpm-e–nodepsmysql-5.0.77-4.el5_6.6rpm..

    2026年2月17日
    6
  • 初中数学课程与信息技术的整合[通俗易懂]

    初中数学课程与信息技术的整合[通俗易懂]2.1基本工具介绍 22.1.1滑动的梯子上的猫 22.1.2智能画笔挥洒自如 72.1.3选了再做谋而后动 92.1.4公式输入即打即现 102.1.5动态测量功能多多 152.2文本命令应有尽有 182.2.1点可不简单 182.2.2直线面面观 222.2.3圆和圆弧很重要 232.2.4圆锥曲线条件多 242.2.5函数曲线最有用 252.2.6图形变换功能强 2…

    2022年5月12日
    43

发表回复

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

关注全栈程序员社区公众号