Python machine learning fusion model: Stacking and Blending (with code)

1 Stacking method Stacking

Can a weak system become a strong system?

picture


When you are faced with a complex classification problem, as is often the case in financial markets, different approaches may emerge when searching for a solution. Although these methods can estimate classifications, sometimes they are not better than other classifications. In this case, the logical choice is to keep them all and then create the final system by integrating the parts. This diversified approach is one of the most convenient: split your decisions between several systems to avoid putting all your eggs in one basket.

Once I have a large number of estimates for this situation, how can I combine the decisions of N subsystems? As a quick answer, I can make an average decision and use it. But is there a different way to fully utilize my subsystem? Of course!

Think creatively!
Several classifiers with a common goal are called multi-classifiers. In machine learning, a multi-classifier is a set of different classifiers that are estimated and fused together to get a result that combines them. Many terms are used to refer to multi-classifiers: multi-model, multi-classifier system, combination classifier, decision committee, etc. They can be divided into two broad categories:

Integrated method: refers to using the same learning technology to combine a set of systems to create a new system. Bagging and lifting are the most extended.
Hybrid methods: Taking a diverse set of learners and combining them using new learning technologies. Stacking (or stacked generalization) is one of the main hybrid multi-classifiers.

How to build a multi-classifier powered by Stacking.

picture

picture

stacking workflow

picture

.

Meta-classifiers can be trained on predicted class labels or on predicted class probabilities.

picture

picture

Give an example of stacking to predict the trend of the EURUSD

Imagine that I want to estimate the trend of EURUSD (EUR/USD trend). First, I turned my problem into a classification problem, so I separated the price data into two types (or classes): upward and downward movements. It’s not my intention to second guess every move I make every day. I only want to detect the main trends: long trades up (class = 1) and short trades down (class = 0).

picture

I've done a posteriori split; I mean all historical data is used to decide the classes, so it takes into account some future information. Therefore, I currently cannot ensure iup or down motion. Therefore, an estimate is needed for today's course.

picture

For the purposes of this example, I designed three separate systems. They are three different learners using different sets of attributes. It doesn't matter whether you use the same learner algorithm or they share some/all properties; the point is that they must be different enough to warrant diversity.

picture

They then trade based on these probabilities: if E is above 50%, it means going long, the larger E is. If E is below 50%, it is a short entry, the smaller the E.

picture

Not ideal, just better than random

picture

Systematic errors are also less correlated

picture

Can a group of weaker players form a dream team?
The purpose of building multiple classifiers is to achieve better predictive performance than any single classifier can achieve. Let's see if this is the case.

The method I will use in this example is based on the Stacking algorithm. The idea of ​​Stacking is that the output of a main classifier called a level 0 model will be used as attributes of another classifier called a meta-model to approximate the same classification problem. The metamodel is left to figure out the merging mechanism. It will be responsible for connecting the reply and true classification of the level 0 model.

The rigorous procedure involves splitting the training set into disjoint sets. Then train each level 0 learner on the entire data, exclude one group, and apply it to the excluded group. By repeating for each group, an estimate of each data is obtained for each learner. These estimates will become the attributes of the trained meta-model or level 1 model. Since my data is a time series, I decided to use the set from day 1 to day d-1 to construct the estimate for day d.

picture

Which mode does this work with?
The meta-model can be a classification tree, a random forest, a support vector machine... any classification learner is effective. For this example, I chose to use the nearest neighbor algorithm. This means that the metamodel will estimate the categories of new data to discover similar configurations of class 0 classifications in past data, and will then assign categories to these similar situations.

Let’s see how well my dream team turned out…

The average error value of the stacking model is the lowest

picture

Conclusion
This is just one example of the large number of multi-classifiers available. Not only can they help you incorporate part of your solution into a unique answer through modern and original techniques, but they can also create a truly dream team. There is also significant room for improvement in the way individual components are integrated into a system.

So, next time you need a combination, spend a little more time researching the possibilities. Avoid traditional averages and explore more sophisticated approaches through force of habit. They may give you extra performance

Model fusion application in kaggle competition

Model fusion is a very powerful technique that can improve the accuracy of various ML tasks. In this article, I will share my approach to integration in Kaggle competitions.

For the first part, we look at creating an integration from a commit file. The second part will look at creating ensembles through stacked generalization/hybridization.

I answer why ensemble reduces generalization errors. Finally, I show different integration methods, along with their results and code for you to try yourself. Kaggle Ensembling Guide I answer why ensembles reduce generalization errors. Finally, I show different integration methods, along with their results and code for you to try yourself.

The stacked generation method is a completely different method of combining multiple models. It talks about the concept of combined learners, but it is used less than bagging and boosting. It is not like bagging and boosting, but It is to combine different models. The specific process is as follows:
1. Divide the training data set into two disjoint sets.
2. Train multiple learners on the first set.
3. Test these learners on the second set
4. Use the prediction results obtained in the third step as input, and use the correct response as Output, train a high-level learner,
What needs to be noted here is the effect of steps 1-3 and cross-validation. We are not using winner-take-all, but using a non-linear combination learner method.

picture

All trained base models will be used to predict the entire training set. The predicted value of the j-th base model for the i-th training sample will be used as the j-th feature value of the i-th sample in the new training set. Finally, based on the new training set for training. In the same way, the prediction process must first go through the predictions of all base models to form a new test set, and finally predict the test set:

Below we introduce a powerful stacking tool, the mlxtend library, which can quickly stack the sklearn model.

StackingClassifier uses API and parameter analysis:

StackingClassifier(classifiers, meta_classifier, use_probas=False,  average_probas=False, verbose=0, use_features_in_secondary=False)

parameter:

