VotingClassifier with pipelines as estimators

主宰稳场 提交于 2020-08-20 04:01:09

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/59897096/votingclassifier-with-pipelines-as-estimators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!