Why Naive Bayes works
MNIST: probabilistic digit classification (Naive Bayes)
Implementation in pure PHP
Run the pure PHP implementation to see how the score for each class is assembled from the prior and feature-wise Gaussian likelihoods.
Example of use
<?php
use app\classes\GaussianNB;
use app\classes\MnistLoader;
try {
[$trainSamples, $trainLabels] = MnistLoader::load('train.csv', normalize: true);
[$testSamples, $testLabels] = MnistLoader::load('test.csv', normalize: true);
} catch (Exception $e) {
echo '<div class="alert alert-danger" role="alert">' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8') . '</div>';
exit;
}
// Initialize and train the Gaussian Naive Bayes classifier
$model = new GaussianNB();
$model->train($trainSamples, $trainLabels);
// Calculate model accuracy
$score = $model->score($testSamples, $testLabels);
echo 'Train samples handled: ' . number_format(count($trainSamples)) . PHP_EOL;
echo 'Test samples handled: ' . number_format(count($testSamples)) . PHP_EOL . PHP_EOL;
echo 'Accuracy: ' . round($score * 100, 2) . '%';
Sample of digit: 0
Predicted digit: 0
Sample of digit: 1
Predicted digit: 1
Result:
Memory: 0 Mb
Time running: < 0.001 sec.
Train samples handled: 12,666
Test samples handled: 2,116
Accuracy: 98.58%
The printed values are log-scores for each class. The larger the value, the more likely the class. After sorting, the first key is the predicted digit class.