Skip to main content
 首页 » 编程设计

python之学习 : How to make an ensemble for two binary classifiers

2025年05月04日9三少

我有两个分类器用于多媒体数据集。一种用于视觉 Material ,一种用于文本 Material 。我想结合这些分类器的预测来做出最终的预测。我一直在阅读有关 bagging、boosting 和 stacking ensembles 的文章,它们似乎都很有用,我想尝试一下。但是,我似乎只能为我的特定问题找到相当理论的例子,没有什么具体的足以让我理解如何实际实现它(在带有 scikit-learn 的 python 中)。我的两个分类器都使用 10 KFold CV 和 SVM 分类。两者都输出带有预测(1 或 0)的 n_samples = 1000 列表。另外,我让它们都生成了一个概率列表,这些概率是预测所基于的,看起来像这样:

 [[ 0.96761819  0.03238181] 
 [ 0.96761819  0.03238181] 
  .... 
 [ 0.96761819  0.03238181] 
 [ 0.96761819  0.03238181]] 

我将如何将它们组合成一个整体。我应该使用什么作为输入?我尝试水平连接标签预测并将它们作为特征输入,但没有成功(概率相同)。

请您参考如下方法:

如果您正在寻找严格的组合,我建议使用 brew因为它建立在 sklearn 之上(意味着您可以使用您的 sklearn 分类器),而且,上次我检查时,sklearn 非常适合创建集成(Bagging、AdaBoost、RandomForest ...),但提供的组合规则不多用于您自己的自定义合奏(例如混合合奏)。

https://github.com/viisar/brew

from brew.base import Ensemble 
from brew.base import EnsembleClassifier 
from brew.combination.combiner import Combiner 
 
# create your Ensemble 
clfs = your_list_of_classifiers # [clf1, clf2] 
ens = Ensemble(classifiers = clfs) 
 
# create your Combiner 
# the rules can be 'majority_vote', 'max', 'min', 'mean' or 'median' 
comb = Combiner(rule='mean') 
 
# now create your ensemble classifier 
ensemble_clf = EnsembleClassifier(ensemble=ens, combiner=comb) 
ensemble_clf.predict(X)