classifiers: base classifiers, array form, [cl1, cl2, cl3]. The attributes of each base classifier are stored in the class attribute self.clfs_.
meta_classifier: target Classifier, which is a classifier that combines the previous classifiers
use_probas: bool (default: False). If set to True, then the input of the target classifier is the category probability value of the previous classification output. Not a category label
average_probas: bool (default: False), used to set whether the previous parameter uses the average when using probability value output.
verbose : int, optional (default=0). Used to control the log output during use. When verbose = 0, nothing is output. When verbose = 1, the serial number and name of the regressor are output. verbose = 2, output detailed parameter information. verbose > 2, Automatically set verbose to less than 2, verbose -2.
use_features_in_secondary: bool (default: False). If set to True, the final target classifier will be based on The classifier is trained on the data generated simultaneously with the original data set. If set to False, the final classifier will only be trained using the data produced by the base classifier.

Attributes:
clfs_: attributes of each base classifier, list, shape is [n_classifiers].
meta_clf_: attributes of the final target classifier

method:

fit(X, y)
fit_transform(X, y=None, fit_params)
get_params(deep=True), if using sklearn GridSearch method, then return the parameters of the classifier.
predict(X)
predict_proba(X)
score(X, y, sample_weight=None), for the given data Set and given label, return evaluation accuracy
set_params(params), set the parameters of the classifier, the setting method of params is the same as the format of sklearn 

Part of the actual code of the python fusion model

#原创公众号python风控模型
from sklearn.datasets import load_iris 
from mlxtend.classifier import StackingClassifier 
from mlxtend.feature_selection import ColumnSelector 
from sklearn.pipeline import make_pipeline 
from sklearn.linear_model import LogisticRegression 
   
iris = load_iris() 
X = iris.data 
y = iris.target 
   
pipe1 = make_pipeline(ColumnSelector(cols=(0, 2)), 
                      LogisticRegression()) 
pipe2 = make_pipeline(ColumnSelector(cols=(1, 2, 3)), 
                      LogisticRegression()) 
   
sclf = StackingClassifier(classifiers=[pipe1, pipe2],  
                          meta_classifier=LogisticRegression()) 
   
sclf.fit(X, y)  

1.1 Basic idea of ​​stacking method

Stacking method Stacking is the most popular method in the field of model fusion in recent years. It is not only one of the most commonly used fusion methods by competition champion teams, but also one of the solutions that will be considered when actually implementing artificial intelligence in industry. As a fusion method of strong learners, Stacking combines the three major advantages ofgood model effect, strong interpretability, and adaptability to complex data, and is the most advanced in the field of fusion. A practical pioneering approach. Among the many applications of stacking, the practical GBDT+LR stacking in CTR (advertising click-through rate prediction) is particularly famous. Therefore, in the main course of "2022 Machine Learning Practice", after explaining the common stacking methods, I will explain the usage of GBDT+LR in CTR in detail, and complete a CTR practice.

What kind of algorithm is Stacking? Its core idea is actually very simple - first, as shown in the figure below, there are two layers of algorithms in the Stacking structure. The first layer is called level 0, and the second layer is called level 1. Level 0 may contain one or more strong algorithms. Learner, while level 1 can only contain one learner. During training, data will first be input to level 0 for training. After training, each algorithm in level 0 will output corresponding prediction results. We piece together these prediction results into a new feature matrix, and then input it into the level 1 algorithm for training. The final prediction result output by the fusion model is the result output by the level 1 learner.

image

In this process, the prediction results output by level 0 are generally arranged as follows:

Learner 1 Learner 2 ... learnern
Sample 1 xxx xxx ... xxx
Sample 2 xxx xxx ... xxx
Sample 3 xxx xxx ... xxx
... ... ... ... ...
Sample m xxx xxx ... xxx

The first column is the result output by learner 1 on all samples, the second column is the result output by learner 2 on all samples, and so on.
At the same time, multiple strong learners trained on level 0 are called base learners (base-model), also called individual learners. The learner trained on level1 is called meta-learner (meta-model). According to industry practice, the learner at level 0 is a learner with high complexity and strong learning ability, such as ensemble algorithm and support vector machine, and < /span>, such as decision trees, linear regression, logistic regression, etc. There is such a requirement because the responsibility of the algorithms at level 0 is to find the relationship between the original data and the label, that is, to establish the hypothesis between the original data and the label, so it requires strong learning capabilities. However, the responsibility of the level 1 algorithm is to fuse the assumptions made by the individual learners and finally output the results of the fusion model, which is equivalent to finding the "best fusion rule" rather than directly establishing assumptions between the original data and labels. Learners at level 1 are highly interpretable and relatively simple learners

Speaking of which, I wonder if you have noticed that The essence of Stacking is to let the algorithm find the fusion rules. Although most people may have never been exposed to a series structure similar to the Stacking algorithm, in fact the process of Stacking is completely consistent with the voting method and the averaging method:

image

In the voting method, we use voting to fuse the results of strong learners. In the averaging method, we use averaging to fuse the results of strong learners. In the stacking method, we use algorithms to fuse the results of strong learners. When the algorithm on level 1 is linear regression, we are actually solving the weighted sum of all strong learner results, and the process of training linear regression is the process of finding the weight of the weighted sum. Similarly, when the algorithm on level 1 is logistic regression, we are actually solving the weighted sum of all strong learner results, and then applying the sigmoid function based on the sum. The process of training logistic regression is the process of finding the weights of the weighted sum. The same goes for any other simple algorithm.

Although for most algorithms, it is difficult to find a clear name like "weighted sum" to summarize the fusion rules found by the algorithm, but in essence, the level 1 algorithm is just learning how to combine level The results output on 0 are better combined, so Stacking is a method of merging the results of the learner by training the learner. The averaging method is to average the output results, and the voting method is to vote the output results. The first two are artificially defined fusion methods, but this Stacking is to let the machine help us find the best fusion. method.  The fundamental advantage of this method is that we can train the meta-learner at level 1 in the direction of minimizing the loss function, while other fusion methods can only ensure that the fusion result has a certain promote. Therefore Stacking is a more effective method than Voting and Averaging. In practical applications, Stacking often outperforms voting or averaging methods.
After we understand the essence of Stacking, many detailed problems in the implementation process will be easily solved, such as:

  • Do you need to perform precise parameter adjustments on the fusion algorithm?

