Click here to Skip to main content
Click here to Skip to main content

A Naive Bayesian Classifier in C#

, 28 May 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A Naive Bayesian Classifier in C#
Sample Image - pict.gif


I was looking for a way to classify short texts into several categories. A simple but probably sufficient method seemed to be naive bayesian classification. Looking for some readily available stuff, I found many different implementations in Perl or Java. The only CLR implementation I could find was NClassifier, yet it was not doing classification into multiple classes. Therefore I decided to write my own.


There is plenty of information around on the Internet describing the theory of bayesian classification. Wikipedia has a good introduction.

Using the Code

First, create an instance of BayesClassifier.Classifier.

BayesClassifier.Classifier m_Classifier = new BayesClassifier.Classifier();

Tip: You may experiment with BayesClassifier.ExcludedWords to define the words that you will consider irrelevant for your classification. That can lead to smaller dictionaries and therefore speed up the classification.

Then define the categories and teach each category:

m_Classifier.TeachCategory("Cat1", new System.IO.StreamReader(file));
m_Classifier.TeachPhrases("Cat2", new string[] { "Hi", "HoHo" });

Finally the method BayesClassifier.Classifier.Classify will return the classification result.

Dictionary<string, double> score = 
    m_Classifier.Classify(new System.IO.StreamReader(file));

Let me know if you have any questions or suggestions, and let me know if you have any experiences with the applicability of the naive bayesian approach. (Since the (wrong) assumption of word independence might turn out to influence the result).


  • 28th May, 2006: Version 1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


Germany Germany
No Biography provided

Comments and Discussions

Questionnice PinmemberMember 1055954512-Jul-14 3:41 
QuestionHow to use your Code? PinmemberLucio Fonseca1-Aug-13 17:27 
SuggestionBig performance improvement possible? Pinmemberpsionski25-Oct-12 10:28 
GeneralMy vote of 4 PinmemberT. Abdul Rahman15-Nov-11 2:24 
GeneralMy vote of 4 PinmemberInterstingCodes7-Mar-11 18:11 
GeneralLearn and Test PinmemberMember 312529116-Jan-11 2:49 
GeneralNeed help for mail filter PinmemberKasunmit21-Jul-10 17:50 
QuestionWhy are the results so low? PinmemberKeith Vinson10-Apr-08 14:04 
GeneralTranslating the scores to regular probabilities. PinmemberJeepy29-Jan-08 12:44 
GeneralRe: Translating the scores to regular probabilities. PinmemberKeith Vinson10-Apr-08 13:55 
GeneralRe: Translating the scores to regular probabilities. Pinmembercodedieb24-Dec-14 23:42 
QuestionImplementing in VC++ ? PinmemberVaclav_Sal11-Sep-07 17:11 
QuestionWhat exactly score means PinmemberNateD23-Aug-07 9:41 
GeneralPlease contact me Pinmemberyonido14-May-07 0:24 
QuestionI ve a Question PinmemberJunaid_Arif_Mufti26-Apr-07 21:03 
AnswerRe: I ve a Question PinmemberJunaid_Arif_Mufti26-Apr-07 21:05 
QuestionQuestions/suggestions - please contact me PinmemberMalteSteckmeister17-Dec-06 15:23 
Questionhow to use your classifier Pinmemberrkamalakar12-Dec-06 1:57 
AnswerRe: how to use your classifier PinmemberErichG2-Jan-07 6:19 
QuestionHow can I use your classifier to classify 20NewsGroup? Pinmemberjohnny198329-Nov-06 14:49 
AnswerRe: How can I use your classifier to classify 20NewsGroup? PinmemberErichG2-Jan-07 7:02 
QuestionRe: How can I use your classifier to classify 20NewsGroup? PinmemberTokes Erno25-Apr-07 14:29 
Generalwhy results tends to negative Pinmemberabdo12345678930-May-06 13:00 
GeneralRe: why results tends to negative PinmemberErichG1-Jun-06 23:39 
General5-stars all the same PinmemberGrav-Vt28-May-06 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 28 May 2006
Article Copyright 2006 by ErichG
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid