Why Naive Bayes works

Case 3. Numeric features (Gaussian Naive Bayes)


Implementation with RubixML

Here we train RubixML GaussianNB on the same samples and predict the label for a new numeric vector.

 
<?php

use Rubix\ML\Classifiers\GaussianNB;
use 
Rubix\ML\Datasets\Labeled;
use 
Rubix\ML\Datasets\Unlabeled;

$samples = [
    [
120, 10],
    [
130, 12],
    [
20,  1],
    [
30,  2],
];

$labels = ['active', 'active', 'inactive', 'inactive'];

$dataset = new Labeled($samples, $labels);

$model = new GaussianNB();
$model->train($dataset);

$dataset = new Unlabeled([
    [
100, 9],
]);

$prediction = $model->predict($dataset);
print_r($prediction);

Result: Memory: 0.394 Mb Time running: 0.007 sec.
Array
(
    [0] => active
)

RubixML returns the predicted label for the given sample. Conceptually it is the same Naive Bayes scheme: class prior × Gaussian feature likelihoods (computed internally by the library).