mlpack: mlpack::naive_bayes::NaiveBayesClassifier< MatType > Class Template Reference
mlpack  master
mlpack::naive_bayes::NaiveBayesClassifier< MatType > Class Template Reference

The simple Naive Bayes classifier. More...

Public Member Functions

 NaiveBayesClassifier (const MatType &data, const arma::Row< size_t > &labels, const size_t classes, const bool incrementalVariance=false)
 Initializes the classifier as per the input and then trains it by calculating the sample mean and variances. More...

 
 NaiveBayesClassifier (const size_t dimensionality=0, const size_t classes=0)
 Initialize the Naive Bayes classifier without performing training. More...

 
template
<
typename
VecType
>
size_t Classify (const VecType &point) const
 Classify the given point, using the training GaussianNB model. More...

 
template
<
typename
VecType
>
void Classify (const VecType &point, size_t &prediction, arma::vec &probabilities) const
 Classify the given point using the training GaussianNB model and also return estimates of the probability for each class in the given vector. More...

 
void Classify (const MatType &data, arma::Row< size_t > &predictions) const
 Classify the given points using the training GaussianNB model. More...

 
void Classify (const MatType &data, arma::Row< size_t > &predictions, arma::mat &probabilities) const
 Classify the given points using the training GaussianNB model and also return estimates of the probabilities for each class in the given matrix. More...

 
const MatType & Means () const
 Get the sample means for each class. More...

 
MatType & Means ()
 Modify the sample means for each class. More...

 
const arma::vec & Probabilities () const
 Get the prior probabilities for each class. More...

 
arma::vec & Probabilities ()
 Modify the prior probabilities for each class. More...

 
template
<
typename
Archive
>
void Serialize (Archive &ar, const unsigned int)
 Serialize the classifier. More...

 
void Train (const MatType &data, const arma::Row< size_t > &labels, const bool incremental=true)
 Train the Naive Bayes classifier on the given dataset. More...

 
template
<
typename
VecType
>
void Train (const VecType &point, const size_t label)
 Train the Naive Bayes classifier on the given point. More...

 
const MatType & Variances () const
 Get the sample variances for each class. More...

 
MatType & Variances ()
 Modify the sample variances for each class. More...

 

Private Member Functions

template
<
typename
VecType
>
void LogLikelihood (const VecType &point, arma::vec &logLikelihoods) const
 Compute the unnormalized posterior log probability (log likelihood) of given point. More...

 
void LogLikelihood (const MatType &data, arma::mat &logLikelihoods) const
 Compute the unnormalized posterior log probability of given points (log likelihood). More...

 

Private Attributes

MatType means
 Sample mean for each class. More...

 
arma::vec probabilities
 Class probabilities. More...

 
size_t trainingPoints
 Number of training points seen so far. More...

 
MatType variances
 Sample variances for each class. More...

 

Detailed Description


template
<
typename
MatType
=
arma::mat
>

class mlpack::naive_bayes::NaiveBayesClassifier< MatType >

The simple Naive Bayes classifier.

This class trains on the data by calculating the sample mean and variance of the features with respect to each of the labels, and also the class probabilities. The class labels are assumed to be positive integers (starting with 0), and are expected to be the last row of the data input to the constructor.

Mathematically, it computes P(X_i = x_i | Y = y_j) for each feature X_i for each of the labels y_j. Alongwith this, it also computes the class probabilities P(Y = y_j).

For classifying a data point (x_1, x_2, ..., x_n), it computes the following: arg max_y(P(Y = y)*P(X_1 = x_1 | Y = y) * ... * P(X_n = x_n | Y = y))

Example use:

extern arma::mat training_data, testing_data;
NaiveBayesClassifier<> nbc(training_data, 5);
arma::vec results;
nbc.Classify(testing_data, results);

Definition at line 48 of file naive_bayes_classifier.hpp.

Constructor & Destructor Documentation

◆ NaiveBayesClassifier() [1/2]

template
<
typename
MatType
=
arma::mat
>
mlpack::naive_bayes::NaiveBayesClassifier< MatType >::NaiveBayesClassifier ( const MatType &  data,
const arma::Row< size_t > &  labels,
const size_t  classes,
const bool  incrementalVariance = false 
)

Initializes the classifier as per the input and then trains it by calculating the sample mean and variances.

Example use:

extern arma::mat training_data, testing_data;
extern arma::Row<size_t> labels;
NaiveBayesClassifier nbc(training_data, labels, 5);
Parameters
dataTraining data points.
labelsLabels corresponding to training data points.
classesNumber of classes in this classifier.
incrementalVarianceIf true, an incremental algorithm is used to calculate the variance; this can prevent loss of precision in some cases, but will be somewhat slower to calculate.

◆ NaiveBayesClassifier() [2/2]

template
<
typename
MatType
=
arma::mat
>
mlpack::naive_bayes::NaiveBayesClassifier< MatType >::NaiveBayesClassifier ( const size_t  dimensionality = 0,
const size_t  classes = 0 
)

Initialize the Naive Bayes classifier without performing training.

All of the parameters of the model will be initialized to zero. Be sure to use Train() before calling Classify(), otherwise the results may be meaningless.

Member Function Documentation

◆ Classify() [1/4]

template
<
typename
MatType
=
arma::mat
>
template
<
typename
VecType
>
size_t mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Classify ( const VecType &  point) const

Classify the given point, using the training GaussianNB model.

The predicted label is returned.

Parameters
pointPoint to classify.

◆ Classify() [2/4]

template
<
typename
MatType
=
arma::mat
>
template
<
typename
VecType
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Classify ( const VecType &  point,
size_t &  prediction,
arma::vec &  probabilities 
) const

Classify the given point using the training GaussianNB model and also return estimates of the probability for each class in the given vector.

Parameters
pointPoint to classify.
predictionThis will be set to the predicted class of the point.
probabilitiesThis will be filled with class probabilities for the point.

◆ Classify() [3/4]

template
<
typename
MatType
=
arma::mat
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Classify ( const MatType &  data,
arma::Row< size_t > &  predictions 
) const

Classify the given points using the training GaussianNB model.

The predicted labels for each point are stored in the given vector.

arma::mat test_data; // each column is a test point
arma::Row<size_t> results;
...
nbc.Classify(test_data, results);
Parameters
dataList of data points.
predictionsVector that class predictions will be placed into.

◆ Classify() [4/4]

template
<
typename
MatType
=
arma::mat
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Classify ( const MatType &  data,
arma::Row< size_t > &  predictions,
arma::mat &  probabilities 
) const

Classify the given points using the training GaussianNB model and also return estimates of the probabilities for each class in the given matrix.

The predicted labels for each point are stored in the given vector.

arma::mat test_data; // each column is a test point
arma::Row<size_t> results;
arma::mat resultsProbs;
...
nbc.Classify(test_data, results, resultsProbs);
Parameters
dataSet of points to classify.
predictionsThis will be filled with predictions for each point.
probabilitiesThis will be filled with class probabilities for each point. Each row represents a point.

◆ LogLikelihood() [1/2]

template
<
typename
MatType
=
arma::mat
>
template
<
typename
VecType
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::LogLikelihood ( const VecType &  point,
arma::vec &  logLikelihoods 
) const
private

Compute the unnormalized posterior log probability (log likelihood) of given point.

Parameters
pointData point to compute posterior log probability of.
logLikelihoodsVector to store log likelihoods in.

◆ LogLikelihood() [2/2]

template
<
typename
MatType
=
arma::mat
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::LogLikelihood ( const MatType &  data,
arma::mat &  logLikelihoods 
) const
private

Compute the unnormalized posterior log probability of given points (log likelihood).

Results are returned as arma::mat, and each column represents a point, each row represents log likelihood of a class.

Parameters
dataSet of points to compute posterior log probability for.
logLikelihoodsMatrix to store log likelihoods in.

◆ Means() [1/2]

template
<
typename
MatType
=
arma::mat
>
const MatType& mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Means ( ) const
inline

Get the sample means for each class.

Definition at line 177 of file naive_bayes_classifier.hpp.

References mlpack::naive_bayes::NaiveBayesClassifier< MatType >::means.

◆ Means() [2/2]

template
<
typename
MatType
=
arma::mat
>
MatType& mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Means ( )
inline

Modify the sample means for each class.

Definition at line 179 of file naive_bayes_classifier.hpp.

References mlpack::naive_bayes::NaiveBayesClassifier< MatType >::means.

◆ Probabilities() [1/2]

template
<
typename
MatType
=
arma::mat
>
const arma::vec& mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Probabilities ( ) const
inline

Get the prior probabilities for each class.

Definition at line 187 of file naive_bayes_classifier.hpp.

References mlpack::naive_bayes::NaiveBayesClassifier< MatType >::probabilities.

◆ Probabilities() [2/2]

template
<
typename
MatType
=
arma::mat
>
arma::vec& mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Probabilities ( )
inline

◆ Serialize()

template
<
typename
MatType
=
arma::mat
>
template
<
typename
Archive
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Serialize ( Archive &  ar,
const unsigned  int 
)

◆ Train() [1/2]

template
<
typename
MatType
=
arma::mat
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Train ( const MatType &  data,
const arma::Row< size_t > &  labels,
const bool  incremental = true 
)

Train the Naive Bayes classifier on the given dataset.

If the incremental algorithm is used, the current model is used as a starting point (this is the default). If the incremental algorithm is not used, then the current model is ignored and the new model will be trained only on the given data. Note that even if the incremental algorithm is not used, the data must have the same dimensionality and number of classes that the model was initialized with. If you want to change the dimensionality or number of classes, either re-initialize or call Means(), Variances(), and Probabilities() individually to set them to the right size.

Parameters
dataThe dataset to train on.
incrementalWhether or not to use the incremental algorithm for training.

◆ Train() [2/2]

template
<
typename
MatType
=
arma::mat
>
template
<
typename
VecType
>
void mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Train ( const VecType &  point,
const size_t  label 
)

Train the Naive Bayes classifier on the given point.

This will use the incremental algorithm for updating the model parameters. The data must be the same dimensionality as the existing model parameters.

Parameters
pointData point to train on.
labelLabel of data point.

◆ Variances() [1/2]

template
<
typename
MatType
=
arma::mat
>
const MatType& mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Variances ( ) const
inline

Get the sample variances for each class.

Definition at line 182 of file naive_bayes_classifier.hpp.

References mlpack::naive_bayes::NaiveBayesClassifier< MatType >::variances.

◆ Variances() [2/2]

template
<
typename
MatType
=
arma::mat
>
MatType& mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Variances ( )
inline

Modify the sample variances for each class.

Definition at line 184 of file naive_bayes_classifier.hpp.

References mlpack::naive_bayes::NaiveBayesClassifier< MatType >::variances.

Member Data Documentation

◆ means

template
<
typename
MatType
=
arma::mat
>
MatType mlpack::naive_bayes::NaiveBayesClassifier< MatType >::means
private

Sample mean for each class.

Definition at line 197 of file naive_bayes_classifier.hpp.

Referenced by mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Means().

◆ probabilities

template
<
typename
MatType
=
arma::mat
>
arma::vec mlpack::naive_bayes::NaiveBayesClassifier< MatType >::probabilities
private

Class probabilities.

Definition at line 201 of file naive_bayes_classifier.hpp.

Referenced by mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Probabilities().

◆ trainingPoints

template
<
typename
MatType
=
arma::mat
>
size_t mlpack::naive_bayes::NaiveBayesClassifier< MatType >::trainingPoints
private

Number of training points seen so far.

Definition at line 203 of file naive_bayes_classifier.hpp.

◆ variances

template
<
typename
MatType
=
arma::mat
>
MatType mlpack::naive_bayes::NaiveBayesClassifier< MatType >::variances
private

Sample variances for each class.

Definition at line 199 of file naive_bayes_classifier.hpp.

Referenced by mlpack::naive_bayes::NaiveBayesClassifier< MatType >::Variances().


The documentation for this class was generated from the following file: