Package smile.classification
Class NaiveBayes
java.lang.Object
smile.classification.AbstractClassifier<double[]>
smile.classification.NaiveBayes
- All Implemented Interfaces:
Serializable
,ToDoubleFunction<double[]>
,ToIntFunction<double[]>
,Classifier<double[]>
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.
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 Distribution
classes. Although the predict(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.
References
- Christopher D. Manning, Prabhakar Raghavan, and Hinrich Schutze. Introduction to Information Retrieval, Chapter 13, 2009.
- Kevin P. Murphy. Machina Learning A Probability Perspective, Chapter 3, 2012.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface smile.classification.Classifier
Classifier.Trainer<T,
M extends Classifier<T>> -
Field Summary
Fields inherited from class smile.classification.AbstractClassifier
classes
-
Constructor Summary
ConstructorDescriptionNaiveBayes
(double[] priori, Distribution[][] condprob) Constructor of general naive Bayes classifier.NaiveBayes
(double[] priori, Distribution[][] condprob, IntSet labels) Constructor of general naive Bayes classifier. -
Method Summary
Modifier and TypeMethodDescriptionint
predict
(double[] x) Predict the class of an instance.int
predict
(double[] x, double[] posteriori) Predict the class of an instance.double[]
priori()
Returns a priori probabilities.boolean
soft()
Returns true if this is a soft classifier that can estimate the posteriori probabilities of classification.Methods inherited from class smile.classification.AbstractClassifier
classes, numClasses
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface smile.classification.Classifier
applyAsDouble, applyAsInt, online, predict, predict, predict, predict, predict, predict, score, update, update, update
-
Constructor Details
-
NaiveBayes
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
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).labels
- the class label encoder.
-
-
Method Details
-
priori
public double[] priori()Returns a priori probabilities.- Returns:
- a priori probabilities.
-
predict
public int predict(double[] x) Predict the class of an instance.- Parameters:
x
- the instance to be classified.- Returns:
- the predicted class label.
-
soft
public boolean soft()Description copied from interface:Classifier
Returns true if this is a soft classifier that can estimate the posteriori probabilities of classification.- Returns:
- true if soft classifier.
-
predict
public int predict(double[] 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.
-