机器学习基础—集成学习Task12(Blending)

导语:
本次任务的主题是“Blending集成学习算法”。
学习链接:
集成学习: EnsembleLearning项目-github.

1.Blending原理

Blending是简化版的Stacking:
stacking严格来说并不是一种算法,而是精美而又复杂的,对模型集成的一种策略。Stacking集成算法可以理解为一个两层的集成第一层含有多个基础分类器,把预测的结果(元特征)提供给第二层, 而第二层的分类器通常是逻辑回归,他把一层分类器的结果当做特征做拟合输出预测结果
详情可参考博客:
https://blog.csdn.net/maqunfi/article/details/82220115
https://blog.csdn.net/sinat_35821976/article/details/83622594
Blending集成学习步骤:

  • (1) 将数据划分为训练集和测试集(test_set),其中训练集需要再次划分为训练集(train_set)和验证集(val_set);
  • (2) 创建第一层的多个模型,这些模型可以使同质的也可以是异质的;
  • (3) 使用train_set训练步骤2中的多个模型,然后用训练好的模型预测val_set和test_set得到val_predict, test_predict1;
  • (4) 创建第二层的模型,使用val_predict作为训练集训练第二层的模型;
  • (5) 使用第二层训练好的模型对第二层测试集test_predict1进行预测,该结果为整个测试集的结果。

示意图: (来源:https://blog.csdn.net/maqunfi/article/details/82220115)在这里插入图片描述
Blending缺点
blending只使用了一部分数据集作为留出集进行验证,也就是只能用上数据中的一部分,实际上这对数据来说是很奢侈浪费的。

2.Blending实践

还是以iris数据集作为分类的实例,比较单一SVCBlending集成的分类效果:
Blending的第一层的三个基模型为SVC、RFC和KNC,输出每个类别(iris共三类)的预测概率,选择第一类和第二类的概率作为第二层的特征(因为这三个概率值总和为1,其中只有两个线性独立):
第二层的模型为线性回归决策树回归模型,将第一层提取的特征作为输入,预测测试集的y类别,验证模型分类效果。
代码:

import pandas as pd
import numpy as np
import sklearn
from sklearn import datasets
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

#导入数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
feature = iris.feature_names
data = pd.DataFrame(X,columns=feature)
data['target'] = y

## 创建训练集和测试集
X_train1,X_test,y_train1,y_test = train_test_split(X, y, 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()
# 设置第二层分类器
from sklearn.tree import DecisionTreeClassifier
DTC = DecisionTreeClassifier()

# 输出第一层的验证集结果与测试集结果
val_features = np.zeros((X_val.shape[0],len(clfs)*2))  # 初始化验证集结果
test_features = np.zeros((X_test.shape[0],len(clfs)*2))  # 初始化测试集结果

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

# 将第一层的验证集的结果输入第二层训练第二层分类器,线性回归LR
lr.fit(val_features,y_val)
# 输出预测的结果
from sklearn.model_selection import cross_val_score
score = cross_val_score(lr,test_features,y_test,cv=5)
print("Blending交叉验证结果(LR):",score)
print("Blending平均值:",np.mean(score))

# 将第一层的验证集的结果输入第二层训练第二层分类器,决策树回归DTC
DTC.fit(val_features,y_val)
# 输出预测的结果
from sklearn.model_selection import cross_val_score
score = cross_val_score(DTC,test_features,y_test,cv=5)
print("Blending交叉验证结果(DTC):",score)
print("Blending平均值:",np.mean(score))

# 输出预测的结果,SVC单模型
from sklearn.model_selection import cross_val_score
score = cross_val_score(SVC(),X_test,y_test,cv=5)
print("SVC交叉验证结果:",score)
print("SVC平均值:",np.mean(score))

结果:
Blending交叉验证结果(LR): [0.95744026 0.99947401 0.96188799 0.83160796 0.96853499]
Blending平均值: 0.9437890427044632
Blending交叉验证结果(DTC): [1. 1. 1. 1. 1.]
Blending平均值: 1.0

SVC交叉验证结果: [1. 1. 1. 1. 0.83333333]
SVC平均值: 0.9666666666666666

从结果可以看出,在5折交叉验证的方式下,采用SVC单模型分类的平均得分为0.97,已经很高了。当以线性回归作为blending第二层时,blending平均得分为0.94,还不如SVC单模型的分类效果。当以决策树回归作为blending第二层时,blending在每一折的得分都为1,取得了最好的效果!因此blending集成方式是有利于提高模型的准确度的,但对基模型与第二层的模型的选择有一定要求,若模型选择不合适或第一层提取到的特征不合适,blending的结果可能弱于单模型!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值