集成学习-Blending算法

集成学习

集成学习(又称模型融合)就是结合若干个体分类器(基学习器)进行综合预测,各个个体学习器通常是弱学习器。集成学习相较于个体学习在预测准确率以及稳定性上都有很大的提高。
普通机器学习:从训练数据中学习一个假设。
集成方法:试图构建一组假设并将它们组合起来,集成学习是一种机器学习范式,多个学习器被训练来解决同一个问题。

集成方法分类为:

  1. Bagging(并行训练):随机森林
  2. Boosting(串行训练):Adaboost; GBDT; XgBoost
  3. Stacking:
  4. Blending:

或者分类为串行集成方法和并行集成方法

  1. 串行模型:通过基础模型之间的依赖,给错误分类样本一个较大的权重来提升模型的性能。
  2. 并行模型的原理:利用基础模型的独立性,然后通过平均能够较大地降低误差

Blending介绍

训练数据划分为训练和验证集+新的训练数据集和新的测试集

将训练数据进行划分,划分之后的训练数据一部分训练基模型,一部分经模型预测后作为新的特征训练元模型。
测试数据同样经过基模型预测,形成新的测试数据。最后,元模型对新的测试数据进行预测。Blending框架图如下所示:
注意:其是在stacking的基础上加了划分数据

Blending流程图

在这里插入图片描述

  • 第一步:将原始训练数据划分为训练集和验证集。
  • 第二步:使用训练集对训练T个不同的模型。
  • 第三步:使用T个基模型,对验证集进行预测,结果作为新的训练数据。
  • 第四步:使用新的训练数据,训练一个元模型。
  • 第五步:使用T个基模型,对测试数据进行预测,结果作为新的测试数据。
  • 第六步:使用元模型对新的测试数据进行预测,得到最终结果。

在这里插入图片描述

案例

相关工具包加载

import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
plt.style.use("ggplot")
%matplotlib inline
import seaborn as sns

创建数据

from sklearn import datasets 
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
data, target = make_blobs(n_samples=10000, centers=2, random_state=1, cluster_std=1.0 )
## 创建训练集和测试集
X_train1,X_test,y_train1,y_test = train_test_split(data, target, test_size=0.2, random_state=1)
## 创建训练集和验证集
X_train,X_val,y_train,y_val = train_test_split(X_train1, y_train1, test_size=0.3, random_state=1)
print("The shape of training X:",X_train.shape)
print("The shape of training y:",y_train.shape)
print("The shape of test X:",X_test.shape)
print("The shape of test y:",y_test.shape)
print("The shape of validation X:",X_val.shape)
print("The shape of validation y:",y_val.shape)
 

设置第一层分类器

from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier

clfs = [SVC(probability=True),RandomForestClassifier(n_estimators=5,n_jobs=-1,criterion='gini'),KNeighborsClassifier()]

设置第二层分类器

from sklearn.linear_model import LinearRegression
lr = LinearRegression()

第一层

val_features = np.zeros((X_val.shape[0],len(clfs)))
test_features = np.zeros((X_test.shape[0],len(clfs)))

for i,clf in enumerate(clfs):
    clf.fit(X_train,y_train)
    val_feature = clf.predict_proba(X_val)[:,1]
    test_feature = clf.predict_proba(X_test)[:,1]
    val_features[:,i] = val_feature
    test_features[:,i] = test_feature

第二层

lr.fit(val_features,y_val)

输出预测的结果

lr.fit(val_features,y_val)
from sklearn.model_selection import cross_val_score
cross_val_score(lr,test_features,y_test,cv=5)
 

参考资料:https://github.com/datawhalechina/team-learning-data-mining/tree/master/EnsembleLearning

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoAI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值