C# implementation of Naive Bayes classifier test Naive BayesNaive Bayes is, probably, the simplest way of categorization of documents. It requires the training set: documents for which categories are already determined by an expert. There is no shortage of articles explaining theoretical part so I reduce the explanation to a simple example. Let us define our training set as two documents shown below in the table. Presume that each of them belong to different category (cat1 and cat2).
We can see that the word w1 can be seen in both categories but we believe that probability for this word to be seen in category 1 is 8/9 and in category 2 is 1/9. On the reason that will be clear later we should not have probability of occurrence of any word to be equal to 0, so we add 1 to every cell, where word count is zero. However, in order to not alter the experimental data significantly we add 1 to every cell in the table.
On the next step we convert all frequencies to probabilities within the training set and add another two lines of experimental data, for which we need to determine the category.
Now we can calculate the probability for document 3 to be in category 1. P{doc3 = cat1} = (0.82^{2} * 0.5) / const = 0.3362 The constant in denominator can be ignored. The probability for document 3 to be in category 2 is accordingly P{doc3 = cat2} = (0.18^{2} * 0.5) / const = 0.0162 The obvious conclusion is that document 3 most likely belong to category 1. Same simple computations for document 4 yield P{doc4 = cat1} = (0.50 * 0.11^{4} * 0.20) / const = 0.000014641 P{doc4 = cat2} = (0.50 * 0.89^{4} * 0.80) / const = 0.2510 Since real case scenario presumes thousands of words, to compute everything in probabilities is not computationally convenient. On that reason people, usually, use logarithmic scale: log(P{doc4 = cat1}) = log(0.50) + 4 * log(0.11) + log(0.20) = 4.83 log(P{doc4 = cat2}) = log(0.50) + 4 * log(0.89) + log(0.80) = 0.60 I added one more correction: it is the size of the file. When the file is large it contains significantly larger number of words and probabilities are skewed. Experimental programming part was not very challenging. As it was expected the result depended on the training set and varied in a very wide range from 30 percent of correct recognition to almost 90 percent. Other methods were not that sensitive for choosing of training set. NB is sort of lottery, it can be better than anything else if you have luck. On that reason it is better to use it as supplementary improvement tool, for example, after HAC, using already categorized files. In my experiments with PATENTCORPUS128 I found the training sample of 12 files out of 128 that provided highest possible accuracy near 93%. Unfortunately taking random training sample of the same size shows accuracy between 60% and 80%. 
