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

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

Comments and Discussions

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

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 | Mobile
Web03 | 2.8.141029.1 | Last Updated 28 May 2006
Article Copyright 2006 by ErichG
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid