Class NaiveBayes
- java.lang.Object
-
- smile.classification.NaiveBayes
-
- All Implemented Interfaces:
- java.io.Serializable, Classifier<double[]>, OnlineClassifier<double[]>, SoftClassifier<double[]>
public class NaiveBayes extends java.lang.Object implements OnlineClassifier<double[]>, SoftClassifier<double[]>, java.io.Serializable
Naive Bayes classifier. A naive Bayes classifier is a simple probabilistic classifier based on applying Bayes' theorem with strong (naive) independence assumptions. Depending on the precise nature of the probability model, naive Bayes classifiers can be trained very efficiently in a supervised learning setting.In spite of their naive design and apparently over-simplified assumptions, naive Bayes classifiers have worked quite well in many complex real-world situations and are very popular in Natural Language Processing (NLP).
For a general purpose naive Bayes classifier without any assumptions about the underlying distribution of each variable, we don't provide a learning method to infer the variable distributions from the training data. Instead, the users can fit any appropriate distributions on the data by themselves with various
Distributionclasses. Although thepredict(double[])method takes an array of double values as a general form of independent variables, the users are free to use any discrete distributions to model categorical or ordinal random variables.For document classification in NLP, there are two different ways we can set up an naive Bayes classifier: multinomial model and Bernoulli model. The multinomial model generates one term from the vocabulary in each position of the document. The multivariate Bernoulli model or Bernoulli model generates an indicator for each term of the vocabulary, either indicating presence of the term in the document or indicating absence. Of the two models, the Bernoulli model is particularly sensitive to noise features. A Bernoulli naive Bayes classifier requires some form of feature selection or else its accuracy will be low.
The different generation models imply different estimation strategies and different classification rules. The Bernoulli model estimates as the fraction of documents of class that contain term. In contrast, the multinomial model estimates as the fraction of tokens or fraction of positions in documents of class that contain term. When classifying a test document, the Bernoulli model uses binary occurrence information, ignoring the number of occurrences, whereas the multinomial model keeps track of multiple occurrences. As a result, the Bernoulli model typically makes many mistakes when classifying long documents. However, it was reported that the Bernoulli model works better in sentiment analysis.
The models also differ in how non-occurring terms are used in classification. They do not affect the classification decision in the multinomial model; but in the Bernoulli model the probability of nonoccurrence is factored in when computing. This is because only the Bernoulli model models absence of terms explicitly.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classNaiveBayes.ModelThe generation models of naive Bayes classifier.static classNaiveBayes.TrainerTrainer for naive Bayes classifier for document classification.
-
Constructor Summary
Constructors Constructor and Description NaiveBayes(double[] priori, Distribution[][] condprob)Constructor of general naive Bayes classifier.NaiveBayes(NaiveBayes.Model model, double[] priori, int p)Constructor of naive Bayes classifier for document classification.NaiveBayes(NaiveBayes.Model model, double[] priori, int p, double sigma)Constructor of naive Bayes classifier for document classification.NaiveBayes(NaiveBayes.Model model, int k, int p)Constructor of naive Bayes classifier for document classification.NaiveBayes(NaiveBayes.Model model, int k, int p, double sigma)Constructor of naive Bayes classifier for document classification.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description double[]getPriori()Returns a priori probabilities.voidlearn(double[][] x, int[] y)Online learning of naive Bayes classifier on sequences, which are modeled as a bag of words.voidlearn(double[] x, int y)Online learning of naive Bayes classifier on a sequence, which is modeled as a bag of words.voidlearn(SparseArray x, int y)Online learning of naive Bayes classifier on a sequence, which is modeled as a bag of words.intpredict(double[] x)Predict the class of an instance.intpredict(double[] x, double[] posteriori)Predict the class of an instance.intpredict(SparseArray x)Predict the class of an instance.intpredict(SparseArray x, double[] posteriori)Predict the class of an instance.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface smile.classification.Classifier
predict
-
-
-
-
Constructor Detail
-
NaiveBayes
public NaiveBayes(double[] priori, Distribution[][] condprob)Constructor of general naive Bayes classifier.- Parameters:
priori- the priori probability of each class.condprob- the conditional distribution of each variable in each class. In particular, condprob[i][j] is the conditional distribution P(xj | class i).
-
NaiveBayes
public NaiveBayes(NaiveBayes.Model model, int k, int p)
Constructor of naive Bayes classifier for document classification. The priori probability of each class will be learned from data. By default, we use add-one/Laplace smoothing, which simply adds one to each count to eliminate zeros. Add-one smoothing can be interpreted as a uniform prior (each term occurs once for each class) that is then updated as evidence from the training data comes in.- Parameters:
model- the generation model of naive Bayes classifier.k- the number of classes.p- the dimensionality of input space.
-
NaiveBayes
public NaiveBayes(NaiveBayes.Model model, int k, int p, double sigma)
Constructor of naive Bayes classifier for document classification. The priori probability of each class will be learned from data. Add-k smoothing.- Parameters:
model- the generation model of naive Bayes classifier.k- the number of classes.p- the dimensionality of input space.sigma- the prior count of add-k smoothing of evidence.
-
NaiveBayes
public NaiveBayes(NaiveBayes.Model model, double[] priori, int p)
Constructor of naive Bayes classifier for document classification. By default, we use add-one/Laplace smoothing, which simply adds one to each count to eliminate zeros. Add-one smoothing can be interpreted as a uniform prior (each term occurs once for each class) that is then updated as evidence from the training data comes in.- Parameters:
model- the generation model of naive Bayes classifier.priori- the priori probability of each class.p- the dimensionality of input space.
-
NaiveBayes
public NaiveBayes(NaiveBayes.Model model, double[] priori, int p, double sigma)
Constructor of naive Bayes classifier for document classification. Add-k smoothing.- Parameters:
model- the generation model of naive Bayes classifier.priori- the priori probability of each class.p- the dimensionality of input space.sigma- the prior count of add-k smoothing of evidence.
-
-
Method Detail
-
getPriori
public double[] getPriori()
Returns a priori probabilities.
-
learn
public void learn(double[] x, int y)Online learning of naive Bayes classifier on a sequence, which is modeled as a bag of words. Note that this method is NOT applicable for naive Bayes classifier with general generation model.- Specified by:
learnin interfaceOnlineClassifier<double[]>- Parameters:
x- training instance.y- training label in [0, k), where k is the number of classes.
-
learn
public void learn(SparseArray x, int y)
Online learning of naive Bayes classifier on a sequence, which is modeled as a bag of words. Note that this method is NOT applicable for naive Bayes classifier with general generation model.- Parameters:
x- training instance in sparse format.y- training label in [0, k), where k is the number of classes.
-
learn
public void learn(double[][] x, int[] y)Online learning of naive Bayes classifier on sequences, which are modeled as a bag of words. Note that this method is NOT applicable for naive Bayes classifier with general generation model.- Parameters:
x- training instances.y- training labels in [0, k), where k is the number of classes.
-
predict
public int predict(double[] x)
Predict the class of an instance.- Specified by:
predictin interfaceClassifier<double[]>- Parameters:
x- the instance to be classified.- Returns:
- the predicted class label. For MULTINOMIAL and BERNOULLI models, returns -1 if the instance does not contain any feature words.
-
predict
public int predict(double[] x, double[] posteriori)Predict the class of an instance.- Specified by:
predictin interfaceSoftClassifier<double[]>- Parameters:
x- the instance to be classified.posteriori- the array to store a posteriori probabilities on output.- Returns:
- the predicted class label. For MULTINOMIAL and BERNOULLI models, returns -1 if the instance does not contain any feature words.
-
predict
public int predict(SparseArray x)
Predict the class of an instance.- Parameters:
x- the instance to be classified.- Returns:
- the predicted class label. For MULTINOMIAL and BERNOULLI models, returns -1 if the instance does not contain any feature words.
-
predict
public int predict(SparseArray x, double[] posteriori)
Predict the class of an instance.- Parameters:
x- the instance to be classified.posteriori- the array to store a posteriori probabilities on output.- Returns:
- the predicted class label. For MULTINOMIAL and BERNOULLI models, returns -1 if the instance does not contain any feature words.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG