BaggingClassifier中KNeighborsClassifier的使用方法&如何解决"KNN doesn't support sample weights issue"

How to use KNeighborsClassifier in BaggingClassifier & How to solve "KNN doesn't support sample weights issue"

我是 Sklearn 的新手,我正在尝试将 KNN、决策树、SVM 和高斯 NB 结合起来用于 BaggingClassifier。

我的部分代码如下所示:

best_KNN = KNeighborsClassifier(n_neighbors=5, p=1)
best_KNN.fit(X_train, y_train)

majority_voting = VotingClassifier(estimators=[('KNN', best_KNN), ('DT', best_DT), ('SVM', best_SVM), ('gaussian', gaussian_NB)], voting='hard')
majority_voting.fit(X_train, y_train)

bagging = BaggingClassifier(base_estimator=majority_voting)
bagging.fit(X_train, y_train)

但这会导致错误提示:

TypeError: Underlying estimator KNeighborsClassifier does not support sample weights.

如果我删除 KNN,"bagging" 部分工作正常。 有没有人有解决这个问题的想法?谢谢你的时间。

BaggingClassifier 中,您只能使用支持样本权重的基本估计器,因为它依赖于 score 方法,该方法采用 sample_weightparam.

您可以列出所有可用的分类器,例如:

import inspect 
from sklearn.utils.testing import all_estimators 
for name, clf in all_estimators(type_filter='classifier'): 
    if 'sample_weight' in inspect.getargspec(clf.fit)[0]: 
        print(name)