mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-25 06:17:34 +00:00
integration tests for knn classifier
This commit is contained in:
parent
e7d2780150
commit
62ec4ec2f2
@ -50,7 +50,7 @@ class KNearestNeighbors implements Classifier
|
|||||||
*/
|
*/
|
||||||
public function predict(array $samples)
|
public function predict(array $samples)
|
||||||
{
|
{
|
||||||
if(!is_array($samples[0])) {
|
if (!is_array($samples[0])) {
|
||||||
$predicted = $this->predictSample($samples);
|
$predicted = $this->predictSample($samples);
|
||||||
} else {
|
} else {
|
||||||
$predicted = [];
|
$predicted = [];
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace Phpml\Metric;
|
namespace Phpml\Metric;
|
||||||
|
|
||||||
@ -7,7 +8,6 @@ use Phpml\Exception\InvalidArgumentException;
|
|||||||
|
|
||||||
class Accuracy
|
class Accuracy
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $actualLabels
|
* @param array $actualLabels
|
||||||
* @param array $predictedLabels
|
* @param array $predictedLabels
|
||||||
@ -25,12 +25,12 @@ class Accuracy
|
|||||||
|
|
||||||
$score = 0;
|
$score = 0;
|
||||||
foreach ($actualLabels as $index => $label) {
|
foreach ($actualLabels as $index => $label) {
|
||||||
if($label===$predictedLabels[$index]) {
|
if ($label === $predictedLabels[$index]) {
|
||||||
$score++;
|
++$score;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($normalize) {
|
if ($normalize) {
|
||||||
$score = $score / count($actualLabels);
|
$score = $score / count($actualLabels);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,10 +5,13 @@ declare (strict_types = 1);
|
|||||||
namespace tests\Classifier;
|
namespace tests\Classifier;
|
||||||
|
|
||||||
use Phpml\Classifier\KNearestNeighbors;
|
use Phpml\Classifier\KNearestNeighbors;
|
||||||
|
use Phpml\CrossValidation\RandomSplit;
|
||||||
|
use Phpml\Dataset\Demo\Iris;
|
||||||
|
use Phpml\Metric\Accuracy;
|
||||||
|
|
||||||
class KNearestNeighborsTest extends \PHPUnit_Framework_TestCase
|
class KNearestNeighborsTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testPredictSimpleSampleWithDefaultK()
|
public function testPredictSingleSampleWithDefaultK()
|
||||||
{
|
{
|
||||||
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
|
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
|
||||||
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
|
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
|
||||||
@ -33,7 +36,7 @@ class KNearestNeighborsTest extends \PHPUnit_Framework_TestCase
|
|||||||
$trainLabels = ['a', 'a', 'a', 'b', 'b', 'b'];
|
$trainLabels = ['a', 'a', 'a', 'b', 'b', 'b'];
|
||||||
|
|
||||||
$testSamples = [[3, 2], [5, 1], [4, 3], [4, -5], [2, 3], [1, 2], [1, 5], [3, 10]];
|
$testSamples = [[3, 2], [5, 1], [4, 3], [4, -5], [2, 3], [1, 2], [1, 5], [3, 10]];
|
||||||
$testLabels = ['b', 'b', 'b', 'b', 'a', 'a', 'a', 'a',];
|
$testLabels = ['b', 'b', 'b', 'b', 'a', 'a', 'a', 'a'];
|
||||||
|
|
||||||
$classifier = new KNearestNeighbors();
|
$classifier = new KNearestNeighbors();
|
||||||
$classifier->train($trainSamples, $trainLabels);
|
$classifier->train($trainSamples, $trainLabels);
|
||||||
@ -41,4 +44,15 @@ class KNearestNeighborsTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals($testLabels, $predicted);
|
$this->assertEquals($testLabels, $predicted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAccuracyOnIrisDataset()
|
||||||
|
{
|
||||||
|
$dataset = new RandomSplit(new Iris(), $testSize = 0.5, $seed = 123);
|
||||||
|
$classifier = new KNearestNeighbors($k = 4);
|
||||||
|
$classifier->train($dataset->getTrainSamples(), $dataset->getTrainLabels());
|
||||||
|
$predicted = $classifier->predict($dataset->getTestSamples());
|
||||||
|
$score = Accuracy::score($dataset->getTestLabels(), $predicted);
|
||||||
|
|
||||||
|
$this->assertEquals(0.96, $score);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace tests\Phpml\Metric;
|
namespace tests\Phpml\Metric;
|
||||||
|
|
||||||
@ -7,7 +8,6 @@ use Phpml\Metric\Accuracy;
|
|||||||
|
|
||||||
class AccuracyTest extends \PHPUnit_Framework_TestCase
|
class AccuracyTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
@ -34,5 +34,4 @@ class AccuracyTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(3, Accuracy::score($actualLabels, $predictedLabels, false));
|
$this->assertEquals(3, Accuracy::score($actualLabels, $predictedLabels, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user