实现机器学习的循序渐进指南VII——Blending & Stacking

目录

介绍

混合(Blending)模型

混合(Blending)架构

混合(Blending)实现

混合(Blending)分类

堆叠(Stacking)模型

堆叠(Stacking)架构

堆叠(Stacking)实现

堆叠(Stacking)分类

结论与分析


可访问 实现机器学习的循序渐进指南系列汇总,获取本系列完成文章列表。 

介绍

混合(Blending)和堆叠(Stacking)是一种模型融合方法,而不是传统的机器学习算法。Netflix中的顶级表演者使用混合(Blending),这被认为是一种堆叠(Stacking)形式。混合(Blending)更像是工程方法,因此,本文中的方程式很少。 

混合(Blending)模型

混合(Blending)架构

一般混合模型中有两层。在第一层中,我们从原始训练集创建一个小型holdset。剩余训练数据用于生成模型以给出保持集的预测。在第二层中,使用holdset作为训练集来训练第二层中的模型。混合的整个过程如下所示。

                                                                                     图1.混合架构

混合(Blending)实现

在第一层混合中,有几个单独的分类器,它们可以是不同的或相同的。所有分类器使用图1中所示的蓝色数据生成模型。然后,训练的模型给出橙色数据的预测,其将用作第二层中的训练数据。第一层的代码如下所示:

train_data1, train_data2, train_label1, train_label2 = train_test_split(train_data, train_label, test_size=0.5, random_state=2019)
# train set in the second layer
train_predict_feature = np.zeros((train_data2.shape[0], self.k))
trained_model = []

# the first layer in Blending
for j, clf in enumerate(self.classifier_set):
    # train each submodel
    print(j, clf)
    clf.train(train_data1, train_label1)
    train_predict_feature[:, j] = clf.predict(train_data2)[:, 0]
    # save the trained model in the first layer
    trained_model.append(clf)

在第二层中,我们使用单层感知器作为分类器。我们使用第一层生成的训练数据训练分类器,其代码如下所示:

# the second layer in Blending
layer2_clf = PerceptronClassifier()
layer2_clf.train(train_predict_feature, train_label2)

混合(Blending)分类

对于混合的测试过程,还有两层。在第一层中,测试集被输入到第一层中的训练分类器,即,用蓝色数据训练的分类器。在第二层中,预测结果被输入到第二层中的训练分类器,即,用橙色数据训练的分类器。然后,输出是最终预测。测试代码如下所示:

test_predict_feature = np.zeros((test_data.shape[0], self.k))
# the first layer in Blending
for j, clf in enumerate(self.layer1_classifier_set):
    test_predict_feature[:, j] = clf.predict(test_data)[:, 0]

# the second layer in Blending
probability = self.layer2_classifier.predict(test_predict_feature)

堆叠(Stacking)模型

堆叠(Stacking)架构

与混合(Blending)一样,堆叠(Stacking)模型中也有两层。在第一层中,使用交叉验证对模型进行训练,并对数据折叠进行预测。在第二层中,所有折叠预测都作为整个数据集的预测进行汇总。每个分类器的预测被融合以获得最终预测。

                                                                                             图2.堆叠架构

堆叠(Stacking)实现

在第一层堆叠中,训练集由交叉验证生成。对于训练集T,它将被等分为若干子集,即T = {t 1t 2...t k }。在交叉验证过程中,每个机器学习算法训练具有(T-t i)的子模型作为图1中的蓝色数据,并给出t i预测。因此,对于k倍交叉验证和n种类型的机器学习算法,总共有nxk子模型。此外,对于每个机器学习算法,它将通过对每个折叠预测进行联合来给出对整个训练集的预测。因此,我们总共得到n预测。第一层的代码如下所示:

for j, clf in enumerate(self.classifier_set):
    # train each submodel
    subtrained_model = []
    # cross validation
    for (train_index, test_index) in skf.split(train_data, train_label):
        X_train, X_test = train_data[train_index], train_data[test_index]
        y_train, y_test = train_label[train_index], train_label[test_index]
        # train and save the model trained with S-si
        clf.train(X_train, y_train)
        subtrained_model.append(clf)
        # get the prediction feature for each sub model
        prediction_feature[test_index, j] = clf.predict(X_test)[:, 0]
    # save the models
    trained_model.append(subtrained_model)

堆叠(Stacking)分类

在第二层混合中,我们使用分类器来融合第一层的预测。我更愿意在这里引入另一种融合方法,而不是再次使用分类器。有一些简单的方法可以融合预测,称为平均,投票和加权。平均是计算预测的平均值作为最终预测。投票是选择大多数预测作为最终预测。加权是为差异源分配权重并计算加权和作为最终预测。

pre_prediction = np.zeros((test_data.shape[0], self.n_folds))
# the first layer in Stacking
for j, sub_model in enumerate(self.trained_classifier_set):
    sub_prediction_feature = np.zeros((test_data.shape[0], self.n_folds))
    i = 0
    for clf in sub_model:
        sub_prediction_feature[:, i] = clf.predict(test_data)[:, 0]
        i = i + 1
        pre_prediction[:, j] = sub_prediction_feature.mean(1)

test_num = test_data.shape[0]
prediction = np.zeros([test_num, 1])
probability = np.zeros([test_num, 1])
# the second layer in Stacking
if self.fusion_type == "Averaging":
    probability = pre_prediction.mean(1)
elif self.fusion_type == "Voting":
    probability = np.sum(pre_prediction, axis=1)/self.k
elif self.fusion_type == "Weighing":
    w = [i/i.sum() for i in pre_prediction]
    probability = np.sum(np.multiply(pre_prediction, w), axis=1)

prediction = (probability > 0.5) * 1

结论与分析

混合(Blending) 和堆叠(Stacking)是不同的融合方法,其性能受所选分类器的影响。因此,有必要花时间在它上面。在这篇文章中,我们使用下面的分类器集

clfs = [PerceptronClassifier(), PerceptronClassifier(), LogisticRegressionClassifier(), LogisticRegressionClassifier()]

混合检测性能:

堆叠检测性能:

检测性能如上所示。很容易知道,当运行时间稍大时,检测性能优于其他性能

可以在MachineLearning找到本文中的相关代码和数据集  

有兴趣的小伙伴可以查看上一篇下一篇

 

原文地址:https://www.codeproject.com/Articles/4354591/Step-by-Step-Guide-to-Implement-Machine-Learning-5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值