二元分类模型高斯朴素贝叶斯中的警告消息?

Warning Message in binary classification model Gaussian Naive Bayes?

我正在使用 multiclass classification-ready 数据集,其中包含 14 个连续变量和从 1 到 10 的 classes。 这是数据文件: https://drive.google.com/file/d/1nPrE7UYR8fbTxWSuqKPJmJOYG3CGN5y9/view?usp=sharing

我的目标是将 scikit-learn Gaussian NB 模型应用于数据,但在二进制 classification 任务中,只有 class 2 是正标签,其余 classes都是负数。为此,我做了以下代码:

from sklearn.naive_bayes import GaussianNB, CategoricalNB
import pandas as pd
dataset = pd.read_csv("PD_21_22_HA1_dataset.txt", index_col=False, sep="\t")
x_d = dataset.values[:, :-1]
y_d = dataset.values[:, -1]
### train_test_split to split the dataframe into train and test sets
## with a partition of 20% for the test https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
X_TRAIN, X_IVS, y_TRAIN, y_IVS = train_test_split(x_d, y_d, test_size=0.20, random_state=23)

yc_TRAIN=np.array([int(i==2) for i in y_TRAIN])
mdl = GaussianNB()

mdl.fit(X_TRAIN, yc_TRAIN)
preds = mdl.predict(X_IVS)
# binarization of "y_true" array
yc_IVS=np.array([int(i==2) for i in y_IVS])
print("The Precision is: %7.4f" % precision_score(yc_IVS, preds))
print("The Matthews correlation coefficient is: %7.4f" % matthews_corrcoef(yc_IVS, preds))

但我在计算精度时收到以下警告消息:

UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples.

马太相关系数函数也输出0,给出runtimewarning: invalid value encountered in double_scalars信息

此外,通过检查 preds,我发现该模型仅预测 negatives/zeros。

我试过按照某些论坛的建议增加 20% 的测试分区,但没有任何效果。

这仅仅是模型无法适应数据的问题,还是我做错了什么可能将错误的数据输入模型 format/type?

编辑:yc_TRAIN 是将 class 2 中的所有案例转换为我的真阳性案例“1”并将其余 classes 转换为 negatives/0 的结果,所以它是一个长度为 9450 的一维数组(与我的预测案例总数相匹配),其中包含超过 8697 个 0 和 753 个 1,所以它的方面将是这样的:

[0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ] 

你的代码看起来不错;这是一个关于不平衡数据集的 classic 问题,它实际上意味着你没有足够的训练数据来正确地 class 验证罕见的积极 class。

在给定的代码中你唯一可以改进的是在train_test_split中设置stratify=y_d,以获得分层训练集; 减少测试集的大小(即留下更多样本用于训练)可能也有帮助:

X_TRAIN, X_IVS, y_TRAIN, y_IVS = train_test_split(x_d, y_d, test_size=0.10, random_state=23, stratify=y_d)

如果这不起作用,您应该开始考虑应用 class 不平衡技术(或不同的模型);但这不再是一个 编程 问题,而是一个 theory/methodology 问题,它应该在适当的 SE 站点而不是这里解决(请参阅介绍和注释 machine-learning tag info).