在 AdaBoostClassifier 中使用 scikit-learn 的 MLPClassifier

Using scikit-learn's MLPClassifier in AdaBoostClassifier

对于二元分类问题,我想使用 MLPClassifier 作为 AdaBoostClassifier 中的基本估计器。但是,这不起作用,因为 MLPClassifier 没有实现 AdaBoostClassifier 所必需的 sample_weight(请参阅 here). Before that, I tried using a Keras model and the KerasClassifier within AdaBoostClassifier but that did also not work as mentioned here

Away,用户V1nc3nt提出的,是在TensorFlow中建立自己的MLPclassifier,兼顾sample_weight.

用户 V1nc3nt 分享了他的大部分代码,但由于我对 Tensorflow 的经验有限,因此无法填写缺失的部分。因此,我想知道是否有人找到了从 MLP 构建 Adaboost 集成的可行解决方案,或者可以帮助我完成 V1nc3nt 提出的解决方案。

非常感谢您!

根据您提到的参考文献,我修改了 MLPClassifier 以适应 sample_weights

试试这个!

from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.ensemble import AdaBoostClassifier

class customMLPClassifer(MLPClassifier):
    def resample_with_replacement(self, X_train, y_train, sample_weight):

        # normalize sample_weights if not already
        sample_weight = sample_weight / sample_weight.sum(dtype=np.float64)

        X_train_resampled = np.zeros((len(X_train), len(X_train[0])), dtype=np.float32)
        y_train_resampled = np.zeros((len(y_train)), dtype=np.int)
        for i in range(len(X_train)):
            # draw a number from 0 to len(X_train)-1
            draw = np.random.choice(np.arange(len(X_train)), p=sample_weight)

            # place the X and y at the drawn number into the resampled X and y
            X_train_resampled[i] = X_train[draw]
            y_train_resampled[i] = y_train[draw]

        return X_train_resampled, y_train_resampled


    def fit(self, X, y, sample_weight=None):
        if sample_weight is not None:
            X, y = self.resample_with_replacement(X, y, sample_weight)

        return self._fit(X, y, incremental=(self.warm_start and
                                            hasattr(self, "classes_")))


X,y = load_iris(return_X_y=True)
adabooster = AdaBoostClassifier(base_estimator=customMLPClassifer())

adabooster.fit(X,y)