【问题标题】:How to build voting classifier in sklearn when the individual classifiers are being fit with different datasets?当单个分类器适合不同的数据集时,如何在 sklearn 中构建投票分类器?
【发布时间】:2019-06-29 08:18:55
【问题描述】:

我正在使用高度不平衡的数据构建分类器。我在测试中感兴趣的策略是使用 3 个不同的重采样数据集 集成模型。换句话说,每个数据集将有所有个来自稀有类的样本,但只有个丰富类的样本 (technique #4 mentioned in this article)。

我想在每个重新采样的数据集上拟合 3 个不同的 VotingClassifiers,然后使用 另一个 VotingClassifier(或类似的)组合各个模型的结果。我知道构建一个单一的投票分类器是这样的:

# First Model
rnd_clf_1 = RandomForestClassifier()
xgb_clf_1 = XGBClassifier()

voting_clf_1 = VotingClassifier(
    estimators = [
        ('rf', rnd_clf_1), 
        ('xgb', xgb_clf_1),
    ],
    voting='soft'
)

# And I can fit it with the first dataset this way:
voting_clf_1.fit(X_train_1, y_train_1)

但是,如果它们适合不同的数据集,如何堆叠它们三个?例如,如果我有三个拟合模型(参见下面的代码),我可以构建一个函数,在每个模型上调用 .predict_proba() 方法,然后“手动”平均各个概率。

但是...有更好的方法吗?

# Fitting the individual models... but how to combine the predictions?
voting_clf_1.fit(X_train_1, y_train_1)
voting_clf_2.fit(X_train_2, y_train_2)
voting_clf_3.fit(X_train_3, y_train_3)

谢谢!

【问题讨论】:

  • 如果这是一个二分类问题,那么采用一种预测模式就可以了。如果类别更多,我们将进行概率最高的预测,但我只是在想它们的概率是否有可比性吗?
  • 嘿@AdityaKansal,感谢您的评论。实际上,我正在寻找的是一种使用 sklearn 本身结合三个投票分类器的方法。我可以制作自己的功能来执行您的建议,但是在 sklearn 中是否已经有一种方法可以做到这一点,而无需重新发明轮子?我遇到的困难是模型配备了不同的数据集,所以我不确定正常的管道能否完成工作......

标签: machine-learning scikit-learn ensemble-learning


【解决方案1】:

通常文章中显示的#4 方法是使用相同类型的分类器实现的。您似乎想在每个示例数据集上尝试 VotingClassifier

imblearn.ensemble.BalancedBaggingClassifier 中已经有这种方法的实现,它是 Sklearn Bagging 方法的扩展。

您可以将估算器输入为VotingClassifier,将估算器的数量输入为您希望执行数据集采样的次数。使用sampling_strategy 参数来提及您想要在多数类上的下采样比例。

工作示例:

from collections import Counter
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
import xgboost as xgb
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from imblearn.ensemble import BalancedBaggingClassifier # doctest: +NORMALIZE_WHITESPACE
X, y = make_classification(n_classes=2, class_sep=2,
weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0,
n_features=20, n_clusters_per_class=1, n_samples=1000, random_state=10)
print('Original dataset shape %s' % Counter(y))

X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    random_state=0)

rnd_clf_1 = RandomForestClassifier()
xgb_clf_1 = xgb.XGBClassifier()

voting_clf_1 = VotingClassifier(
    estimators = [
        ('rf', rnd_clf_1), 
        ('xgb', xgb_clf_1),
    ],
    voting='soft'
)

bbc = BalancedBaggingClassifier(base_estimator=voting_clf_1, random_state=42)
bbc.fit(X_train, y_train) # doctest: +ELLIPSIS

y_pred = bbc.predict(X_test)
print(confusion_matrix(y_test, y_pred))

here。在手动拟合估算器后,您可能可以重用 _predict_proba()_collect_probas() 函数。

【讨论】:

    猜你喜欢
    • 2018-03-28
    • 2011-12-27
    • 2018-07-09
    • 1970-01-01
    • 2021-12-26
    • 2020-04-09
    • 2021-05-27
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多