matlab - 朴素贝叶斯 : the within-class variance in each feature of TRAINING must be positive

标签 matlab classification bayesian variance naivebayes

尝试拟合朴素贝叶斯时:

    training_data = sample; % 
    target_class = K8;
 # train model
 nb = NaiveBayes.fit(training_data, target_class);

 # prediction
 y = nb.predict(cluster3);

我得到一个错误:

??? Error using ==> NaiveBayes.fit>gaussianFit at 535
The within-class variance in each feature of TRAINING
must be positive. The within-class variance in feature
2 5 6 in class normal. are not positive.

Error in ==> NaiveBayes.fit at 498
            obj = gaussianFit(obj, training, gindex);

任何人都可以阐明这个问题以及如何解决它吗?请注意,我已经阅读了类似的帖子 here但我不确定该怎么办?似乎它试图根据列而不是行进行拟合,类方差应该基于每一行属于特定类的概率。如果我删除这些列,它会起作用,但显然这不是我想要做的。

最佳答案

假设您的代码(或来自 mathworks 的 NaiveBayes 代码)中没有任何错误,并再次假设您的 training_data 是 NxD 的形式,其中有 N 个观察值和 D 个特征,然后是第 2、5 和 6 列至少一个类完全为零。如果您的训练数据相对较小且类别数量较多,则可能会发生这种情况,其中单个类别可能由一些观察值表示。由于 NaiveBayes 默认将所有特征视为正态分布的一部分,因此它无法处理与单个类相关的所有特征的方差为零的列。换句话说,NaiveBayes 无法通过将正态分布拟合到特定类的特征来找到概率分布的参数(注意:分布的默认值是 normal)。

查看您的特征的性质。如果它们似乎不遵循每个类中的正态分布,那么 normal 不是您要使用的选项。也许你的数据更接近多项式模型 mn:

nb = NaiveBayes.fit(training_data, target_class, 'Distribution', 'mn');

关于matlab - 朴素贝叶斯 : the within-class variance in each feature of TRAINING must be positive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13427664/

相关文章:

matlab - 检测图像中的数独方 block

matlab - MATLAB 中结构的高效索引

machine-learning - 为什么线性模型适用于图像分类?

artificial-intelligence - 贝叶斯网络中的变量必须是 bool 值吗?

c++ - 将 rmultinom 与 Rcpp 结合使用

matlab - 我可以在一个循环内的多个 Matlab 图形中制作多个子图吗?

image-processing - 试图理解高斯模糊在 matlab 中的实现

javascript - 如何计算Google Earth Engine中分类图像(Landsat)中每个类别的像素总和?

python - 使用K-nn算法处理音乐流派,如何处理提取的特征?

python - pymc3 中具有(大)时间^2 项的分层模型中的 MCMC 收敛