Click here to Skip to main content
12,357,929 members (70,204 online)
Click here to Skip to main content
Add your own
alternative version

Stats

110.3K views
6.8K downloads
73 bookmarked
Posted

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

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.

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

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)

Share

About the Author

ErichG
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
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 
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
Member 312529116-Jan-11 1:49
memberMember 312529116-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_Sal11-Sep-07 16:11
memberVaclav_Sal11-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 
AnswerRe: How can I use your classifier to classify 20NewsGroup? Pin
ErichG2-Jan-07 6:02
memberErichG2-Jan-07 6:02 
QuestionRe: How can I use your classifier to classify 20NewsGroup? Pin
Tokes Erno25-Apr-07 13:29
memberTokes Erno25-Apr-07 13:29 

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.

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