jsat.classifiers.bayesian
Class MultinomialNaiveBayes
- java.lang.Object
-
- jsat.classifiers.BaseUpdateableClassifier
-
- jsat.classifiers.bayesian.MultinomialNaiveBayes
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, Classifier, UpdateableClassifier, Parameterized
public class MultinomialNaiveBayes extends BaseUpdateableClassifier implements Parameterized
An implementation of the Multinomial Naive Bayes model (MNB). In this model, vectors are implicitly assumed to be sparse and that zero values can be skipped. This model requires that all numeric features be non negative, any negative value will be treated as a zero.
Note: the is no reason to ever use more than oneepochfor MNB
MNB requires taking the log probabilities to perform predictions, which created a trade off. Updating the classifier requires the non log form, but updates require the log form, making classification take considerably longer to take the logs of the probabilities. This can be reduced byfinalizingthe model. This prevents the model from being updated further, but reduces classification time. By default, this will be done after a call totrain(jsat.classifiers.ClassificationDataSet)but not afterupdate(jsat.classifiers.DataPoint, int)- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description MultinomialNaiveBayes()Creates a new Multinomial model with laplace smoothingMultinomialNaiveBayes(double smoothing)Creates a new Multinomial model with the given amount of smoothing
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description CategoricalResultsclassify(DataPoint data)Performs classification on the given data point.MultinomialNaiveBayesclone()voidfinalizeModel()Finalizes the current model.doublegetSmoothing()booleanisFinalizeAfterTraining()Returnstrueif the model will be finalized after batch training.voidsetFinalizeAfterTraining(boolean finalizeAfterTraining)If settrue, the model will be finalized after a call totrain(jsat.classifiers.ClassificationDataSet).voidsetSmoothing(double smoothing)Sets the amount of smoothing applied to the model.voidsetUp(CategoricalData[] categoricalAttributes, int numericAttributes, CategoricalData predicting)Prepares the classifier to begin learning from itsUpdateableClassifier.update(jsat.classifiers.DataPoint, int)method.booleansupportsWeightedData()Indicates whether the model knows how to train using weighted data points.voidtrain(ClassificationDataSet dataSet)Trains the classifier and constructs a model for classification using the given data set.voidtrain(ClassificationDataSet dataSet, boolean parallel)Trains the classifier and constructs a model for classification using the given data set.voidupdate(DataPoint dataPoint, int targetClass)Updates the classifier by giving it a new data point to learn from.-
Methods inherited from class jsat.classifiers.BaseUpdateableClassifier
getEpochs, setEpochs, trainEpochs
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jsat.parameters.Parameterized
getParameter, getParameters
-
-
-
-
Constructor Detail
-
MultinomialNaiveBayes
public MultinomialNaiveBayes()
Creates a new Multinomial model with laplace smoothing
-
MultinomialNaiveBayes
public MultinomialNaiveBayes(double smoothing)
Creates a new Multinomial model with the given amount of smoothing- Parameters:
smoothing- the amount of smoothing to apply
-
-
Method Detail
-
setSmoothing
public void setSmoothing(double smoothing)
Sets the amount of smoothing applied to the model.
Using a value of 1.0 is equivalent to laplace smoothing
The smoothing can be changed after the model has already been trained without needed to re-train the model for the change to take effect.- Parameters:
smoothing- the positive smoothing constant
-
getSmoothing
public double getSmoothing()
- Returns:
- the smoothing applied to categorical counts
-
setFinalizeAfterTraining
public void setFinalizeAfterTraining(boolean finalizeAfterTraining)
If settrue, the model will be finalized after a call totrain(jsat.classifiers.ClassificationDataSet). This prevents the model from being updated in an online fashion for an reduction in classification time.- Parameters:
finalizeAfterTraining-trueto finalize after a call to train,falseto keep the model updatable.
-
isFinalizeAfterTraining
public boolean isFinalizeAfterTraining()
Returnstrueif the model will be finalized after batch training.falseif it will be left in an updatable state.- Returns:
trueif the model will be finalized after batch training.
-
clone
public MultinomialNaiveBayes clone()
- Specified by:
clonein interfaceClassifier- Specified by:
clonein interfaceUpdateableClassifier- Specified by:
clonein classBaseUpdateableClassifier
-
train
public void train(ClassificationDataSet dataSet, boolean parallel)
Description copied from interface:ClassifierTrains the classifier and constructs a model for classification using the given data set. If the training method knows how, it will used the threadPool to conduct training in parallel. This method will block until the training has completed.- Specified by:
trainin interfaceClassifier- Overrides:
trainin classBaseUpdateableClassifier- Parameters:
dataSet- the data set to train onparallel-trueif multiple threads should be used to train the model.falseif it should be done in a single threaded manner.
-
train
public void train(ClassificationDataSet dataSet)
Description copied from interface:ClassifierTrains the classifier and constructs a model for classification using the given data set.- Specified by:
trainin interfaceClassifier- Overrides:
trainin classBaseUpdateableClassifier- Parameters:
dataSet- the data set to train on
-
finalizeModel
public void finalizeModel()
Finalizes the current model. This prevents the model from being updated further, causingupdate(jsat.classifiers.DataPoint, int)to throw an exception. This finalization reduces the cost of callingclassify(jsat.classifiers.DataPoint)
-
setUp
public void setUp(CategoricalData[] categoricalAttributes, int numericAttributes, CategoricalData predicting)
Description copied from interface:UpdateableClassifierPrepares the classifier to begin learning from itsUpdateableClassifier.update(jsat.classifiers.DataPoint, int)method.- Specified by:
setUpin interfaceUpdateableClassifier- Parameters:
categoricalAttributes- an array containing the categorical attributes that will be in each data pointnumericAttributes- the number of numeric attributes that will be in each data pointpredicting- the information for the target class that will be predicted
-
update
public void update(DataPoint dataPoint, int targetClass)
Description copied from interface:UpdateableClassifierUpdates the classifier by giving it a new data point to learn from.- Specified by:
updatein interfaceUpdateableClassifier- Parameters:
dataPoint- the data point to learntargetClass- the target class of the data point
-
classify
public CategoricalResults classify(DataPoint data)
Description copied from interface:ClassifierPerforms classification on the given data point.- Specified by:
classifyin interfaceClassifier- Parameters:
data- the data point to classify- Returns:
- the results of the classification.
-
supportsWeightedData
public boolean supportsWeightedData()
Description copied from interface:ClassifierIndicates whether the model knows how to train using weighted data points. If it does, the model will train assuming the weights. The values returned by this method may change depending on the parameters set for the model.- Specified by:
supportsWeightedDatain interfaceClassifier- Returns:
- true if the model supports weighted data, false otherwise
-
-
DataMelt 3.0 © DataMelt by jWork.ORG