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 = [
    [
12010],
    [
13012],
    [
20,  1],
    [
30,  2],
];

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

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

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

$dataset = new Unlabeled([
    [
1009],
]);

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

Result: Memory: 0.398 Mb Time running: 0.006 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).