I've built a little naive Bayesian classifier that works with Boolean and real values. Boolean distributions are dealt with via Bernoulli distributions, while real valued data are dealt with kernel mixture estimators. I'm currently in the process of adding count data in.

How would one deal with categorical data though, e.g. Monday, Tuesday, Wednesday, or Toyota, Honda, Ford?

My initial thoughts are to assign a number to each category, treat it as a normal real value and round to the nearest integer category on prediction. That seems very wrong to me though.

  • $\begingroup$ Not sure about the correct answer, but your initial thought is definitely incorrect (as you suggest), as that implies that there is an ordering to the categories. $\endgroup$
    – eykanal
    Dec 17 '12 at 20:51
  • 1
    $\begingroup$ I'm not sure I follow what you mean "treat it as a normal real value and round to the nearest integer category on prediction", but it sounds like an incorrect approach. Categorical valued data is treated similar to boolean data. It is a discrete variable and should be treated as discrete, not real value. Just assign integers $1,...,K$ as labels to your $K$ categories, then use the counts to estimate parameters of your categorical distribution just as you used the counts to estimate the Bernoulli's. $\endgroup$
    – jerad
    Dec 17 '12 at 21:21
  • $\begingroup$ Correct, it is the incorrect approach. I wasn't sure what the correct technique was. Also, oddly enough the platform I'm using, Mathematica, doesn't seem to have a categorical/discrete distribution available to it. Jarad, thanks for the pointer I'll get working on this. $\endgroup$
    – user17880
    Dec 17 '12 at 22:36

For an Naive Bayes classifier, categorical values are the easiest to deal with. All you are really after is P(Feature | Class). This should be easy for the days of the week. Compute P(Monday | Class=Yes) and so on.


The way to deal with categorical data is to create each category as a feature and with boolean values. Not only this way removes the limitation of categories for some of the libraries (no applicable here since you have written your own function) but also it's fast.

  • $\begingroup$ Are you sure this works? If we encode each category as a boolean feature, that pretty much implicitly violates the independence assumption of Naive Bayes. Please see Ami Tavory's answer to this question. $\endgroup$
    – AdmiralWen
    Jul 6 '17 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy