pythonscikit-learnimblearn

Pipeline including several steps: StandardScaler(), RandomUnderSampler, Classifiers


I have the following code and showing the error TypeError: Last step of Pipeline should implement fit or be the string 'passthrough'. '[('sc', StandardScaler()), ('rus', RandomUnderSampler()), ('clf', LogisticRegression(max_iter=10000, multi_class='ovr', solver='sag'))]' (type <class 'list'>) doesn't

my code as follow:

from sklearn.pipeline import Pipeline
from imblearn.pipeline import make_pipeline
from sklearn.pipeline import make_pipeline

also, i have imported all classifiers in my list

classifiers = [LogisticRegression(solver='sag',penalty='l2',multi_class='ovr',
                              max_iter=10000,random_state=None,fit_intercept=True),
           LinearDiscriminantAnalysis(shrinkage='auto'),LinearSVC(multi_class='ovr',penalty ='l2'),
           QuadraticDiscriminantAnalysis(),SGDClassifier(max_iter=10000),
           GaussianProcessClassifier(max_iter_predict =10000,multi_class='one_vs_rest'),
           RidgeClassifier(solver='sag',random_state=None,max_iter=10000),
           DecisionTreeClassifier(min_samples_leaf=1),BaggingClassifier(),RandomForestClassifier()]

for classifier in classifiers:
model = make_pipeline( [('sc',StandardScaler()),('rus',RandomUnderSampler()),
                               ('clf',classifier)])
model.fit(X_train,y_train)

I need help to see where have i done something wrong or maybe i am missing something out!


Solution

  • the solution was:

    for classifier in classifiers:
    model = Pipeline_imb( [('sc',StandardScaler()),('rus',RandomUnderSampler()),
                                   ('clf',classifier)])
    model.fit(X_train,y_train)
    

    i had to install: from imblearn.pipeline import make_pipeline as make_pipeline_imb from imblearn.pipeline import Pipeline as Pipeline_imb