使用GaussianNB classifi交叉验证的Python Naive Bayes

2024-09-30 20:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将带有10倍分层交叉验证的Naive Bayes应用到我的数据中,然后我想看看模型如何处理我最初设置的测试数据。 然而,我得到的结果(即预测结果和概率值y_pred_nb2y_score_nb2)与我在没有任何交叉验证的情况下运行代码时相同。 问题:我该如何纠正?

代码如下,其中X_train包含整个数据集的75%,而X_test包含25%。

from sklearn.model_selection import StratifiedKFold 

params = {}

#gridsearch searches for the best hyperparameters and keeps the classifier with the highest recall score
skf = StratifiedKFold(n_splits=10)

nb2 = GridSearchCV(GaussianNB(), cv=skf, param_grid=params)
%time nb2.fit(X_train, y_train)

# predict values on the test set
y_pred_nb2 = nb2.predict(X_test) 

print(y_pred_nb2)

# predicted probabilities on the test set
y_scores_nb2 = nb2.predict_proba(X_test)[:, 1]

print(y_scores_nb2)

Tags: the数据代码testontrainparamspredict