Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tune GaussianNB?

Trying to fit data with GaussianNB() gives me low accuracy score.

I'd like to try Grid Search, but it seems that parameters sigma and theta cannot be set. Is there anyway to tune GausssianNB?

like image 386
vlad Avatar asked Oct 03 '16 09:10

vlad


People also ask

What is Gaussiannb in Naive Bayes?

A Gaussian Naive Bayes algorithm is a special type of NB algorithm. It's specifically used when the features have continuous values. It's also assumed that all the features are following a gaussian distribution i.e, normal distribution.

Does Naive Bayes have Hyperparameters?

Also, naive Bayes has almost no hyperparameters to tune, so it usually generalizes well. One thing to note is that due to the feature independence assumption, the class probabilities output by naive Bayes can be pretty inaccurate.


2 Answers

You can tune 'var_smoothing' parameter like this:

nb_classifier = GaussianNB()

params_NB = {'var_smoothing': np.logspace(0,-9, num=100)}
gs_NB = GridSearchCV(estimator=nb_classifier, 
                 param_grid=params_NB, 
                 cv=cv_method,   # use any cross validation technique 
                 verbose=1, 
                 scoring='accuracy') 
gs_NB.fit(x_train, y_train)

gs_NB.best_params_
like image 175
ana Avatar answered Dec 02 '22 09:12

ana


As of version 0.20

GaussianNB().get_params().keys() returns 'priors' and 'var_smoothing'

A grid search would look like:

pipeline = Pipeline([
    ('clf', GaussianNB())
])

parameters = {
    'clf__priors': [None],
    'clf__var_smoothing': [0.00000001, 0.000000001, 0.00000001]
}

cv = GridSearchCV(pipeline, param_grid=parameters)

cv.fit(X_train, y_train)
y_pred_gnb = cv.predict(X_test)
like image 23
Helen Batson Avatar answered Dec 02 '22 08:12

Helen Batson