---
name: "mlpack_naive_bayes_NaiveBayesClassifier"
section: "3"
platform: "linux"
package: "mlpack-doc"
version: "1.0.10-1"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/mlpack_naive_bayes_NaiveBayesClassifier"
---

# mlpack\_naive\_bayes\_NaiveBayesClassifier(3)

The simple Naive Bayes classifier.

## NAME

mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \> -

The simple Naive Bayes classifier.

## SYNOPSIS

### Public Member Functions

**NaiveBayesClassifier** (const MatType &data, const arma::Col\< 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.*  void **Classify** (const MatType &data, arma::Col\< size\_t \> &results)\
 *Given a bunch of data points, this function evaluates the class of each of those data points, and puts it in the vector 'results'.*  const MatType & **Means** () const \
 *Get the sample means for each class.*  MatType & **Means** ()\
 *Modify the sample means for each class.*  const arma::vec & **Probabilities** () const \
 *Get the prior probabilities for each class.*  arma::vec & **Probabilities** ()\
 *Modify the prior probabilities for each class.*  const MatType & **Variances** () const \
 *Get the sample variances for each class.*  MatType & **Variances** ()\
 *Modify the sample variances for each class.*

### Private Attributes

MatType **means**\
 *Sample mean for each class.*  arma::vec **probabilities**\
 *Class probabilities.*  MatType **variances**\
 *Sample variances for each class.*

## 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 classs 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 58 of file naive\_bayes\_classifier.hpp.

## Constructor & Destructor Documentation

### template\<typename MatType = arma::mat\> mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::NaiveBayesClassifier (const MatType &data, const arma::Col\< size\_t \> &labels, const size\_tclasses, const boolincrementalVariance = `false`)

Initializes the classifier as per the input and then trains it by calculating the sample mean and variances. The input data is expected to have integer labels as the last row (starting with 0 and not greater than the number of classes).

Example use:

```
extern arma::mat training_data, testing_data;
extern arma::Col<size_t> labels;
NaiveBayesClassifier nbc(training_data, labels, 5);
```

**Parameters:**

*data* Training data points. \
 *labels* Labels corresponding to training data points. \
 *classes* Number of classes in this classifier. \
 *incrementalVariance* If 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.

## Member Function Documentation

### template\<typename MatType = arma::mat\> void mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Classify (const MatType &data, arma::Col\< size\_t \> &results)

Given a bunch of data points, this function evaluates the class of each of those data points, and puts it in the vector 'results'.

```
arma::mat test_data; // each column is a test point
arma::Col<size_t> results;
...
nbc.Classify(test_data, &results);
```

**Parameters:**

*data* List of data points. \
 *results* Vector that class predictions will be placed into.

### 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 113 of file naive\_bayes\_classifier.hpp.

References mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::means.

### template\<typename MatType = arma::mat\> MatType& mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Means ()` [inline]`

Modify the sample means for each class.

Definition at line 115 of file naive\_bayes\_classifier.hpp.

References mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::means.

### 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 123 of file naive\_bayes\_classifier.hpp.

References mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::probabilities.

### template\<typename MatType = arma::mat\> arma::vec& mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Probabilities ()` [inline]`

Modify the prior probabilities for each class.

Definition at line 125 of file naive\_bayes\_classifier.hpp.

References mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::probabilities.

### 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 118 of file naive\_bayes\_classifier.hpp.

References mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::variances.

### template\<typename MatType = arma::mat\> MatType& mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Variances ()` [inline]`

Modify the sample variances for each class.

Definition at line 120 of file naive\_bayes\_classifier.hpp.

References mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::variances.

## Member Data Documentation

### template\<typename MatType = arma::mat\> MatType mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::means` [private]`

Sample mean for each class.

Definition at line 62 of file naive\_bayes\_classifier.hpp.

Referenced by mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Means().

### template\<typename MatType = arma::mat\> arma::vec mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::probabilities` [private]`

Class probabilities.

Definition at line 68 of file naive\_bayes\_classifier.hpp.

Referenced by mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Probabilities().

### template\<typename MatType = arma::mat\> MatType mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::variances` [private]`

Sample variances for each class.

Definition at line 65 of file naive\_bayes\_classifier.hpp.

Referenced by mlpack::naive\_bayes::NaiveBayesClassifier\< MatType \>::Variances().

## Author

Generated automatically by Doxygen for MLPACK from the source code.