The individual learner is coarse-tuned and the meta-learner is fine-tuned. If the fit is insufficient, both types of learners can be fine-tuned. Theoretically, the closer the algorithm output is to the real label, the better. However, individual learners can easily overfit after being fine-tuned and then fused.

  • How to choose the individual learner algorithm to maximize the effect of stacking?

Consistent with voting and averaging, control overfitting, increase diversity, and pay attention to the overall computing time of the algorithm.

  • Can the individual learner be a less complex algorithm such as logistic regression or decision tree? Can the meta-learner be a very complex algorithm like xgboost?

All are OK, everything is subject to the model effect. For level 0, when adding weak learners to increase model diversity and the effects of weak learners are better, these algorithms can be retained. For level 1, any algorithm can be used as long as it does not overfit. Personal recommendation is that you can use more complex algorithms for classification, but it is best to use simple algorithms for regression.

  • Can level 0 and level 1 algorithms use different loss functions?

Yes, because different loss functions actually measure similar differences: the difference between the true value and the predicted value. However, different losses have different sensitivities to differences, and it is recommended to use similar loss functions if possible.

  • Can level 0 and level 1 algorithms use different evaluation metrics?

Personal suggestionThe algorithms on level 0 and level 1 must use the same model evaluation index. Although two groups of algorithms are connected in series in Stacking, the training of these two groups of algorithms is completely separated. In deep learning, we also have a similar structure of powerful algorithms connected in series with weak algorithms. For example, a convolutional neural network consists of a powerful convolutional layer and a weak linear layer connected in series. The main responsibility of the convolutional layer is to find out the relationship between features and labels. The main responsibility of the linear layer is to integrate assumptions and output. However, in deep learning, the training of all layers on a network is performed simultaneously, and the weights on the entire network need to be updated each time the loss function is reduced. However, in Stacking, the algorithm on level 1 does not affect the results of level 0 at all when adjusting the weight. Therefore, in order to ensure that the two groups of algorithms can obtain the results we want after the final fusion, the only evaluation index must be used during training. Baseline for training.

2 Implement stacking in sklearn

class sklearn.ensemble.StackingClassifier(
estimators,
final_estimator=None, *,
cv=None,
stack_method="auto",
n_jobs=None,
passthrough=False,
verbose=0)

class sklearn.ensemble.StackingRegressor(
estimators,
final_estimator=None,*,
cv=None,
n_jobs=None,
passthrough=False,
verbose=0)

parameter illustrate
estimators List of individual evaluators. In sklearn, when only using a single evaluator as the individual evaluator, the
model can run, but the effect is often not very good.
final_estimator meta-learner, there can only be one evaluator. When the fusion model performs a classification task, the meta-learner must be a classification algorithm.
When the fusion model performs a regression task, the meta-learner must be a regression algorithm.
cv is used to specify the specific type, fold number and other details of cross-validation.
You can perform simple K-fold cross-validation, or you can enter the cross-validation class in sklearn.
stack_method Parameters unique to classifiers represent the specific test results output by individual learners.
passthrough When training the meta-learner, whether to add the original data as the feature matrix.
n_jobs, verbose Number of threads and monitoring parameters.

In sklearn, just enter estimators and final_estimator to perform stacking. We can continue to use the combination of individual learners used in the voting method and use random forests as meta-learners to complete stacking:

  • Tool Library & Data
#常用工具库
import re
import numpy as np
import pandas as pd
import matplotlib as mlp
import matplotlib.pyplot as plt
import time

#算法辅助 & 数据
import sklearn
from sklearn.model_selection import KFold, cross_validate
from sklearn.datasets import load_digits #分类 - 手写数字数据集
from sklearn.datasets import load_iris
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split

#算法(单一学习器)
from sklearn.neighbors import KNeighborsClassifier as KNNC
from sklearn.neighbors import KNeighborsRegressor as KNNR
from sklearn.tree import DecisionTreeRegressor as DTR
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.linear_model import LinearRegression as LR
from sklearn.linear_model import LogisticRegression as LogiR
from sklearn.ensemble import RandomForestRegressor as RFR
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.ensemble import GradientBoostingRegressor as GBR
from sklearn.ensemble import GradientBoostingClassifier as GBC
from sklearn.naive_bayes import GaussianNB
import xgboost as xgb

#融合模型
from sklearn.ensemble import StackingClassifier
data = load_digits()
X = data.data
y = data.target

X.shape

np.unique(y) #10分类

Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,y,test_size=0.2,random_state=1412)

image

  • Define cross-validation function
def fusion_estimators(clf):
    """
    对融合模型做交叉验证,对融合模型的表现进行评估
    """
    cv = KFold(n_splits=5,shuffle=True,random_state=1412)
    results = cross_validate(clf,Xtrain,Ytrain
                             ,cv = cv
                             ,scoring = "accuracy"
                             ,n_jobs = -1
                             ,return_train_score = True
                             ,verbose=False)
    test = clf.fit(Xtrain,Ytrain).score(Xtest,Ytest)
    print("train_score:{}".format(results["train_score"].mean())
          ,"
 cv_mean:{}".format(results["test_score"].mean())
          ,"
 test_score:{}".format(test)
         )
