public class NaiveBayes extends Learner
P(v) Prodf P(f|v)where Prod is a multiplication quantifier over f, and f stands for a feature. The value corresponding to the highest score is selected as the prediction. Feature values that were never observed given a particular prediction value during training are smoothed with a configurable constant that defaults to e-15.
This Learner
learns a discrete
classifier from other discrete
classifiers. Features coming from real
classifiers are ignored. It is also
assumed that a single discrete label feature will be produced in association with each example
object. A feature taking one of the values observed in that label feature will be produced by the
learned classifier.
This algorithm's user-configurable parameters are stored in member fields of this class. They may
be set via either a constructor that names each parameter explicitly or a constructor that takes
an instance of Parameters
as input. The documentation in each
member field in this class indicates the default value of the associated parameter when using the
former type of constructor. The documentation of the associated member field in the
Parameters
class indicates the default value of the parameter when
using the latter type of constructor.
NaiveBayes.NaiveBayesVector
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected static class |
NaiveBayes.Count
A
Count object stores two doubles , one which holds a accumulated
count value and the other intended to hold the natural logarithm of the count. |
protected class |
NaiveBayes.NaiveBayesVector
Keeps track of all the counts associated with a given label.
|
static class |
NaiveBayes.Parameters
Simply a container for all of
NaiveBayes 's configurable parameters. |
Modifier and Type | Field and Description |
---|---|
static int |
defaultSmoothing
The default conditional feature probability is e
defaultSmoothing
. |
protected edu.illinois.cs.cogcomp.core.datastructures.vectors.OVector |
network
One
NaiveBayes.NaiveBayesVector for each observed prediction value. |
protected double |
smoothing
The exponential of this number is used as the conditional probability of a feature that was
never observed during training; default
defaultSmoothing . |
candidates, encoding, extractor, labeler, labelLexicon, lcFilePath, lexFilePath, lexicon, lossFlag, predictions, readLexiconOnDemand
containingPackage, name
Constructor and Description |
---|
NaiveBayes()
Default constructor.
|
NaiveBayes(double smooth)
Initializes the smoothing constant.
|
NaiveBayes(NaiveBayes.Parameters p)
Initializing constructor.
|
NaiveBayes(String n)
Initializes the name of the classifier.
|
NaiveBayes(String name,
double smooth)
Initializes the name and smoothing constant.
|
NaiveBayes(String n,
NaiveBayes.Parameters p)
Initializing constructor.
|
Modifier and Type | Method and Description |
---|---|
FeatureVector |
classify(int[] exampleFeatures,
double[] exampleValues)
Prediction value counts and feature counts given a particular prediction value are used to
select the most likely prediction value.
|
Object |
clone()
Returns a deep clone of this learning algorithm.
|
String |
discreteValue(int[] exampleFeatures,
double[] exampleValues)
Prediction value counts and feature counts given a particular prediction value are used to
select the most likely prediction value.
|
Feature |
featureValue(int[] f,
double[] v)
Returns the classification of the given example as a single feature instead of a
FeatureVector . |
void |
forget()
Clears the network.
|
Learner.Parameters |
getParameters()
Retrieves the parameters that are set in this learner.
|
void |
learn(int[] exampleFeatures,
double[] exampleValues,
int[] exampleLabels,
double[] labelValues)
Trains the learning algorithm given an object as an example.
|
void |
read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Reads the binary representation of a learner with this object's run-time type, overwriting
any and all learned or manually specified parameters as well as the label lexicon but without
modifying the feature lexicon.
|
ScoreSet |
scores(int[] exampleFeatures,
double[] exampleValues)
The scores in the returned
ScoreSet are the posterior probabilities of each possible
label given the example. |
void |
setLabeler(Classifier l)
Sets the labeler.
|
void |
setParameters(NaiveBayes.Parameters p)
Sets the values of parameters that control the behavior of this learning algorithm.
|
void |
setSmoothing(double s)
Sets the smoothing parameter to the specified value.
|
void |
write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
Writes the learned function's internal representation in binary form.
|
void |
write(PrintStream out)
Writes the algorithm's internal representation as text.
|
classify, classify, classify, classify, countFeatures, createPrediction, createPrediction, demandLexicon, discreteValue, discreteValue, doneLearning, doneWithRound, emptyClone, featureValue, featureValue, getCurrentLexicon, getExampleArray, getExampleArray, getExtractor, getLabeler, getLabelLexicon, getLexicon, getLexiconDiscardCounts, getLexiconLocation, getModelLocation, getPrunedLexiconSize, initialize, learn, learn, learn, learn, read, readLabelLexicon, readLearner, readLearner, readLearner, readLearner, readLearner, readLearner, readLexicon, readLexicon, readLexiconOnDemand, readLexiconOnDemand, readModel, readModel, readParameters, realValue, realValue, realValue, save, saveLexicon, saveModel, scores, scores, scoresAugmented, setCandidates, setEncoding, setExtractor, setLabelLexicon, setLexicon, setLexiconLocation, setLexiconLocation, setLossFlag, setModelLocation, setModelLocation, setParameters, setReadLexiconOnDemand, unclone, unsetLossFlag, write, writeLexicon, writeModel, writeParameters
allowableValues, classify, discreteValueArray, getCompositeChildren, getInputType, getOutputType, realValueArray, test, toString, valueIndexOf
public static final int defaultSmoothing
defaultSmoothing
.protected double smoothing
defaultSmoothing
.protected edu.illinois.cs.cogcomp.core.datastructures.vectors.OVector network
NaiveBayes.NaiveBayesVector
for each observed prediction value.public NaiveBayes()
public NaiveBayes(double smooth)
smooth
- The exponential of this number is used as the conditional probability of a
feature that was never observed during training.public NaiveBayes(NaiveBayes.Parameters p)
NaiveBayes.Parameters
object.p
- The settings of all parameters.public NaiveBayes(String n)
n
- The classifier's name.public NaiveBayes(String name, double smooth)
name
- The classifier's name.smooth
- The exponential of this number is used as the conditional probability of a
feature that was never observed during training.public NaiveBayes(String n, NaiveBayes.Parameters p)
NaiveBayes.Parameters
object.n
- The name of the classifier.p
- The settings of all parameters.public void setParameters(NaiveBayes.Parameters p)
p
- The parameters.public Learner.Parameters getParameters()
getParameters
in class Learner
public void setSmoothing(double s)
s
- The new value for the smoothing parameter.public void setLabeler(Classifier l)
setLabeler
in class Learner
l
- A labeling classifier.public void learn(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, double[] labelValues)
public ScoreSet scores(int[] exampleFeatures, double[] exampleValues)
ScoreSet
are the posterior probabilities of each possible
label given the example.scores
in class Learner
exampleFeatures
- The example's array of feature indices.exampleValues
- The example's array of feature values.public Feature featureValue(int[] f, double[] v)
FeatureVector
.featureValue
in class Learner
f
- The features array.v
- The values array.public String discreteValue(int[] exampleFeatures, double[] exampleValues)
discreteValue
in class Learner
exampleFeatures
- The example's array of feature indices.exampleValues
- The example's array of feature values.public FeatureVector classify(int[] exampleFeatures, double[] exampleValues)
public void write(PrintStream out)
public void write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
public void read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Copyright © 2016. All rights reserved.