Click or drag to resize
Accord.NET (logo)

NaiveBayesLearningTDistribution, TOptions, TInput Class

Naïve Bayes learning algorithm.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearning.BayesNaiveBayesLearningBaseNaiveBayesTDistribution, TInput, TDistribution, TInput, IndependentOptionsTOptions
    Accord.MachineLearning.BayesNaiveBayesLearningTDistribution, TOptions, TInput

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

Type Parameters

TDistribution
TOptions
TInput

The NaiveBayesLearningTDistribution, TOptions, TInput type exposes the following members.

Constructors
  NameDescription
Public methodNaiveBayesLearningTDistribution, TOptions, TInput
Initializes a new instance of the NaiveBayesLearningTDistribution, TOptions, TInput class
Top
Properties
  NameDescription
Public propertyDistribution
Gets or sets the distribution creation function. This function can be used to specify how the initial distributions of the model should be created. By default, this function attempts to call the empty constructor of the distribution using Activator.CreateInstance().
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public propertyEmpirical
Gets or sets whether the class priors should be estimated from the data.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public propertyModel
Gets or sets the model being learned.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public propertyOptions
Gets or sets the fitting options to use when estimating the class-specific distributions.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public propertyParallelOptions
Gets or sets the parallelization options for this algorithm.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Top
Methods
  NameDescription
Protected methodCreate
Creates an instance of the model to be learned.
(Overrides NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptionsCreate(TInput, Int32).)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
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.)
Protected methodFit
Fits one of the distributions in the naive bayes model.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn(TInput, Double, Double)
Learns a model that can map the given inputs to the given outputs.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public methodLearn(TInput, Int32, Double)
Learns a model that can map the given inputs to the given outputs.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Public methodLearn(TInput, Int32, Double)
Learns a model that can map the given inputs to the given outputs.
(Inherited from NaiveBayesLearningBaseTModel, TDistribution, TInput, TOptions.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
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
Examples

For basic examples on how to learn a Naive Bayes algorithm, please see NaiveBayes page. The following examples show how to set more specialized learning settings for Normal (Gaussian) models.

// 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, NormalOptions>();

// Set options for the component distributions
teacher.Options.InnerOption.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