Click here to Skip to main content
15,352,974 members
Articles / Programming Languages / C#
Posted 28 May 2006


77 bookmarked

A Naive Bayesian Classifier in C#

Rate me:
Please Sign up or sign in to vote.
4.36/5 (25 votes)
28 May 2006CPOL1 min read
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

QuestionAwesome..!! Pin
riteshbawaskar16-Aug-17 4:51
Memberriteshbawaskar16-Aug-17 4:51 
Questionresult is negative !!!!! Pin
Amin Akmali6-Jan-17 5:48
MemberAmin Akmali6-Jan-17 5:48 
QuestionMy vote of 5 and a Question Pin
Md Saleem Navalur23-Jun-16 4:22
MemberMd Saleem Navalur23-Jun-16 4:22 
GeneralMy vote of 5 Pin
Kevin Whitefoot21-Jan-16 0:30
MemberKevin Whitefoot21-Jan-16 0:30 
Simple to use and handles multiple categories. Just what I was looking for.
QuestionResulted scores Pin
Paula_Dia21-May-15 11:43
MemberPaula_Dia21-May-15 11:43 
Questionnice Pin
Member 1055954512-Jul-14 2:41
MemberMember 1055954512-Jul-14 2:41 
QuestionHow to use your Code? Pin
Lucio Fonseca1-Aug-13 16:27
MemberLucio Fonseca1-Aug-13 16:27 
SuggestionBig performance improvement possible? Pin
psionski25-Oct-12 9:28
Memberpsionski25-Oct-12 9:28 
GeneralMy vote of 4 Pin
T. Abdul Rahman15-Nov-11 1:24
MemberT. Abdul Rahman15-Nov-11 1:24 
GeneralMy vote of 4 Pin
InterstingCodes7-Mar-11 17:11
MemberInterstingCodes7-Mar-11 17:11 
GeneralLearn and Test Pin
Hamed Metalgear16-Jan-11 1:49
MemberHamed Metalgear16-Jan-11 1:49 
GeneralNeed help for mail filter Pin
Kasunmit21-Jul-10 16:50
MemberKasunmit21-Jul-10 16:50 
QuestionWhy are the results so low? Pin
Keith Vinson10-Apr-08 13:04
MemberKeith Vinson10-Apr-08 13:04 
GeneralTranslating the scores to regular probabilities. Pin
Jeepy29-Jan-08 11:44
MemberJeepy29-Jan-08 11:44 
GeneralRe: Translating the scores to regular probabilities. Pin
Keith Vinson10-Apr-08 12:55
MemberKeith Vinson10-Apr-08 12:55 
GeneralRe: Translating the scores to regular probabilities. Pin
codedieb24-Dec-14 22:42
Membercodedieb24-Dec-14 22:42 
QuestionImplementing in VC++ ? Pin
Vaclav_11-Sep-07 16:11
MemberVaclav_11-Sep-07 16:11 
QuestionWhat exactly score means Pin
NateD23-Aug-07 8:41
MemberNateD23-Aug-07 8:41 
GeneralPlease contact me Pin
yonido13-May-07 23:24
Memberyonido13-May-07 23:24 
QuestionI ve a Question Pin
Junaid_Arif_Mufti26-Apr-07 20:03
MemberJunaid_Arif_Mufti26-Apr-07 20:03 
AnswerRe: I ve a Question Pin
Junaid_Arif_Mufti26-Apr-07 20:05
MemberJunaid_Arif_Mufti26-Apr-07 20:05 
QuestionQuestions/suggestions - please contact me Pin
MalteSteckmeister17-Dec-06 14:23
MemberMalteSteckmeister17-Dec-06 14:23 
Questionhow to use your classifier Pin
rkamalakar12-Dec-06 0:57
Memberrkamalakar12-Dec-06 0:57 
AnswerRe: how to use your classifier Pin
ErichG2-Jan-07 5:19
MemberErichG2-Jan-07 5:19 
QuestionHow can I use your classifier to classify 20NewsGroup? Pin
johnny198329-Nov-06 13:49
Memberjohnny198329-Nov-06 13:49 

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

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