(十二)集成学习(下)——Blending

参考:DataWhale教程链接

集成学习(上)所有Task:

(一)集成学习上——机器学习三大任务

(二)集成学习上——回归模型

(三)集成学习上——偏差与方差

(四)集成学习上——回归模型评估与超参数调优

(五)集成学习上——分类模型

(六)集成学习上——分类模型评估与超参数调优

(七)集成学习中——投票法

(八)集成学习中——bagging

(九)集成学习中——Boosting简介&AdaBoost

(十)集成学习中——GBDT

(十一)集成学习中——XgBoost、LightGBM

(十二)集成学习(下)——Blending

(十三)集成学习(下)——Stacking

(十四)集成学习(下)——幸福感预测

(十五)集成学习(下)——蒸汽量预测

Blending集成学习算法

Blending集成学习方式:

  • (1) 将数据划分为训练集TrainData和测试集TestData,其中训练集需要再次划分为训练集Train_TrainData和验证集Train_ValData;

  • (2) 构建第一层模型:选择 M M M个基模型(对Train_TrainData数据集进行训练),这些模型可以使同质的也可以是异质的;

  • (3) 训练第一层模型:使用Train_TrainData训练步骤2中的 M M M个模型,然后用训练好的 M M M个模型预测Train_ValData得到val_predict;

  • (4) 构建第二层的模型:一般是逻辑回归;

  • (5) 训练第二层的模型:以Train_ValData的特征为输入,以val_predict为因变量训练第二层的模型;

    至此,模型训练完成
    接下来是模型预测

  • (6) 模型预测:用TestData走一遍第一层模型,得到test_predict1,再用test_predict1作为输入走一遍第二层模型进行预测,该结果为整个测试集的结果。

Blending集成方式的优劣:

  • 优点:实现简单粗暴,没有太多的理论的分析。
  • 缺点:只使用了一部分数据集作为留出集进行验证,也就是只能用上数据中的一部分,实际上这对数据来说是很奢侈浪费的。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.tree import DecisionTreeClassifier
from itertools import product
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris

data = load_iris()
iris_data = data.data
x = pd.DataFrame(data.data, columns=data.feature_names)[['sepal length (cm)', 'sepal width (cm)']]
x = data.data[:, :2]
y = data.target
# 创建训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1, stratify=y)
# 创建训练集和验证集
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.3, random_state=1)

x_min = x_train[:, 0].min() - 1
x_max = x_train[:, 0].max() + 1
y_min = x_train[:, 1].min() - 1
y_max = x_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

# blending
# 第一分类器
# clfs = [SVC(probability=True), RandomForestClassifier(n_estimators=5, n_jobs=-1, criterion='gini'),KNeighborsClassifier()]
clfs = [SVC(probability=True), RandomForestClassifier(n_estimators=5, n_jobs=-1, criterion='gini')]
# 设置第二层分类器
# dtc = DecisionTreeClassifier(max_depth=5)
knc=KNeighborsClassifier()
# 输出第一层的验证集结果与测试集结果
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
# 将第一层的验证集的结果输入第二层训练第二层分类器
knc.fit(val_features, y_val)
Z = knc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
colors = ListedColormap(['#AAAAFF', '#AAFFAA', '#FFAAAA'])
plt.figure()
plt.contourf(xx, yy, Z, cmap=colors, alpha=0.3)
plt.scatter(x[:, 0], x[:, 1], c=y)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值