Click here to Skip to main content
13,861,169 members
Click here to Skip to main content
Add your own
alternative version


77 bookmarked
Posted 28 May 2006
Licenced CPOL

A Naive Bayesian Classifier in C#

, 28 May 2006
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

You may also be interested in...

Comments and Discussions

QuestionAwesome..!! Pin
riteshbawaskar16-Aug-17 5:51
memberriteshbawaskar16-Aug-17 5:51 
Questionresult is negative !!!!! Pin
Amin Akmali6-Jan-17 6:48
memberAmin Akmali6-Jan-17 6:48 
QuestionMy vote of 5 and a Question Pin
Md Saleem Navalur23-Jun-16 5:22
memberMd Saleem Navalur23-Jun-16 5:22 
GeneralMy vote of 5 Pin
Kevin Whitefoot21-Jan-16 1:30
memberKevin Whitefoot21-Jan-16 1:30 
QuestionResulted scores Pin
Paula_Dia21-May-15 12:43
memberPaula_Dia21-May-15 12:43 
Questionnice Pin
Member 1055954512-Jul-14 3:41
memberMember 1055954512-Jul-14 3:41 
QuestionHow to use your Code? Pin
Lucio Fonseca1-Aug-13 17:27
memberLucio Fonseca1-Aug-13 17:27 
SuggestionBig performance improvement possible? Pin
psionski25-Oct-12 10:28
memberpsionski25-Oct-12 10:28 
GeneralMy vote of 4 Pin
T. Abdul Rahman15-Nov-11 2:24
memberT. Abdul Rahman15-Nov-11 2:24 
GeneralMy vote of 4 Pin
InterstingCodes7-Mar-11 18:11
memberInterstingCodes7-Mar-11 18:11 
GeneralLearn and Test Pin
Hamed Metalgear16-Jan-11 2:49
memberHamed Metalgear16-Jan-11 2:49 
GeneralNeed help for mail filter Pin
Kasunmit21-Jul-10 17:50
memberKasunmit21-Jul-10 17:50 
QuestionWhy are the results so low? Pin
Keith Vinson10-Apr-08 14:04
memberKeith Vinson10-Apr-08 14:04 
GeneralTranslating the scores to regular probabilities. Pin
Jeepy29-Jan-08 12:44
memberJeepy29-Jan-08 12:44 
GeneralRe: Translating the scores to regular probabilities. Pin
Keith Vinson10-Apr-08 13:55
memberKeith Vinson10-Apr-08 13:55 
GeneralRe: Translating the scores to regular probabilities. Pin
codedieb24-Dec-14 23:42
membercodedieb24-Dec-14 23:42 
QuestionImplementing in VC++ ? Pin
Vaclav_11-Sep-07 17:11
memberVaclav_11-Sep-07 17:11 
QuestionWhat exactly score means Pin
NateD23-Aug-07 9:41
memberNateD23-Aug-07 9:41 
GeneralPlease contact me Pin
yonido14-May-07 0:24
memberyonido14-May-07 0:24 
QuestionI ve a Question Pin
Junaid_Arif_Mufti26-Apr-07 21:03
memberJunaid_Arif_Mufti26-Apr-07 21:03 
AnswerRe: I ve a Question Pin
Junaid_Arif_Mufti26-Apr-07 21:05
memberJunaid_Arif_Mufti26-Apr-07 21:05 
QuestionQuestions/suggestions - please contact me Pin
MalteSteckmeister17-Dec-06 15:23
memberMalteSteckmeister17-Dec-06 15:23 
Questionhow to use your classifier Pin
rkamalakar12-Dec-06 1:57
memberrkamalakar12-Dec-06 1:57 
AnswerRe: how to use your classifier Pin
ErichG2-Jan-07 6:19
memberErichG2-Jan-07 6:19 
QuestionHow can I use your classifier to classify 20NewsGroup? Pin
johnny198329-Nov-06 14:49
memberjohnny198329-Nov-06 14: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 28 May 2006
Article Copyright 2006 by ErichG
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid