MATLAB Answers


Naive Bayes - within-class variance must be positive.

Asked by Nirmal
on 4 Jun 2012

I am trying to use Naive Bayes for some classification task, I am not sure what it is complaining about.

??? Error using ==>>gaussianFit at 535
The within-class variance in each feature of TRAINING must be positive. The within-class variance in
feature 5 6 12 13 15 16 17 in class 1 are not positive.
Error in ==> at 498
            obj = gaussianFit(obj, training, gindex);

Thank you for reading

  1 Comment

Are you able to post a small bit of your data and code that exhibit the error?


No products are associated with this question.

1 Answer

Answer by Tom Lane
on 5 Jun 2012
 Accepted answer

Suppose you have data X and classes C. Can you look at


If you see that columns 5, 6, 12, etc. have zero variance, that is the problem. The fit is based on fitting a normal distribution separately for each class and feature. If any class has 0 variance for a feature, that normal fit is degenerate.

What you want to do about this depends on you. It is possible to change the fit to a kernel density estimate and specify the width. Or you could try a decision tree or knn classifier.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!