Why Naive Bayes works
Case 1. Categorical features and frequencies
Implementation with RubixML
Here the same case is implemented with RubixML: we train Naive Bayes on the same data and see which class the model assigns to a new sample.
Example of use
<?php
use Rubix\ML\Datasets\Unlabeled;
include __DIR__ . '/rubix-code.php';
// New sample to classify (must use the same categorical encoding as during training).
$sample = ['from_ads', 'has_account'];
// RubixML expects samples to be wrapped into a Dataset object.
$dataset = new Unlabeled([$sample]);
// Predict class label.
$prediction = $model->predict($dataset);
// Print the predicted label.
print_r($prediction);
Result:
Memory: 0.366 Mb
Time running: 0.009 sec.
Array
(
[0] => buyer
)
RubixML returns the predicted class label for the given sample. Under the hood it applies the same Naive Bayes idea (class prior × conditional likelihoods), so the result should match the pure PHP implementation for the same data.