Stacking (sometimes called stacked generalization) involves training a learning algorithm to combine the predictions of several other learning algorithms. First, all of the other algorithms are trained using the available data, then a combiner algorithm is trained to make a final prediction using all the predictions of the other algorithms as additional inputs. --wiki
Stacking是一种集成的机器学习算法,可以学习如何最好地组合来自多个机器学习模型的预测。和之前讲的bagging不同的是,在stacking中包含不同的模型,而不是像bagging中那样所有的predictor都是决策树,stacking中不同的模型使用的是相同的数据集,而在bagging中使用的是训练数据集的子样本;stacking相较于boosting而言,在stacking中,一个模型是用来学习如何最好地结合不同模型的预测,而不是像在boosting中那样,通过修正之前模型的预测结果来得到最终结果。
Stacking适合于不同模型做出的预测或模型预测中的误差不相关或具有较低的相关性。堆叠模型的结构涉及两个或多个基本模型(通常称为0级模型)和一个将基本模型预测结合在一起的元模型(称为1级模型)。
Level-0 模型(基础-模型):使用训练数据拟合模型;
Level-1 模型(元-模型):学习如何最佳地结合基础模型的预测的模型。
Level-1的模型是根据level-0模型对样本的数据所做的预测结果进行训练的。比如在下面的图中,level-0就是那个基础模型,通常是复杂多样的,它是指不同的用于解决预测模型任务的predictors,这些不同的predictors,可以是logistic regression, SVM Classifier, Random Forest Classifier 等等。Level-1图中的ensemble’s prediction这个过程,元模型通常比较简单,主要功能是对基础模型做出的预测进行解答。因此元模型通常使用线性模型,例如用于回归任务的线性回归(Linear Regression)(用来预测一个数值)和用于分类任务的逻辑回归(Logistic Regression)(预测类标签)。
常用的的ensemble’s prediction方法比如说:majority vote,blending,etc.。
堆叠用来提高模型的性能,但是也不能保证在所有的情况下有所改进。实现性能上的改进还取决于问题的复杂性,以及训练数据是否很好地表达问题和是否足够复杂,以至于通过组合预测来学习更多。它还取决于模型的选择及它们在预测方面是否足够熟练和低相关性。
如果基础模型的性能在各方面表现得与堆叠集成一样或者说更好,那么应该使用基础模型,而不是stacking。因为基础模型的复杂度比较低,它更易于训练和修改。
关于sklearn中的ensemble API可以参考下面的连接:
https://scikit-learn.org/stable/modules/classes.html#module-sklearn.ensemble
举例使用一下:Stacking API
StackingClassifier–LogisticRegression
import sklearn
print(sklearn.__version__)
from sklearn.datasets import make_classification # generate a random n-class classification problem
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import StackingClassifier
from matplotlib import pyplot
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
# KNeighborsClassifier: Classifier implementing the k-nearest neighbors vote,K 最邻近法
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from matplotlib import pyplot
def get_dataset():
X,y=make_classification(n_samples=1000,n_features=20,n_informative=15,n_redundant=5,random_state=1)
return X,y
def get_stacking():
level0=list()
level0.append(('lr',LogisticRegression()))
level0.append(('knn',KNeighborsClassifier()))
level0.append(('cart',DecisionTreeClassifier()))
level0.append(('svm',SVC()))
level0.append(('bayes',GaussianNB()))
level1=LogisticRegression()
model=StackingClassifier(estimators=level0,final_estimator=level1,cv=5)
return model
def get_models():
models=dict()
models['lr']=LogisticRegression()
models['knn']=KNeighborsClassifier()
models<