python - sklearn : Naive Bayes classifier gives low accuracy

标签 python machine-learning scikit-learn bayesian naivebayes

我有一个数据集,其中包含 200000 个带标签的训练示例。 对于每个训练示例,我有 10 个特征,包括连续的和离散的。 我正在尝试使用 python 的 sklearn 包来训练模型并进行预测,但我遇到了一些麻烦(也有一些问题)。

首先让我写下我目前为止写的代码:

from sklearn.naive_bayes import GaussianNB
# data contains the 200 000 examples
# targets contain the corresponding labels for each training example
gnb = GaussianNB()
gnb.fit(data, targets)
predicted = gnb.predict(data)

问题是我得到的准确率非常低(太多错误分类的标签)——大约 20%。 但是,我不太确定数据是否存在问题(例如,需要更多数据或其他问题)或代码是否存在问题。

在给定具有离散和连续特征的数据集的情况下,这是实现朴素贝叶斯分类器的正确方法吗?

此外,在机器学习中,我们知道数据集应该分为训练集和验证/测试集。这是由 sklearn 自动执行的,还是我应该使用训练数据集 fit 模型,然后使用验证集调用 predict

任何想法或建议将不胜感激。

最佳答案

The problem is that I get really low accuracy (too many misclassified labels) - around 20%. However I am not quite sure whether there is a problem with the data (e.g. more data is needed or something else) or with the code.

对于朴素贝叶斯来说,这不是什么大错误,这是一个非常简单的分类器,你不应该期望它很强大,更多的数据可能无济于事。您的高斯估计器可能已经非常好,只是朴素的假设是问题所在。使用更强的模型。您可以从随机森林开始,因为即使非该领域的专家也能轻松使用它。

Is this the proper way to implement a Naive Bayes classifier given a dataset with both discrete and continuous features?

不,不是,您应该在离散特征中使用不同的分布,但是 scikit-learn 不支持,您必须手动执行此操作。如前所述 - 改变你的模型。

Furthermore, in Machine Learning we know that the dataset should be split into training and validation/testing sets. Is this automatically performed by sklearn or should I fit the model using the training dataset and then call predict using the validation set?

没有任何事情以这种方式自动完成,您需要自己完成(scikit learn 有很多工具 - 请参阅交叉验证包)。

关于python - sklearn : Naive Bayes classifier gives low accuracy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40535925/

相关文章:

python - 具有低标准差的高斯图像滤波图。值(value)观

java - JSAT : Data wrangling/manipulating

python - scikit-learn 回归预测结果太好了。我搞砸了什么?

python - 使用 pandas 和 scikit (OneHotEncoder) 虚拟化逻辑回归的分类变量

python - 寻找组合的组合

python - 获得一个数字的所有除数的最佳方法是什么?

python - 将 Django UserCreationForm 的密码验证错误从密码 2 移至密码 1

tensorflow - flutter TFLite 错误 : "metal_delegate.h" File Not Found

python - 值错误: negative dimensions are not allowed in scikit linear regression CV model with sparse matrices

python - 为什么 scikit learn 的平均精度分数返回 nan?