我正在尝试使用EnsembleVoteClassifier
库中的mlxtend
,我的分类器是ANN,SVM和Logistic回归。我正在预先拟合模型并致电EnsembleVoteClassifier
只是为了进行预测:
ensemble=EnsembleVoteClassifier(clfs=[model_nn, model_logreg],voting='hard',refit=False)
ensemble.fit(X_train,y_train)
y_pred_ensemble = ensemble.predict(X_test)
问题出在Keras。我的代码如下:
model_nn = Sequential()
model_nn.add(Dense(20, input_shape=(X_train.shape[1],),
kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=42),
bias_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=42)))
model_nn.add(Activation('relu'))
model_nn.add(BatchNormalization())
model_nn.add(Dropout(0.5))
model_nn.add(Dense(2, activation='softmax'))
model_nn.compile (loss = 'sparse_categorical_crossentropy', optimizer=k.optimizers.Adam(lr=1e-4))
early_stopping_monitor = EarlyStopping(monitor='val_loss', mode='min', patience=20)
lr_reduce= ReduceLROnPlateau(monitor='val_loss', verbose=1, mode='min', patience=20)
history = model_nn.fit(X_train, y_train, epochs=1000,
class_weight=class_weights,
batch_size=32,
validation_data=(X_val, y_val), verbose = 1,
callbacks=[early_stopping_monitor, lr_reduce])
y_pred_nn = model_nn.predict(X_test)
y_pred_nn = y_pred_nn.argmax(axis=1)
问题在于预测类的形状为(n_samples,2)
,这会在EnsembleVoteClassifier中产生错误:
raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (, 2)
是否有任何方法可以通过管道来解决形状问题,并输出与sklearn
相同形状的keras预测?
谢谢。