php-ml/tests/Phpml/Metric/AccuracyTest.php

56 lines
1.5 KiB
PHP
Raw Normal View History

2016-04-08 20:11:59 +00:00
<?php
2016-04-08 20:49:17 +00:00
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-04-08 20:11:59 +00:00
namespace tests\Phpml\Metric;
2016-05-31 18:01:54 +00:00
use Phpml\Classification\SVC;
use Phpml\CrossValidation\RandomSplit;
use Phpml\Dataset\Demo\IrisDataset;
2016-04-08 20:11:59 +00:00
use Phpml\Metric\Accuracy;
2016-05-31 18:01:54 +00:00
use Phpml\SupportVectorMachine\Kernel;
2016-04-08 20:11:59 +00:00
class AccuracyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments()
{
$actualLabels = ['a', 'b', 'a', 'b'];
$predictedLabels = ['a', 'a'];
Accuracy::score($actualLabels, $predictedLabels);
}
public function testCalculateNormalizedScore()
{
$actualLabels = ['a', 'b', 'a', 'b'];
$predictedLabels = ['a', 'a', 'b', 'b'];
$this->assertEquals(0.5, Accuracy::score($actualLabels, $predictedLabels));
}
public function testCalculateNotNormalizedScore()
{
$actualLabels = ['a', 'b', 'a', 'b'];
$predictedLabels = ['a', 'b', 'b', 'b'];
$this->assertEquals(3, Accuracy::score($actualLabels, $predictedLabels, false));
}
2016-05-31 18:01:54 +00:00
public function testAccuracyOnDemoDataset()
{
$dataset = new RandomSplit(new IrisDataset(), 0.5, 123);
2016-05-31 18:01:54 +00:00
$classifier = new SVC(Kernel::RBF);
$classifier->train($dataset->getTrainSamples(), $dataset->getTrainLabels());
$predicted = $classifier->predict($dataset->getTestSamples());
$accuracy = Accuracy::score($dataset->getTestLabels(), $predicted);
$this->assertEquals(0.959, $accuracy, '', 0.01);
}
2016-04-08 20:11:59 +00:00
}