simple Naive Bayes classifier

This commit is contained in:
Arkadiusz Kondas 2016-04-14 22:56:54 +02:00
parent 50fbcddfc4
commit 100205d767
2 changed files with 83 additions and 0 deletions

View File

@ -6,12 +6,24 @@ namespace Phpml\Classifier;
class NaiveBayes implements Classifier class NaiveBayes implements Classifier
{ {
/**
* @var array
*/
private $samples;
/**
* @var array
*/
private $labels;
/** /**
* @param array $samples * @param array $samples
* @param array $labels * @param array $labels
*/ */
public function train(array $samples, array $labels) public function train(array $samples, array $labels)
{ {
$this->samples = $samples;
$this->labels = $labels;
} }
/** /**
@ -21,5 +33,38 @@ class NaiveBayes implements Classifier
*/ */
public function predict(array $samples) public function predict(array $samples)
{ {
if (!is_array($samples[0])) {
$predicted = $this->predictSample($samples);
} else {
$predicted = [];
foreach ($samples as $index => $sample) {
$predicted[$index] = $this->predictSample($sample);
}
}
return $predicted;
}
/**
* @param array $sample
*
* @return mixed
*/
private function predictSample(array $sample)
{
$predictions = [];
foreach ($this->labels as $index => $label) {
$predictions[$label] = 0;
foreach ($sample as $token => $count) {
if (array_key_exists($token, $this->samples[$index])) {
$predictions[$label] += $count * $this->samples[$index][$token];
}
}
}
arsort($predictions, SORT_NUMERIC);
reset($predictions);
return key($predictions);
} }
} }

View File

@ -0,0 +1,38 @@
<?php
declare (strict_types = 1);
namespace tests\Classifier;
use Phpml\Classifier\NaiveBayes;
class NaiveBayesTest extends \PHPUnit_Framework_TestCase
{
public function testPredictSingleSample()
{
$samples = [[5, 1, 1], [1, 5, 1], [1, 1, 5]];
$labels = ['a', 'b', 'c'];
$classifier = new NaiveBayes();
$classifier->train($samples, $labels);
$this->assertEquals('a', $classifier->predict([3, 1, 1]));
$this->assertEquals('b', $classifier->predict([1, 4, 1]));
$this->assertEquals('c', $classifier->predict([1, 1, 6]));
}
public function testPredictArrayOfSamples()
{
$trainSamples = [[5, 1, 1], [1, 5, 1], [1, 1, 5]];
$trainLabels = ['a', 'b', 'c'];
$testSamples = [[3, 1, 1], [5, 1, 1], [4, 3, 8], [1, 1, 2], [2, 3, 2], [1, 2, 1], [9, 5, 1], [3, 1, 2]];
$testLabels = ['a', 'a', 'c', 'c', 'b', 'b', 'a', 'a'];
$classifier = new NaiveBayes();
$classifier->train($trainSamples, $trainLabels);
$predicted = $classifier->predict($testSamples);
$this->assertEquals($testLabels, $predicted);
}
}