GridSearchCV: object has no attribute 'classes_'

110 views Asked by At

I am using 'GridSearchCV' for hyper-parameter optimization and use 'roc_auc' for scoring.

from sklearn.metrics import roc_auc_score, make_scorer

scoring = make_scorer(roc_auc_score, needs_proba=True)
clf = GridSearchCV(pipe, search_space,cv=cv_inner, scoring= scoring,refit=True, error_score="raise") #scoring=f1_weighted'
clf.fit(Xtrain, ytrain)
                
best_model = clf.best_estimator_
yhat = best_model.predict_proba(X_valid)

But, I'm getting the following error: AttributeError: 'Model_selection' object has no attribute 'classes_'

The 'Model_selection' class is as follows:

class Model_selection(BaseEstimator):
    def __init__(self, estimator = RandomForestClassifier()):
        self.estimator = estimator
    def fit(self, X, y=None, **kwargs):
        self.estimator.fit(X, y)
        return self
    def predict(self, X, y=None):
        return self.estimator.predict(X)
    def predict_proba(self, X):
        return self.estimator.predict_proba(X)
    def score(self, X, y):
        return self.estimator.score(X, y)

What am I doing wrong here?

1

There are 1 answers

0
Sean Fitzgerald On

I believe I had a similar problem making a wrapper for MLPClassifier to work wtih skopt. In my wrapper I had to strictly follow this template: https://scikit-learn.org/stable/developers/develop.html

def fit(self, X, y):
    # Check that X and y have correct shape
    X, y = check_X_y(X, y)
    # Store the classes seen during fit
    self.classes_ = unique_labels(y)

.... Define model here

    model.fit(X, y)
    self.model = model
    return self

I was also missing the self.classes_ = unique_labels(y) Remember to ammend your imports accordingly.