def individual_estimators(estimators):
    """
    对模型融合中每个评估器做交叉验证,对单一评估器的表现进行评估
    """
    for estimator in estimators:
        cv = KFold(n_splits=5,shuffle=True,random_state=1412)
        results = cross_validate(estimator[1],Xtrain,Ytrain
                             ,cv = cv
                             ,scoring = "accuracy"
                             ,n_jobs = -1
                             ,return_train_score = True
                             ,verbose=False)
        test = estimator[1].fit(Xtrain,Ytrain).score(Xtest,Ytest)
        print(estimator[0]
          ,"
 train_score:{}".format(results["train_score"].mean())
          ,"
 cv_mean:{}".format(results["test_score"].mean())
          ,"
 test_score:{}".format(test)
          ,"
")
  • Definition of individual learners and meta-learners

When we explained the voting method before, we have given a detailed explanation on how to define individual learners, and have also made a lot of efforts to find the following 7 models. Here, we will use the 7 models selected from Voting:

#逻辑回归没有增加多样性的选项
clf1 = LogiR(max_iter = 3000, C=0.1, random_state=1412,n_jobs=8)
#增加特征多样性与样本多样性
clf2 = RFC(n_estimators= 100,max_features="sqrt",max_samples=0.9, random_state=1412,n_jobs=8)
#特征多样性,稍微上调特征数量
clf3 = GBC(n_estimators= 100,max_features=16,random_state=1412) 

#增加算法多样性,新增决策树与KNN
clf4 = DTC(max_depth=8,random_state=1412)
clf5 = KNNC(n_neighbors=10,n_jobs=8)
clf6 = GaussianNB()

#新增随机多样性,相同的算法更换随机数种子
clf7 = RFC(n_estimators= 100,max_features="sqrt",max_samples=0.9, random_state=4869,n_jobs=8)
clf8 = GBC(n_estimators= 100,max_features=16,random_state=4869)

estimators = [("Logistic Regression",clf1), ("RandomForest", clf2)
              , ("GBDT",clf3), ("Decision Tree", clf4), ("KNN",clf5) 
              #, ("Bayes",clf6)
              , ("RandomForest2", clf7), ("GBDT2", clf8)
             ]
  • Import sklearn for modeling
#选择单个评估器中分数最高的随机森林作为元学习器
#也可以尝试其他更简单的学习器
final_estimator = RFC(n_estimators=100
                      , min_impurity_decrease=0.0025
                      , random_state= 420, n_jobs=8)
clf = StackingClassifier(estimators=estimators #level0的7个体学习器
                         ,final_estimator=final_estimator #level 1的元学习器
                         ,n_jobs=8)

Before adjusting the fitting: that is, without adding min_impurity_decrease=0.0025

fusion_estimators(clf) #没有过拟合限制
""
train_score:1.0 
 cv_mean:0.9812112853271389 
 test_score:0.9861111111111112
""

After adding overfitting

fusion_estimators(clf) #精调过拟合
""
train_score:1.0 
 cv_mean:0.9812185443283005 
 test_score:0.9888888888888889
""
benchmark voting laws stacking method
5 fold cross validation 0.9666 0.9833 0.9812(↓)
Test set results 0.9527 0.9889 0.9889(-)

It can be seen that the score of stacking on the test set is the same as the voting method, but the 5-fold cross-validation score is not as high as the voting method. This may be because the data we train now is relatively simple, but when the data learning is more difficult, the advantages of stacking will slowly emerge. Of course, the meta-learner we are using now has almost default parameters. We can use Bayesian optimization to fine-tune the parameters of the meta-learner, and then compare them. The effect of the stacking method may surpass the voting method.

Feature matrix of 3-element learner

3.1 Two issues with meta-learner feature matrices

In the stacking process, individual learners train and predict on the original data, and then arrange the prediction results into a new feature matrix and put them into the meta-learner for learning. Among them, the prediction results of individual learners, that is, the matrix that needs to be trained by the meta-learner, are generally arranged as follows:

Learner 1 Learner 2 ... learnern
Sample 1 xxx xxx ... xxx
Sample 2 xxx xxx ... xxx
Sample 3 xxx xxx ... xxx
... ... ... ... ...
Sample m xxx xxx ... xxx

Based on our understanding of machine learning and model fusion, it is not difficult to find the following two problems:

  • First, there must be very few features in the feature matrix of the meta-learner

An individual learner can only output one set of prediction results. We arrange these prediction results, and the number of features in the new feature matrix is ​​equal to the number of individual learners. Generally, there are at most 20-30 individual learners in the fusion model, which means that there are at most 20-30 features in the feature matrix of the meta-learner. This feature quantity is far from enough for machine learning algorithms in industry and competitions.

  • Secondly, there are not too many samples in the feature matrix of the meta-learner.

The responsibility of the individual learner is to find the hypothesis between the original data and the label. In order to verify whether this hypothesis is accurate, what we need to look at is the generalization ability of the individual learner. Only when the generalization ability of the individual learner is strong, can we safely put the prediction results output by the individual learner into the meta-learner for fusion.


However. When we train the stacking model, we must divide the original data set into three parts: training set, verification set and test set -


Among them, test set< /span> It is no wonder that according to industry practice, the meta-learner needs to be a less complex algorithm, because the feature matrix of the meta-learner is far smaller than that of industrial machine learning in terms of feature volume and sample size. required standards. In order to solve these two problems, there are multiple solutions in the Stacking method, and these solutions can be implemented through the stacking class in sklearn. . Generally, the verification set only accounts for 30%-40% of the entire data set at most, which means that the sample size in the feature matrix used by the meta-learner is at most 40% of the original data. verification set Therefore, in the end, there is only a very small is used to train the individual learner and is content that has been fully disclosed to the individual learner. If If prediction is made on the training set, the prediction result will be "on the high side" and cannot represent the generalization ability of the individual learner. training set


And the is used to test the effect of the entire fusion model, so it cannot be used during the training process.



class sklearn.ensemble.StackingClassifier(estimators, final_estimator=None, *, cv=None, stack_method="auto", n_jobs=None, passthrough=False, verbose=0)

class sklearn.ensemble.StackingRegressor(estimators, final_estimator=None, *, cv=None, n_jobs=None, passthrough=False, verbose=0)

parameter illustrate
estimators List of individual evaluators. In sklearn, when only using a single evaluator as the individual evaluator, the
model can run, but the effect is often not very good.
final_estimator meta-learner, there can only be one evaluator. When the fusion model performs a classification task, the meta-learner must be a classification algorithm.
When the fusion model performs a regression task, the meta-learner must be a regression algorithm.
cv is used to specify the specific type, fold number and other details of cross-validation.
You can perform simple K-fold cross-validation, or you can enter the cross-validation class in sklearn.
stack_method Parameters unique to classifiers represent the specific test results output by individual learners.
passthrough When training the meta-learner, whether to add the original data as the feature matrix.
n_jobs, verbose Number of threads and monitoring parameters.

3.2 Solution to too small sample size: cross-validation

  • parameterscv, perform cross-validation in stacking
    In the original paper where the stacking method was proposed, the original author was naturally aware of the meta-learner There is a problem that the sample size of the feature matrix is ​​too small, so the idea of ​​using cross-validation inside the stacking process to expand the feature matrix of the meta-learner is proposed, that is, in Cross-validation is performed internally for each individual learner, but the results of this cross-validation are not used to verify the generalization ability. Instead, cross-validation is directly used as a tool for producing data.

Specifically, during the stacking process, we perform cross-validation like this -

For any individual learner, assuming we perform 5-fold cross-validation, we will divide the training data into 5 parts, and build a total of 5 models according to 4 parts for training and 1 part for verification, and train 5 times:
 

image


During the cross-validation process, the data in each validation set is not put into the model for training, so the prediction results on these validation sets can measure the generalization ability of the model.

Generally speaking, the final output of cross-validation is the scores on the 5 validation sets, but before calculating the scores, we must make predictions on the 5 validation sets and output the results. So we can build 5 models in cross-validation, and take turns to get the prediction results output by the 5 models, and these 5 prediction results just correspond to the 5 subsets divided in the full data set, a prediction result that corresponds to the sample in the original data can be obtained. This vertical stacking is just like stacking stones on the beach, which is where the name "stacking method" comes from. . This means that when we complete the cross-validation, we also complete the prediction for all the data in the original data. Now, as long as the prediction results of the 5 subsets are stacked vertically
 

image


Using this method to predict, you can letThe number of predicted values ​​output by any individual learner = sample size, in this way, meta-learning The number of rows of the feature matrix of the filter is equal to the sample size of the original data:

Learner 1 Learner 2 ... learnern
Sample 1 xxx xxx ... xxx
Sample 2 xxx xxx ... xxx
Sample 3 xxx xxx ... xxx
... ... ... ... ...
Sample m xxx xxx ... xxx

During the stacking process, this cross-validation process will definitely occur and is not within the scope of our human intervention. However, we can use parameters cv to decide what kind of cross-validation to use, including how many folds to use, whether to consider the distribution of classification labels, etc. Specifically, in the parameter cv, you can enter:

InputNone, 5-fold cross validation is used by default


Enter sklearnAny cross-validation object


InputAny integer, representing the fold number in Stratified K-fold validation. Stratified K-fold validation is a cross-validation that considers the proportion of each category in the label. If you choose Stratified K-fold cross-validation, the cross-validation will ensure the proportion of categories in the original label during each training. = Category proportion of training labels = Category proportion of validation labels.

 

image


Now you know how Stacking handles the problem of too few feature matrix samples for the meta-learner. It needs to be emphasized again that internal cross-validation is not verifying generalization ability, but a tool for producing data, so there is not much that can be adjusted in cross-validation itself. The only thing worth mentioning is that when the cross-validation fold is large, the antibody overfitting ability of the model will increase, and the learning ability will decrease slightly. When the cross-validation fold is small, the model is more likely to overfit. But if the amount of data is large enough, using too many cross-validation folds will not bring any benefits, but will only increase the training time:

estimators = [("Logistic Regression",clf1), ("RandomForest", clf2)
              , ("GBDT",clf3), ("Decision Tree", clf4), ("KNN",clf5) 
              #, ("Bayes",clf6)
              , ("RandomForest2", clf7), ("GBDT2", clf8)
             ]
final_estimator = RFC(n_estimators=100
                      , min_impurity_decrease=0.0025
                      , random_state= 420, n_jobs=8)
def cvtest(cv):
    clf = StackingClassifier(estimators=estimators
                         ,final_estimator=final_estimator
                         , cv = cv
                         , n_jobs=8)
    start = time.time()
    clf.fit(Xtrain,Ytrain)
    print((time.time() - start)) #消耗时间
    print(clf.score(Xtrain,Ytrain)) #训练集上的结果
    print(clf.score(Xtest,Ytest)) #测试集上的结果
cvtest(2) #非常少的验证次数
""
3.8339908123016357
1.0
0.9861111111111112
""

cvtest(10) #普通的验证次数
""
13.57864761352539
1.0
0.9833333333333333
""

cvtest(30) #很大的验证次数
""
39.74843621253967
1.0
0.9833333333333333
""

It can be seen that as the fold number in cv increases, the training time will definitely increase, but the performance of the model is not necessarily certain. Therefore, just choose 5 to 10-fold cross-validation. At the same time, since stacking comes with cross-validation and meta-learner algorithms, the stacking method runs much slower than the voting method and the mean method., this is where the stacking method is not very user-friendly.

3.3 Solutions with too few features

  • Parameterstack_method, change the result type output by the individual learner

对于分类stacking来说,如果特征量太少,我们可以更换个体学习器输出的结果类型。具体来说,如果个体学习器输出的是具体类别(如[0,1,2]),那1个个体学习器的确只能输出一列预测结果。但如果把输出的结果类型更换成概率值、置信度等内容,输出结果的结构一下就可以从一列拓展到多列。

如果这个行为由参数stack_method控制,这是只有StackingClassifier才拥有的参数,它控制个体分类器具体的输出。stack_method里面可以输入四种字符串:"auto", "predict_proba", "decision_function", "predict",除了"auto"之外其他三个都是sklearn常见的接口。

clf = LogiR(max_iter=3000, random_state=1412)

clf = clf.fit(Xtrain,Ytrain)
#predict_proba:输出概率值
clf.predict_proba(Xtrain)

image

#decision_function:每个样本点到分类超平面的距离,可以衡量置信度
#对于无法输出概率的算法,如SVM,我们通常使用decision_function来输出置信度
clf.decision_function(Xtrain)

image

#predict:输出具体的预测标签
clf.predict(Xtrain)

image


对参数stack_method有:

输入"auto",sklearn会在每个个体学习器上按照"predict_proba", "decision_function", "predict"的顺序,分别尝试学习器可以使用哪个接口进行输出。即,如果一个算法可以使用predict_proba接口,那就不再尝试后面两个接口,如果无法使用predict_proba,就尝试能否使用decision_function。


输入三大接口中的任意一个接口名,则默认全部个体学习器都按照这一接口进行输出。然而,如果遇见某个算法无法按照选定的接口进行输出,stacking就会报错。

因此,我们一般都默认让stack_method保持为"auto"。从上面的我们在逻辑回归上尝试的三个接口结果来看,很明显,当我们把输出的结果类型更换成概率值、置信度等内容,输出结果的结构一下就可以从一列拓展到多列。

  • predict_proba

对二分类,输出样本的真实标签1的概率,一列

对n分类,输出样本的真实标签为[0,1,2,3...n]的概率,一共n列

  • decision_function

对二分类,输出样本的真实标签为1的置信度,一列

对n分类,输出样本的真实标签为[0,1,2,3...n]的置信度,一共n列

  • predict

对任意分类形式,输出算法在样本上的预测标签,一列

在实践当中,我们会发现输出概率/置信度的效果比直接输出预测标签的效果好很多,既可以向元学习器提供更多的特征、还可以向元学习器提供个体学习器的置信度。我们在投票法中发现使用概率的“软投票”比使用标签类被的“硬投票”更有效,也是因为考虑了置信度。

  • 参数passthrough,将原始特征矩阵加入新特征矩阵

对于分类算法,我们可以使用stack_method,但是对于回归类算法,我们没有这么多可以选择的接口。回归类算法的输出永远就只有一列连续值,因而我们可以考虑将原始特征矩阵加入个体学习器的预测值,构成新特征矩阵。这样的话,元学习器所使用的特征也不会过于少了。当然,这个操作有较高的过拟合风险,因此当特征过于少、且stacking算法的效果的确不太好的时候,我们才会考虑这个方案。

控制是否将原始数据加入特征矩阵的参数是passthrough,我们可以在该参数中输入布尔值。当设置为False时,表示不将原始特征矩阵加入个体学习器的预测值,设置为True时,则将原始特征矩阵加入个体学习器的预测值、构成大特征矩阵。

  • 接口transform与属性stack_method_
estimators = [("Logistic Regression",clf1), ("RandomForest", clf2)
              , ("GBDT",clf3), ("Decision Tree", clf4), ("KNN",clf5) 
              #, ("Bayes",clf6)
              , ("RandomForest2", clf7), ("GBDT2", clf8)
             ]
final_estimator = RFC(n_estimators=100
                      , min_impurity_decrease=0.0025
                      , random_state= 420, n_jobs=8)
clf = StackingClassifier(estimators=estimators
                         ,final_estimator=final_estimator
                         ,stack_method = "auto"
                         ,n_jobs=8)
clf = clf.fit(Xtrain,Ytrain)

当我们训练完毕stacking算法后,可以使用接口transform来查看当前元学习器所使用的训练特征矩阵的结构

clf.transform(Xtrain).shape
# (1437, 70)

image


这个70 代表这个一共有7个个体学习器,每个个体学习器都有10个概率输出
如之前所说,这个特征矩阵的行数就等于训练的样本量:

Xtrain.shape[0]
# 1437

不过你能判断为什么这里有70列吗?因为我们有7个个体学习器,而现在数据是10分类的数据,因此每个个体学习器都输出了类别[0,1,2,3,4,5,6,7,8,9]所对应的概率,因此总共产出了70列数据:

pd.DataFrame(clf.transform(Xtrain)).head()

image


如果加入参数passthrough,特征矩阵的特征量会变得更大:

clf = StackingClassifier(estimators=estimators
                         ,final_estimator=final_estimator
                         ,stack_method = "auto"
                         ,passthrough = True
                         ,n_jobs=8)
clf = clf.fit(Xtrain,Ytrain)
clf.transform(Xtrain).shape #这里就等于70 + 原始特征矩阵的特征数量64
# (1437, 134)

image


使用属性stack_method_,我们可以查看现在每个个体学习器都使用了什么接口做为预测输出:

clf.stack_method_

["predict_proba",
"predict_proba",
"predict_proba",
"predict_proba",
"predict_proba",
"predict_proba",
"predict_proba"]

不难发现,7个个体学习器都使用了predict_proba的概率接口进行输出,这与我们选择的算法都是可以输出概率的算法有很大的关系

4 Stacking融合的训练/测试流程

现在我们已经知道了stacking算法中所有关于训练的信息,我们可以梳理出如下训练流程:

  • stacking的训练
  1. 将数据分割为训练集、测试集,其中训练集上的样本为(M_{train}),测试集上的样本量为(M_{test})
     
  2. 将训练集输入level 0的个体学习器,分别在每个个体学习器上进行交叉验证。在每个个体学习器上,将所有交叉验证的验证结果纵向堆叠形成预测结果。假设预测结果为概率值,当融合模型执行回归或二分类任务时,该预测结果的结构为((M_{train},1)),当融合模型执行K分类任务时(K>2),该预测结果的结构为((M_{train},K))
     
  3. 将所有个体学习器的预测结果横向拼接,形成新特征矩阵。假设共有N个个体学习器,则新特征矩阵的结构为((M_{train}, N))。如果是输出多分类的概率,那最终得出的新特征矩阵的结构为((M_{train}, N*K))
     
  4. 将新特征矩阵放入元学习器进行训练。

image

不难发现,虽然训练的流程看起来比较流畅,但是测试却不知道从何做起,因为:

  • 最终输出预测结果的是元学习器,因此直觉上来说测试数据集或许应该被输入到元学习器当中。然而,元学习器是使用新特征矩阵进行预测的,新特征矩阵的结构与规律都与原始数据不同,所以元学习器根本不可能接受从原始数据中分割出来的测试数据。因此正确的做法应该是让测试集输入level 0的个体学习器。

  • However, there is a problem: Level 0 individual learners do cross-validation during the training process, and cross-validation will only output the verification results, not Leave the trained model. Therefore, there is no trained model that can be used for prediction in level 0.

In order to resolve this contradiction in our training process, there are hidden steps:

  • stacking training
  1. Split the data into a training set and a test set, where the samples on the training set are (M_{train}) and the sample size on the test set is (M_{test})
     
  2. Enter the training set into the individual learners at level 0, and perform cross-validation on each individual learner. On each individual learner, the validation results of all cross-validations are stacked vertically to form the prediction results. Assume that the prediction result is a probability value. When the fusion model performs a regression or binary classification task, the structure of the prediction result is ((M_{train},1)). When the fusion model performs a K classification task (K>2), the structure of the prediction result is ((M_{train},1)). The structure of the prediction result is ((M_{train},K))
     
  3. Hidden step: Train all individual learners using all training data in preparation for testing.
     
  4. The prediction results of all individual learners are spliced ​​horizontally to form a new feature matrix. Assuming there are N individual learners in total, the structure of the new feature matrix is ​​((M_{train}, N)).
     
  5. Put the new feature matrix into the meta-learner for training.
  • stacking test
  1. Input the test set into the individual learners of level 0, and predict the corresponding results on each individual learner. Assume that the test results are probability values. When the fusion model performs regression or binary classification tasks, the structure of the test results is ((M_{test},1)). When the fusion model performs K classification tasks (K>2), the structure of the test results is ((M_{test},1)). The structure of the test results is ((M_{test},K))
     
  2. The prediction results of all individual learners are horizontally spliced ​​into a new feature matrix. Assuming there are N individual learners in total, the structure of the new feature matrix is ​​((M_{test}, N)).
     
  3. Put the new feature matrix into the meta-learner for prediction.

Therefore, in stacking, not only must all cross-validation be completed for individual learners, but also the training data must be reused to train all models after the cross-validation is completed. No wonder Stacking fusion is more complex and runs slowly.

So far, we have explained the voting method and the stacking method. In sklearn, we explain the following 4 categories:

Fusion method kind
voting laws ensemble.VotingClassifier
averaging method ensemble.VotingRegressor
Stacking method classification ensemble.StackingClassifier
stacked regression ensemble.StackingRegressor

Although these classes are model fusion methods, we can use these methods as we would any single algorithm class - we can easily perform manual parameter tuning, cross-validation, grid search, Bayesian optimization, pipeline packaging and other operations without worrying about code compatibility issues. However, it should be noted that the fusion tool in sklearn only supports the evaluator in sklearn, and does not support the native code of xgb and lgbm. Therefore, if we want to fuse models under native code, we must write the fusion process by ourselves.

2. Improved stacking method: Blending

1 The basic idea and process of Blending

Blending fusion is an improved algorithm based on Stacking fusion. In the previous course, we mentioned that the stacking method uses an algorithm at level 1, which can make the fusion itself move in the direction of minimizing the loss function. At the same time, stacking uses its own internal cross-validation to generate data, and the training data can be used in depth. , so that the overall effect of the model is better. But behind these operations, there are two huge problems:

  • Stacking fusion requires a huge amount of calculations, requiring high time and computing power costs, and

  • Stacking fusion is too complex in both data and algorithm, so the possibility of overfitting of the fusion model is too high.

In response to these two problems in stacking, the competition champion teams continued to explore and created a variety of improved stacking methods during practice. Today, one of the more effective methods among various stacking methods is the famous Blending method. Blending is literally translated as "mixing", but its core idea is actually exactly the same as Stacking: using two-layer algorithms in series, there are multiple strong learners on level0, there is only one meta-learner on level1, and there is a strong learner on level0 Responsible for fitting the relationship between data and real labels, outputting prediction results, forming a new feature matrix, and then letting the meta-learner on level 1 learn and predict on the new feature matrix.

image

However, unlike stacking, in order to reduce the amount of calculation and reduce the risk of overfitting of the fusion model, Blending cancels K-fold cross-validation and greatly reduces the amount of data required to train the meta-learner. The specific process is as follows:

  • blending training
  1. Split the data into a training set, a validation set and a test set, where the samples on the training set are (M_{train}), the samples on the validation set are (M_v), and the sample size on the test set is (M_{test})
     
  2. Enter the training set into the individual learners at level 0, and train on each individual learner separately. After training is completed, it is verified on the verification set and the prediction results on the verification set are output. Assume that the prediction result is a probability value. When the fusion model performs a regression or binary classification task, the structure of the prediction result is ((M_v,1)). When the fusion model performs a K classification task (K>2), the structure of the prediction result is The structure is ((M_v,K)). At this moment, all individual learners have been trained.
     
  3. The verification results of all individual learners are spliced ​​horizontally to form a new feature matrix. Assuming there are N individual learners in total, the structure of the new feature matrix is ​​((M_v, N)).
     
  4. Put the new feature matrix into the meta-learner for training.
  • blending test
  1. Input the test set into the individual learners of level 0, and predict the corresponding results on each individual learner. Assume that the test results are probability values. When the fusion model performs regression or binary classification tasks, the structure of the test results is ((M_{test},1)). When the fusion model performs K classification tasks (K>2), the structure of the test results is ((M_{test},1)). The structure of the test results is ((M_{test},K))
     
  2. The prediction results of all individual learners are horizontally spliced ​​into a new feature matrix. Assuming there are N individual learners in total, the structure of the new feature matrix is ​​((M_{test}, N)).
     
  3. Put the new feature matrix into the meta-learner for pre-testing
    .

2 Manually implement the Blending algorithm

def BlendingClassifier(X,y,estimators,final_estimator,test_size=0.2,vali_size=0.4):
    """
    该函数用于实现Blending分类融合
    X,y:整体数据集,会被分割为训练集、测试集、验证集三部分
    estimators: level0的个体学习器,输入格式形如sklearn中要求的[(名字,算法),(名字,算法)...]    
    final_estimator:元学习器
    test_size:测试集占全数据集的比例
    vali_size:验证集站全数据集的比例
    
    """
    
    #第一步,分割数据集
    #1.分测试集
    #2.分训练和验证集,验证集占完整数据集的比例为0.4,因此占排除测试集之后的比例为0.4/(1-0.2)
    X_,Xtest,y_,Ytest = train_test_split(X,y,test_size=test_size,random_state=1412)
    Xtrain,Xvali,Ytrain,Yvali = train_test_split(X_,y_,test_size=vali_size/(1-test_size),random_state=1412)
    
    #训练
    #建立空dataframe用于保存个体学习器上的验证结果,即用于生成新特征矩阵
    #新建空列表用于保存训练完毕的个体学习器,以便在测试中使用
    NewX_vali = pd.DataFrame()
    trained_estimators = []
    #循环、训练每个个体学习器、并收集个体学习器在验证集上输出的概率
    for clf_id, clf in estimators:
        clf = clf.fit(Xtrain,Ytrain)
        val_predictions = pd.DataFrame(clf.predict_proba(Xvali))
        #保存结果,在循环中逐渐构筑新特征矩阵
        NewX_vali = pd.concat([NewX_vali,val_predictions],axis=1)
        trained_estimators.append((clf_id,clf))
    #元学习器在新特征矩阵上训练、并输出训练分数
    final_estimator = final_estimator.fit(NewX_vali,Yvali)
    train_score = final_estimator.score(NewX_vali,Yvali)
    
    #测试
    #建立空dataframe用于保存个体学习器上的预测结果,即用于生成新特征矩阵
    NewX_test = pd.DataFrame()
    #循环,在每个训练完毕的个体学习器上进行预测,并收集每个个体学习器上输出的概率
    for clf_id,clf in trained_estimators:
        test_prediction = pd.DataFrame(clf.predict_proba(Xtest))
        #保存结果,在循环中逐渐构筑特征矩阵
        NewX_test = pd.concat([NewX_test,test_prediction],axis=1)
    #元学习器在新特征矩阵上测试、并输出测试分数
    test_score = final_estimator.score(NewX_test,Ytest)
    
    #打印训练分数与测试分数
    print(train_score,test_score)
#逻辑回归没有增加多样性的选项
clf1 = LogiR(max_iter = 3000, C=0.1, random_state=1412,n_jobs=8)
#增加特征多样性与样本多样性
clf2 = RFC(n_estimators= 100,max_features="sqrt",max_samples=0.9, random_state=1412,n_jobs=8)
#特征多样性,稍微上调特征数量
clf3 = GBC(n_estimators= 100,max_features=16,random_state=1412) 

#增加算法多样性,新增决策树与KNN
clf4 = DTC(max_depth=8,random_state=1412)
clf5 = KNNC(n_neighbors=10,n_jobs=8)
clf6 = GaussianNB()

#新增随机多样性,相同的算法更换随机数种子
clf7 = RFC(n_estimators= 100,max_features="sqrt",max_samples=0.9, random_state=4869,n_jobs=8)
clf8 = GBC(n_estimators= 100,max_features=16,random_state=4869)

estimators = [("Logistic Regression",clf1), ("RandomForest", clf2)
              , ("GBDT",clf3), ("Decision Tree", clf4), ("KNN",clf5) 
              #, ("Bayes",clf6)
              #, ("RandomForest2", clf7), ("GBDT2", clf8)
             ]
final_estimator = RFC(n_estimators= 100
                      #, max_depth = 8
                      , min_impurity_decrease=0.0025
                      , random_state= 420, n_jobs=8)
#很明显,过拟合程度比Stacking要轻,但是测试集的表现没有stacking强
BlendingClassifier(X,y,estimators,final_estimator)

image

#验证比例越大,模型学习能力越弱 - 注意验证集比例上限0.8,因为有0.2是测试数据
BlendingClassifier(X,y,estimators,final_estimator,vali_size=0.7)

image

#blending的运行速度比stacking快了不止一个档次……
BlendingClassifier(X,y,estimators,final_estimator,vali_size=0.1)
""
1.0 0.9833333333333333
""

image

benchmark voting laws Stacking Blending
5 fold cross validation 0.9666 0.9833 0.9812(↓) -
Test set results 0.9527 0.9889 0.9889(-) 0.9833(↓)

Judging from the results, the voting method has the most stable and excellent performance. This is related to the fact that the data set we selected is a relatively simple data set. At the same time, the voting method is also the algorithm we have adjusted the most and is most in place. Stacking and blending will show more advantages when running on large data sets. At this point, we have finished explaining blending. In the official course of "2022 Machine Learning Practical Combat", we will explain in more detail the application of blending on complex algorithms such as xgboost.

Original address: https://www.cnblogs.com/lipu123/p/17563377.html

For more knowledge about python machine learning, please follow the official account (python risk control model)

Guess you like

Origin blog.csdn.net/toby001111/article/details/133305657