Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

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 186.8K   9.6K   77   31
A Naive Bayesian Classifier in C#
Sample Image - pict.gif

Introduction

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.

Background

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.

C#
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:

C#
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.

C#
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).

History

  • 28th May, 2006: Version 1.0

License

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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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