我正在研究二元分类模型,分类器是朴素贝叶斯。我有一个几乎平衡的数据集,但是当我预测时,我收到以下错误消息:
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
我正在使用 gridsearch 和 CV k-fold 10。测试集和预测包含这两个类,所以我不理解该消息。我正在研究 6 个其他模型的相同数据集、训练/测试分割、CV 和随机种子,这些模型工作得非常完美。数据从外部提取到数据帧中,进行随机化并且种子是固定的。然后,朴素贝叶斯分类模型在此代码片段之前的开头对文件进行分类。
X_train, X_test, y_train, y_test, len_train, len_test = \
train_test_split(data['X'], data['y'], data['len'], test_size=0.4)
pipeline = Pipeline([
('classifier', MultinomialNB())
])
cv=StratifiedKFold(len_train, n_folds=10)
len_train = len_train.reshape(-1,1)
len_test = len_test.reshape(-1,1)
params = [
{'classifier__alpha': [0, 0.0001, 0.001, 0.01]}
]
grid = GridSearchCV(
pipeline,
param_grid=params,
refit=True,
n_jobs=-1,
scoring='accuracy',
cv=cv,
)
nb_fit = grid.fit(len_train, y_train)
preds = nb_fit.predict(len_test)
print(confusion_matrix(y_test, preds, labels=['1','0']))
print(classification_report(y_test, preds))
我被Python“强迫”改变了系列的形状,也许这就是罪魁祸首?