NaiveBayesTDistribution Class |
Namespace: Accord.MachineLearning.Bayes
[SerializableAttribute] public class NaiveBayes<TDistribution> : NaiveBayes<TDistribution, double> where TDistribution : Object, IFittableDistribution<double>, IUnivariateDistribution, IUnivariateDistribution<double>
The NaiveBayesTDistribution type exposes the following members.
Name | Description | |
---|---|---|
NaiveBayesTDistribution(Int32, Int32, FuncInt32, Int32, TDistribution) |
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution) |
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution) |
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution) |
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution) |
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution, Double) | Obsolete.
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution, Double) | Obsolete.
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution, Double) | Obsolete.
Constructs a new Naïve Bayes Classifier.
| |
NaiveBayesTDistribution(Int32, Int32, TDistribution, Double) | Obsolete.
Constructs a new Naïve Bayes Classifier.
|
Name | Description | |
---|---|---|
ClassCount | Obsolete.
Obsolete.
| |
Distributions |
Gets the probability distributions for each class and input.
| |
InputCount | Obsolete.
Obsolete.
| |
NumberOfClasses |
Gets the number of classes expected and recognized by the classifier.
(Inherited from ClassifierBaseTInput, TClasses.) | |
NumberOfInputs |
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.) | |
NumberOfOutputs |
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.) | |
Priors |
Gets the prior beliefs for each class.
(Inherited from BayesTDistribution, TInput.) |
Name | Description | |
---|---|---|
Compute(Double) | Obsolete.
Obsolete.
| |
Compute(Double, Double) | Obsolete.
Obsolete.
| |
Compute(Double, Double, Double) | Obsolete.
Obsolete.
| |
Decide(TInput) |
Computes class-label decisions for a given set of input vectors.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Decide(TInput) |
Computes a class-label decision for a given input.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Decide(TInput, TClasses) |
Computes a class-label decision for a given input.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Decide(TInput, Boolean) |
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Decide(TInput, Double) |
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Decide(TInput, Int32) |
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Decide(TInput, Double) |
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Error | Obsolete.
Obsolete.
| |
Estimate(Double, Int32, Boolean) | Obsolete.
Obsolete.
| |
EstimateTOptions(Double, Int32, Boolean, TOptions) | Obsolete.
Obsolete.
| |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
LogLikelihood(TInput) |
Computes the log-likelihood that the given input
vector belongs to its most plausible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput) |
Computes the log-likelihood that the given input
vectors belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32) |
Predicts a class label vector for the given input vector, returning the
log-likelihood that the input vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Double) |
Computes the log-likelihood that the given input
vectors belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32) |
Predicts a class label for each input vector, returning the
log-likelihood that each vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from BayesTDistribution, TInput.) | |
LogLikelihood(TInput, Int32, Double) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32, Double) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihood(TInput, Int32, Double) |
Predicts a class label for each input vector, returning the
log-likelihood that each vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput) |
Computes the log-likelihood that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput) |
Computes the log-likelihood that the given input
vectors belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Double) |
Computes the log-likelihood that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32) |
Predicts a class label vector for the given input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Double) |
Computes the log-likelihood that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32) |
Predicts a class label vector for each input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32, Double) |
Predicts a class label vector for the given input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32, Double) |
Computes the log-likelihood that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
LogLikelihoods(TInput, Int32, Double) |
Predicts a class label vector for each input vector, returning the
log-likelihoods of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Probabilities(TInput) |
Computes the probabilities that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput) |
Computes the probabilities that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Double) |
Computes the probabilities that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Int32) |
Predicts a class label vector for the given input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Double) |
Computes the probabilities that the given input
vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Int32) |
Predicts a class label vector for each input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Int32, Double) |
Predicts a class label vector for the given input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probabilities(TInput, Int32, Double) |
Predicts a class label vector for each input vector, returning the
probabilities of the input vector belonging to each possible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32) |
Computes the probability that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Double) |
Predicts a class label for the given input vector, returning the
probability that the input vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32) |
Computes the probability that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32) |
Computes the probability that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32) |
Predicts a class label for each input vector, returning the
probability that each vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32, Double) |
Computes the probability that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32, Double) |
Computes the probability that the given input vector
belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Probability(TInput, Int32, Double) |
Predicts a class label for each input vector, returning the
probability that each vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Save(Stream) | Obsolete.
Saves the Naïve Bayes model to a stream.
| |
Save(String) | Obsolete.
Saves the Naïve Bayes model to a stream.
| |
Score(TInput) |
Computes a numerical score measuring the association between
the given input vector and its most strongly
associated class (as predicted by the classifier).
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput) |
Computes a numerical score measuring the association between
the given input vector and its most strongly
associated class (as predicted by the classifier).
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32) |
Computes a numerical score measuring the association between
the given input vector and a given
classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Score(TInput, Int32) |
Predicts a class label for the input vector, returning a
numerical score measuring the strength of association of the
input vector to its most strongly related class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Double) |
Computes a numerical score measuring the association between
the given input vector and its most strongly
associated class (as predicted by the classifier).
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32) |
Computes a numerical score measuring the association between
the given input vector and a given
classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32) |
Computes a numerical score measuring the association between
the given input vector and a given
classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32) |
Predicts a class label for each input vector, returning a
numerical score measuring the strength of association of the
input vector to the most strongly related class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32, Double) |
Computes a numerical score measuring the association between
the given input vector and a given
classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32, Double) |
Computes a numerical score measuring the association between
the given input vector and a given
classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Score(TInput, Int32, Double) |
Predicts a class label for each input vector, returning a
numerical score measuring the strength of association of the
input vector to the most strongly related class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput, Double) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput, Int32) |
Predicts a class label vector for the given input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput, Double) |
Computes a numerical score measuring the association between
the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput, Int32) |
Predicts a class label vector for each input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput, Int32, Double) |
Predicts a class label vector for the given input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
Scores(TInput, Int32, Double) |
Predicts a class label vector for each input vector, returning a
numerical score measuring the strength of association of the input vector
to each of the possible classes.
(Inherited from MulticlassScoreClassifierBaseTInput.) | |
ToMulticlass |
Views this instance as a multi-class generative classifier.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
ToMultilabel |
Views this instance as a multi-label generative classifier,
giving access to more advanced methods, such as the prediction
of one-hot vectors.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Transform(TInput) |
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Transform(TInput) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.) | |
Transform(TInput, TClasses) |
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Transform(TInput, Boolean) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Boolean) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(Type) | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) | |
ToT | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) |
A naive Bayes classifier is a simple probabilistic classifier based on applying Bayes' theorem with strong (naive) independence assumptions. A more descriptive term for the underlying probability model would be "independent feature model".
In simple terms, a naive Bayes classifier assumes that the presence (or absence) of a particular feature of a class is unrelated to the presence (or absence) of any other feature, given the class variable. In spite of their naive design and apparently over-simplified assumptions, naive Bayes classifiers have worked quite well in many complex real-world situations.
This class implements an arbitrary-distribution (real-valued) Naive-Bayes classifier. There is also a special named constructor to create classifiers assuming normal distributions for each variable. For a discrete (integer-valued) distribution classifier, please see NaiveBayes.
References:
This page contains two examples, one using text and another one using normal double vectors. The first example is the classic example given by Tom Mitchell. If you are not interested in text or in this particular example, please jump to the second example below.
In the first example, we will be using a mixed-continuous version of the famous Play Tennis example by Tom Mitchell (1998). In Mitchell's example, one would like to infer if a person would play tennis or not based solely on four input variables. The original variables were categorical, but in this example, two of them will be categorical and two will be continuous. The rows, or instances presented below represent days on which the behavior of the person has been registered and annotated, pretty much building our set of observation instances for learning:
// We will represent Mitchell's Tennis example using a DataTable. However, // the use of a DataTable is not required in order to use the Naive Bayes. // Please take a look at the other examples below for simpler approaches. DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); // We will set Temperature and Humidity to be continuous data.Columns["Temperature"].DataType = typeof(double); data.Columns["Humidity"].DataType = typeof(double); // Add some data data.Rows.Add("D1", "Sunny", 38.0, 96.0, "Weak", "No"); data.Rows.Add("D2", "Sunny", 39.0, 90.0, "Strong", "No"); data.Rows.Add("D3", "Overcast", 38.0, 75.0, "Weak", "Yes"); data.Rows.Add("D4", "Rain", 25.0, 87.0, "Weak", "Yes"); data.Rows.Add("D5", "Rain", 12.0, 30.0, "Weak", "Yes"); data.Rows.Add("D6", "Rain", 11.0, 35.0, "Strong", "No"); data.Rows.Add("D7", "Overcast", 10.0, 40.0, "Strong", "Yes"); data.Rows.Add("D8", "Sunny", 24.0, 90.0, "Weak", "No"); data.Rows.Add("D9", "Sunny", 12.0, 26.0, "Weak", "Yes"); data.Rows.Add("D10", "Rain", 25, 30.0, "Weak", "Yes"); data.Rows.Add("D11", "Sunny", 26.0, 40.0, "Strong", "Yes"); data.Rows.Add("D12", "Overcast", 27.0, 97.0, "Strong", "Yes"); data.Rows.Add("D13", "Overcast", 39.0, 41.0, "Weak", "Yes"); data.Rows.Add("D14", "Rain", 23.0, 98.0, "Strong", "No");
In order to estimate a discrete Naive Bayes, we will first convert this problem to a more simpler representation. Since some variables are categories, it does not matter if they are represented as strings, or numbers, since both are just symbols for the event they represent. Since numbers are more easily representable than text strings, we will convert the problem to use a discrete alphabet through the use of a codebook.
A codebook effectively transforms any distinct possible value for a variable into an integer symbol. For example, “Sunny” could as well be represented by the integer label 0, “Overcast” by “1”, Rain by “2”, and the same goes by for the other variables. So:
// Create a new codification codebook to // convert strings into discrete symbols Codification codebook = new Codification(data);
Now that we already have our learning input/output pairs, we should specify our Bayes model. We will be trying to build a model to predict the last column, entitled “PlayTennis”. For this, we will be using the “Outlook”, “Temperature”, “Humidity” and “Wind” as predictors (variables which will we will use for our decision).
// Some distributions require constructor parameters, and as such, cannot // be automatically initialized by the learning algorithm. For this reason, // we might need to specify how each component should be initialized: IUnivariateFittableDistribution[] priors = { new GeneralDiscreteDistribution(codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain) new NormalDistribution(), // Continuous value (Celsius) new NormalDistribution(), // Continuous value (percentage) new GeneralDiscreteDistribution(codebook["Wind"].Symbols) // 2 possible values (Weak, strong) }; // Create a new Naive Bayes classifiers for the two classes var learner = new NaiveBayesLearning<IUnivariateFittableDistribution>() { // Tell the learner how to initialize the distributions Distribution = (classIndex, variableIndex) => priors[variableIndex] }; // Extract symbols from data and train the classifier DataTable symbols = codebook.Apply(data); double[][] inputs = symbols.ToArray("Outlook", "Temperature", "Humidity", "Wind"); int[] outputs = symbols.ToArray<int>("PlayTennis"); // Learn the Naive Bayes model var naiveBayes = learner.Learn(inputs, outputs);
Now that we have created and estimated our classifier, we can query the classifier for new input samples through the NaiveBayes{TDistribution}.Decide(double[]) method.
// Create an instance representing a "sunny, cool, humid and windy day": double[] instance = new double[] { codebook.Translate(columnName:"Outlook", value:"Sunny"), //n 0 12.0, 90.0, codebook.Translate(columnName:"Wind", value:"Strong") // 1 }; // We can obtain a class prediction using int predicted = naiveBayes.Decide(instance); // Or compute probabilities of each class using double[] probabilities = naiveBayes.Probabilities(instance); // Or obtain the log-likelihood of prediction double ll = naiveBayes.LogLikelihood(instance); // Finally, the result can be translated back using string result = codebook.Translate("PlayTennis", predicted); // Should be "No"
In this second example, we will be creating a simple multi-class classification problem using integer vectors and learning a discrete Naive Bayes on those vectors.
// Let's say we have the following data to be classified // into three possible classes. Those are the samples: // double[][] inputs = { // input output new double[] { 0, 1, 1, 0 }, // 0 new double[] { 0, 1, 0, 0 }, // 0 new double[] { 0, 0, 1, 0 }, // 0 new double[] { 0, 1, 1, 0 }, // 0 new double[] { 0, 1, 0, 0 }, // 0 new double[] { 1, 0, 0, 0 }, // 1 new double[] { 1, 0, 0, 0 }, // 1 new double[] { 1, 0, 0, 1 }, // 1 new double[] { 0, 0, 0, 1 }, // 1 new double[] { 0, 0, 0, 1 }, // 1 new double[] { 1, 1, 1, 1 }, // 2 new double[] { 1, 0, 1, 1 }, // 2 new double[] { 1, 1, 0, 1 }, // 2 new double[] { 0, 1, 1, 1 }, // 2 new double[] { 1, 1, 1, 1 }, // 2 }; int[] outputs = // those are the class labels { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, }; // Create a new Gaussian distribution naive Bayes learner var teacher = new NaiveBayesLearning<NormalDistribution>(); // Set options for the component distributions teacher.Options.InnerOption = new NormalOptions { Regularization = 1e-5 // to avoid zero variances }; // Learn the naive Bayes model NaiveBayes<NormalDistribution> bayes = teacher.Learn(inputs, outputs); // Use the model to predict class labels int[] predicted = bayes.Decide(inputs); // Estimate the model error. The error should be zero: double error = new ZeroOneLoss(outputs).Loss(predicted); // Now, let's test the model output for the first input sample: int answer = bayes.Decide(new double[] { 1, 0, 0, 1 }); // should be 1