davidsbatista / machine-learning-notebooks

Assorted exercises and proof-of-concepts to understand and study machine learning and statistical learning theory
45 stars 26 forks source link

Why am i getting this Unbound Local Error using a package which is wrapper around gridsearchCV? (hyperclassifersearch) #5

Open zahs123 opened 2 years ago

zahs123 commented 2 years ago

I am pasting an issue here which is based on hyperclassifersearch package (which is based on this code). Any idea why i am getting the following?

One thing i do not understand is that when i use One Hot encoding ( when i switch to targetencoding i don't get the error) i get this error from running the below:


     86 
     87         print('Search is done.')
---> 88         return best_model # allows to predict with the best model overall
     89 
     90     def evaluate_model(self, sort_by='mean_test_score', show_timing_info=False):

UnboundLocalError: local variable 'best_model' referenced before assignment

The code i used to generate that error is as follows:

#define pipeline

    numeric_transformer = Pipeline(steps=[('imputer',SimpleImputer(missing_values=np.nan,strategy='constant', fill_value=0))]
    preprocessor = ColumnTransformer(transformers=[('num', numeric_transformer, numeric_cols),('cat', OneHotEncoder(handle_unknown='ignore'), cat_cols)])
    model = XGBClassifier(objective='binary:logistic',n_jobs=-1_label_encoder=False)
    pipeline = Pipeline(steps=[('preprocessor', preprocessor),('clf', model)])

models = { 

    'xgb' : pipeline } 

params = { 
    'xgb': { 'clf__n_estimators': [200,300]}
        }

    cv = StratifiedKFold(n_splits=3, random_state=42,shuffle=True)

    search = HyperclassifierSearch(models, params)
    gridsearch = search.train_model(X_train, y_train, cv=cv,scoring='recall')

I dont understand this error? Can anybody help https://github.com/janhenner/HyperclassifierSearch <-- repo to package.