|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--ir.classifiers.Classifier | +--ir.classifiers.NaiveBayes
Implements the NaiveBayes Classifier with Laplace smoothing. Stores probabilities internally as logs to prevent underflow problems.
Field Summary | |
protected java.util.Vector |
Categories
Vector of categories (classes) in the data |
boolean |
debug
Flag for debug prints |
protected double |
EPSILON
Small value to be used instead of 0 in probabilities, if Laplace smoothing is not used |
protected boolean |
isLaplace
Flag to set Laplace smoothing when estimating probabilities |
static java.lang.String |
name
Name of classifier |
int |
numCategories
Number of categories |
int |
numExamples
Number of training examples, set by train function |
int |
numFeatures
Number of features |
protected BayesResult |
trainResult
Stores the training result, set by the train function |
Constructor Summary | |
NaiveBayes(java.lang.String[] categories,
boolean d)
Create an naive bayes classifier with these attributes |
Method Summary | |
protected double[] |
calculatePriors(java.util.Vector trainExamples)
Calculates the class priors |
protected double[] |
calculateProbs(Example testExample)
Calculates the prob of the testExample being generated by each category |
protected java.util.Hashtable |
conditionalProbs(java.util.Vector trainExamples)
Calculates the conditional probs of each feature in the different categories |
protected void |
displayProbs(double[] classPriors,
java.util.Hashtable featureHash)
Displays the probs for each feature in the different categories |
double |
getEpsilon()
Returns value of EPSILON |
boolean |
getIsLaplace()
Returns value of isLaplace |
java.lang.String |
getName()
Returns the name |
BayesResult |
getTrainResult()
Returns training result |
void |
setCategories(java.lang.String[] categories)
Set vector of categories (classes) in the data with the input string of categories |
void |
setDebug(boolean bool)
Sets the debug flag |
void |
setEpsilon(double ep)
Sets the value of EPSILON (default 1e-6) |
void |
setInvertedIndex(ir.vsr.InvertedIndex index)
Since NaiveBayes does not use an inverted Index, this function does nothing in the case of NaiveBayes |
void |
setLaplace(boolean bool)
Sets the Laplace smoothing flag |
boolean |
test(Example testExample)
Categorizes the test example using the trained Naive Bayes classifier, returning true if the predicted category is same as the actual category |
void |
train(java.util.Vector trainExamples)
Trains the Naive Bayes classifier - estimates the prior probs and calculates the counts for each feature in different categories |
boolean |
usesInvertedIndex()
Function to indicate that this class does not use an inverted index |
Methods inherited from class ir.classifiers.Classifier |
argMax |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected java.util.Vector Categories
protected boolean isLaplace
protected double EPSILON
protected BayesResult trainResult
public static final java.lang.String name
public int numCategories
public int numFeatures
public int numExamples
public boolean debug
Constructor Detail |
public NaiveBayes(java.lang.String[] categories, boolean d)
cats
- The array of Strings containing the category namesd
- Flag to turn on detailed outputMethod Detail |
public boolean usesInvertedIndex()
usesInvertedIndex
in class Classifier
public void setInvertedIndex(ir.vsr.InvertedIndex index)
setInvertedIndex
in class Classifier
public void setCategories(java.lang.String[] categories)
public void setDebug(boolean bool)
public void setLaplace(boolean bool)
public void setEpsilon(double ep)
public java.lang.String getName()
getName
in class Classifier
public double getEpsilon()
public BayesResult getTrainResult()
public boolean getIsLaplace()
public void train(java.util.Vector trainExamples)
train
in class Classifier
trainExamples
- The vector of training examplespublic boolean test(Example testExample)
test
in class Classifier
testExample
- The test example to be categorizedprotected double[] calculatePriors(java.util.Vector trainExamples)
trainExample
- The training examples from which class priors will be estimatedprotected java.util.Hashtable conditionalProbs(java.util.Vector trainExamples)
trainExamples
- The training examples from which counts will be estimatedprotected double[] calculateProbs(Example testExample)
testExample
- The test example to be categorizedprotected void displayProbs(double[] classPriors, java.util.Hashtable featureHash)
classPriors
- Prior probsfeatureHash
- Feature hashtable after training
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |