public class NaiveBayes extends Classifier
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
name
Name of classifier
|
categories, random
Constructor and Description |
---|
NaiveBayes(java.lang.String[] categories,
boolean debug)
Create a naive Bayes classifier with these attributes
|
Modifier and Type | Method and Description |
---|---|
protected double[] |
calculatePriors(java.util.List<Example> 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<java.lang.String,double[]> |
conditionalProbs(java.util.List<Example> trainExamples)
Calculates the conditional probs of each feature in the different categories
|
protected void |
displayProbs(double[] classPriors,
java.util.Hashtable<java.lang.String,double[]> 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 |
setDebug(boolean bool)
Sets the debug flag
|
void |
setEpsilon(double ep)
Sets the value of EPSILON (default 1e-6)
|
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.List<Example> trainExamples)
Trains the Naive Bayes classifier - estimates the prior probs and calculates the
counts for each feature in different categories
|
argMax, getCategories
public static final java.lang.String name
public NaiveBayes(java.lang.String[] categories, boolean debug)
categories
- The array of Strings containing the category namesdebug
- Flag to turn on detailed outputpublic 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.List<Example> 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.List<Example> trainExamples)
trainExamples
- The training examples from which class priors will be estimatedprotected java.util.Hashtable<java.lang.String,double[]> conditionalProbs(java.util.List<Example> 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<java.lang.String,double[]> featureHash)
classPriors
- Prior probsfeatureHash
- Feature hashtable after training