尝试拟合朴素贝叶斯时:
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/