python 如何调整GaussianNB?

k97glaaz  于 2023-04-04  发布在  Python
关注(0)|答案(4)|浏览(424)

尝试用GaussianNB()拟合数据会给我带来较低的准确性分数。
我想尝试网格搜索,但似乎无法设置参数sigmatheta。是否有任何方法可以调优GausssianNB

wqlqzqxt

wqlqzqxt1#

您可以像这样调整'var_smoothing'参数:

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_
8aqjt8rx

8aqjt8rx2#

截至version 0.20
GaussianNB().get_params().keys()返回'priors'和'var_smoothing'
网格搜索看起来像这样:

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)
g52tjvyc

g52tjvyc3#

在sklearn管道中,它可能看起来如下所示:

pipe = Pipeline(steps=[
                    ('pca', PCA()),
                    ('estimator', GaussianNB()),
                    ])
    
parameters = {'estimator__var_smoothing': [1e-11, 1e-10, 1e-9]}
Bayes = GridSearchCV(pipe, parameters, scoring='accuracy', cv=10).fit(X_train, y_train)
print(Bayes.best_estimator_)
print('best score:')
print(Bayes.best_score_)
predictions = Bayes.best_estimator_.predict(X_test)
ecbunoof

ecbunoof4#

朴素贝叶斯不需要调整任何超参数。

相关问题