对于二进制分类问题,我想使用MLPClassifier
作为AdaBoostClassifier
中的基本估计量。但是,这不起作用,因为MLPClassifier
未实现sample_weight
,这是AdaBoostClassifier所必需的(请参见here)。在此之前,我尝试使用Keras模型和KerasClassifier
中的AdaBoostClassifier
,但是也没有像提到的here那样工作。
用户V1nc3nt提出的way是在TensorFlow中构建自己的MLPclassifier
并考虑sample_weight。
用户V1nc3nt共享了他的大部分代码,但是由于我对Tensorflow的经验有限,所以我无法填写缺少的部分。因此,我想知道是否有人找到了用于从MLP构建Adaboost集成的可行解决方案,或者可以帮助我完成V1nc3nt提出的解决方案。
非常感谢您!
答案 0 :(得分:0)
根据您提供的参考,我修改了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)