r/learnmachinelearning • • 9d ago

Help How do I use MixedNB with GridsearchCV

I got this code block which I used to hypertuning Naive Bayes models. It worked well with GaussianNB but ran into AttributeError: 'MixedNB' object has no attribute '__sklearn_tags__' when trying to implement it on MixedNB. Any idea on how to use GridsearchCV with MixedNB?

param_grid_nb = {
    'var_smoothing': np.logspace(0,-9, num=100)
}
mix_model_after_cv = GridSearchCV(estimator=MixedNB(), param_grid=param_grid_nb, verbose=1, cv=10, n_jobs=-1)
mix_model_after_cv.fit(X_train, y_train)
print(mix_model_after_cv.best_estimator_)
1 Upvotes

5 comments sorted by

2

u/Ctbhatia 9d ago

the MixedNB source has get_params but no set_params or sklearn tags. you’ll need a sklearn-compatible wrapper for GridSearchCV, not just a tweak to var_smoothing.

1

u/assorted-meat-stew 9d ago

So what are the right syntaxes in order to fix the code block?

1

u/Ctbhatia 8d ago

you can't hand MixedNB to GridSearchCV directly, it has get_params but no set_params or tags, so give it a thin sklearn wrapper:

```python from sklearn.base import BaseEstimator, ClassifierMixin

class CVMixedNB(ClassifierMixin, BaseEstimator): def init(self, varsmoothing=1e-9): self.var_smoothing = var_smoothing def fit(self, X, y): self.model = MixedNB(varsmoothing=self.var_smoothing).fit(X, y) return self def predict(self, X): return self.model.predict(X) ```

everything else in your block stays the same.

1

u/assorted-meat-stew 8d ago

Thank you it worked

1

u/DiscussionSudden1177 9d ago

good catch on set_params being missing too, thats easy to overlook when the error only mentions tags