Search code examples
pythonkerasscikit-learnpipelineensemble-learning

VotingClassifier with pipelines as estimators


I want to build an sklearn VotingClassifier ensemble out of multiple different models (Decision Tree, SVC, and a Keras Network). All of them need a different kind of data preprocessing, which is why I made a pipeline for each of them.

# Define pipelines

# DTC pipeline
featuriser = Featuriser()
dtc = DecisionTreeClassifier()
dtc_pipe = Pipeline([('featuriser',featuriser),('dtc',dtc)])

# SVC pipeline
scaler = TimeSeriesScalerMeanVariance(kind='constant')
flattener = Flattener()
svc = SVC(C = 100, gamma = 0.001, kernel='rbf')
svc_pipe = Pipeline([('scaler', scaler),('flattener', flattener), ('svc', svc)])

# Keras pipeline
cnn = KerasClassifier(build_fn=get_model())
cnn_pipe = Pipeline([('scaler',scaler),('cnn',cnn)])

# Make an ensemble
ensemble = VotingClassifier(estimators=[('dtc', dtc_pipe), 
                                        ('svc', svc_pipe),
                                        ('cnn', cnn_pipe)], 
                            voting='hard')

The Featuriser,TimeSeriesScalerMeanVariance and Flattener classes are some custom made transformers that all employ fit,transform and fit_transform methods.

When I try to ensemble.fit(X, y) fit the whole ensemble I get the error message:

ValueError: The estimator list should be a classifier.

Which I can understand, as the individual estimators are not specifically classifiers but pipelines. Is there a way to still make it work?


Solution

  • The problem is with the KerasClassifier. It does not provide the _estimator_type, which was checked in _validate_estimator.

    It is not the problem of using pipeline. Pipeline provides this information as a property. See here.

    Hence, the quick fix is setting _estimator_type='classifier'.

    A reproducible example:

    # Define pipelines
    from sklearn.pipeline import Pipeline
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.svm import SVC
    from sklearn.preprocessing import MinMaxScaler, Normalizer
    from sklearn.ensemble import VotingClassifier
    from keras.wrappers.scikit_learn import KerasClassifier
    from sklearn.datasets import make_classification
    from keras.layers import Dense
    from keras.models import Sequential
    
    X, y = make_classification()
    
    # DTC pipeline
    featuriser = MinMaxScaler()
    dtc = DecisionTreeClassifier()
    dtc_pipe = Pipeline([('featuriser', featuriser), ('dtc', dtc)])
    
    # SVC pipeline
    scaler = Normalizer()
    svc = SVC(C=100, gamma=0.001, kernel='rbf')
    svc_pipe = Pipeline(
        [('scaler', scaler), ('svc', svc)])
    
    # Keras pipeline
    def get_model():
        # create model
        model = Sequential()
        model.add(Dense(10, input_dim=20, activation='relu'))
        model.add(Dense(1, activation='sigmoid'))
        # Compile model
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model
    
    
    cnn = KerasClassifier(build_fn=get_model)
    cnn._estimator_type = "classifier"
    cnn_pipe = Pipeline([('scaler', scaler), ('cnn', cnn)])
    
    
    # Make an ensemble
    ensemble = VotingClassifier(estimators=[('dtc', dtc_pipe), 
                                            ('svc', svc_pipe),
                                            ('cnn', cnn_pipe)], 
                                voting='hard')
    
    ensemble.fit(X, y)