joel
joel

Reputation: 1186

TypeError: 'Pipeline' object is not callable in custom classifier

I am working on a classification problem. I have created a class for my classifier. I am having an issue with this class. I have a get_clf_pipeline method which returns the classifier pipeline, which is used by the train method. This method doesn`t require access to an instance or class data and I tried making it a staticmethod but that approach didn`t work as well. How can I fix this issue?

class Event_classifier:
    
    def __init__(self):
        self.clf_pipeline = self.get_clf_pipeline()
    
    def get_clf_pipeline(self):
        """ Return the pipeline
        """
        categorical_features = ['CRole', 'Clevel', 'Gender']

        categorical_transformer = Pipeline(steps=[
            ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
            ('onehot', OneHotEncoder(handle_unknown='ignore'))])

        preprocessor = ColumnTransformer(
            transformers=[        
                ('cat', categorical_transformer, categorical_features)])

        estimators = [
            ('rf',RandomForestClassifier(n_estimators=200,class_weight='balanced')),
            ('mnb', MultinomialNB()),
            ('svr', make_pipeline(StandardScaler(with_mean=False),
                                  LinearSVC(random_state=42)))
        ]

        stacked_clf = StackingClassifier(
             estimators=estimators, final_estimator=LogisticRegression(class_weight='balanced')
        )
        clf_pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                              ('classifier', stacked_clf)])
        return clf_pipeline

    def train(self, X,y):
        """Trains the classifier on input data
        """
        print(type(self.clf_pipeline))
        self.clf_pipeline(X,y)
    
    def predict(self):
        """Predict on test data
        """
        if (X.shape) == 1:
            X = X.T
        y_pred = self.clf_pipeline.predict(X)
        return y_pred        
    

Creating a class instance and training on data

ec = Event_classifier()

ec.train(X_train, y_train)
print('Model training complete')

I get the following error

<class 'sklearn.pipeline.Pipeline'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-129-36d8dfcdfc12> in <module>
      1 ec = Event_classifier()
      2 
----> 3 ec.train(X_train, y_train)
      4 print('Model training complete')
      5 

<ipython-input-126-de5f651c0a3d> in train(self, X, y)
     35         """
     36         print(type(self.clf_pipeline))
---> 37         self.clf_pipeline(X,y)
     38 
     39     def predict(self):

TypeError: 'Pipeline' object is not callable

Upvotes: 0

Views: 6368

Answers (1)

Ben Reiniger
Ben Reiniger

Reputation: 12582

Your self.clf_pipeline is a Pipeline object, so self.clf_pipeline(X,y) is trying to call the pipeline on the inputs X, y, but (as the error says) Pipelines aren't functions. Presumably you want something like self.clf_pipeline.fit(X, y) instead.

One more thing jumps out: when is X.shape == 1 (in your predict method)?

Upvotes: 1

Related Questions