Click or drag to resize
Accord.NET (logo)

NaiveBayesTDistribution Class

Naïve Bayes Classifier for arbitrary distributions.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Int32
    Accord.MachineLearningClassifierBaseDouble, Int32
      Accord.MachineLearningMulticlassClassifierBaseDouble
        Accord.MachineLearningMulticlassScoreClassifierBaseDouble
          Accord.MachineLearningMulticlassLikelihoodClassifierBaseDouble
            Accord.MachineLearning.BayesBayesIndependentTDistribution, Double, Double
              Accord.MachineLearning.BayesNaiveBayesTDistribution, Double
                Accord.MachineLearning.BayesNaiveBayesTDistribution

Namespace:  Accord.MachineLearning.Bayes
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class NaiveBayes<TDistribution> : NaiveBayes<TDistribution, double>
where TDistribution : Object, IFittableDistribution<double>, IUnivariateDistribution, IUnivariateDistribution<double>
Request Example View Source

Type Parameters

TDistribution

The NaiveBayesTDistribution type exposes the following members.

Constructors
  NameDescription
Public methodNaiveBayesTDistribution(Int32, Int32, FuncInt32, Int32, TDistribution)
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution)
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution)
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution)
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution)
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution, Double) Obsolete.
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution, Double) Obsolete.
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution, Double) Obsolete.
Constructs a new Naïve Bayes Classifier.
Public methodNaiveBayesTDistribution(Int32, Int32, TDistribution, Double) Obsolete.
Constructs a new Naïve Bayes Classifier.
Top
Properties
Methods
  NameDescription
Public methodCompute(Double) Obsolete.
Obsolete.
Public methodCompute(Double, Double) Obsolete.
Obsolete.
Public methodCompute(Double, Double, Double) Obsolete.
Obsolete.
Public methodDecide(TInput)
Computes class-label decisions for a given set of input vectors.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodDecide(TInput)
Computes a class-label decision for a given input.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodDecide(TInput, TClasses)
Computes a class-label decision for a given input.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodDecide(TInput, Boolean)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodDecide(TInput, Double)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodDecide(TInput, Int32)
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodDecide(TInput, Double)
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodError Obsolete.
Obsolete.
Public methodEstimate(Double, Int32, Boolean) Obsolete.
Obsolete.
Public methodEstimateTOptions(Double, Int32, Boolean, TOptions) Obsolete.
Obsolete.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLogLikelihood(TInput)
Computes the log-likelihood that the given input vector belongs to its most plausible class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput)
Computes the log-likelihood that the given input vectors belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(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.)
Public methodLogLikelihood(TInput, Double)
Computes the log-likelihood that the given input vectors belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput, Int32)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput, Int32)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(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.)
Public methodLogLikelihood(TInput, Int32)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from BayesTDistribution, TInput.)
Public methodLogLikelihood(TInput, Int32, Double)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput, Int32, Double)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(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.)
Public methodLogLikelihoods(TInput)
Computes the log-likelihood that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput)
Computes the log-likelihood that the given input vectors belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Double)
Computes the log-likelihood that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(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.)
Public methodLogLikelihoods(TInput, Double)
Computes the log-likelihood that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Int32)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(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.)
Public methodLogLikelihoods(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.)
Public methodLogLikelihoods(TInput, Int32, Double)
Computes the log-likelihood that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(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.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodProbabilities(TInput)
Computes the probabilities that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput)
Computes the probabilities that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Double)
Computes the probabilities that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbabilities(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.)
Public methodProbabilities(TInput, Double)
Computes the probabilities that the given input vector belongs to each of the possible classes.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbabilities(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.)
Public methodProbabilities(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.)
Public methodProbabilities(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.)
Public methodProbability(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.)
Public methodProbability(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.)
Public methodProbability(TInput, Int32)
Computes the probability that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbability(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.)
Public methodProbability(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.)
Public methodProbability(TInput, Int32)
Computes the probability that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Int32)
Computes the probability that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Int32)
Predicts a class label for each input vector, returning the probability that each vector belongs to its predicted class.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Int32, Double)
Computes the probability that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Int32, Double)
Computes the probability that the given input vector belongs to the specified classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodProbability(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.)
Public methodSave(Stream) Obsolete.
Saves the Naïve Bayes model to a stream.
Public methodSave(String) Obsolete.
Saves the Naïve Bayes model to a stream.
Public methodScore(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.)
Public methodScore(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.)
Public methodScore(TInput, Int32)
Computes a numerical score measuring the association between the given input vector and a given classIndex.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodScore(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.)
Public methodScore(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.)
Public methodScore(TInput, Int32)
Computes a numerical score measuring the association between the given input vector and a given classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScore(TInput, Int32)
Computes a numerical score measuring the association between the given input vector and a given classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScore(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.)
Public methodScore(TInput, Int32, Double)
Computes a numerical score measuring the association between the given input vector and a given classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScore(TInput, Int32, Double)
Computes a numerical score measuring the association between the given input vector and a given classIndex.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScore(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.)
Public methodScores(TInput)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScores(TInput)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScores(TInput, Double)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScores(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.)
Public methodScores(TInput, Double)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from MulticlassScoreClassifierBaseTInput.)
Public methodScores(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.)
Public methodScores(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.)
Public methodScores(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.)
Public methodToMulticlass
Views this instance as a multi-class generative classifier.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodToMultilabel
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.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTransform(TInput)
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodTransform(TInput)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.)
Public methodTransform(TInput, TClasses)
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodTransform(TInput, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassLikelihoodClassifierBaseTInput.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(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.)
Public Extension MethodToTOverloaded.
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.)
Top
Remarks

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:

  • Wikipedia contributors. "Naive Bayes classifier." Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 16 Dec. 2011. Web. 5 Jan. 2012.

Examples

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
See Also