mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-14 17:34:06 +00:00
a348111e97
* tests: update to PHPUnit 6.0 with rector * fix namespaces on tests * composer + tests: use standard test namespace naming * update travis * resolve conflict * phpstan lvl 2 * phpstan lvl 3 * phpstan lvl 4 * phpstan lvl 5 * phpstan lvl 6 * phpstan lvl 7 * level max * resolve conflict * [cs] clean empty docs * composer: bump to PHPUnit 6.4 * cleanup * composer + travis: add phpstan * phpstan lvl 1 * composer: update dev deps * phpstan fixes * update Contributing with new tools * docs: link fixes, PHP version update * composer: drop php-cs-fixer, cs already handled by ecs * ecs: add old set rules * [cs] apply rest of rules
89 lines
3.3 KiB
PHP
89 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Phpml\Tests\Classification;
|
|
|
|
use Phpml\Classification\DecisionTree;
|
|
use Phpml\ModelManager;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DecisionTreeTest extends TestCase
|
|
{
|
|
private $data = [
|
|
['sunny', 85, 85, 'false', 'Dont_play'],
|
|
['sunny', 80, 90, 'true', 'Dont_play'],
|
|
['overcast', 83, 78, 'false', 'Play'],
|
|
['rain', 70, 96, 'false', 'Play'],
|
|
['rain', 68, 80, 'false', 'Play'],
|
|
['rain', 65, 70, 'true', 'Dont_play'],
|
|
['overcast', 64, 65, 'true', 'Play'],
|
|
['sunny', 72, 95, 'false', 'Dont_play'],
|
|
['sunny', 69, 70, 'false', 'Play'],
|
|
['rain', 75, 80, 'false', 'Play'],
|
|
['sunny', 75, 70, 'true', 'Play'],
|
|
['overcast', 72, 90, 'true', 'Play'],
|
|
['overcast', 81, 75, 'false', 'Play'],
|
|
['rain', 71, 80, 'true', 'Dont_play'],
|
|
];
|
|
|
|
private $extraData = [
|
|
['scorching', 90, 95, 'false', 'Dont_play'],
|
|
['scorching', 100, 93, 'true', 'Dont_play'],
|
|
];
|
|
|
|
public function testPredictSingleSample()
|
|
{
|
|
[$data, $targets] = $this->getData($this->data);
|
|
$classifier = new DecisionTree(5);
|
|
$classifier->train($data, $targets);
|
|
$this->assertEquals('Dont_play', $classifier->predict(['sunny', 78, 72, 'false']));
|
|
$this->assertEquals('Play', $classifier->predict(['overcast', 60, 60, 'false']));
|
|
$this->assertEquals('Dont_play', $classifier->predict(['rain', 60, 60, 'true']));
|
|
|
|
[$data, $targets] = $this->getData($this->extraData);
|
|
$classifier->train($data, $targets);
|
|
$this->assertEquals('Dont_play', $classifier->predict(['scorching', 95, 90, 'true']));
|
|
$this->assertEquals('Play', $classifier->predict(['overcast', 60, 60, 'false']));
|
|
|
|
return $classifier;
|
|
}
|
|
|
|
public function testSaveAndRestore(): void
|
|
{
|
|
[$data, $targets] = $this->getData($this->data);
|
|
$classifier = new DecisionTree(5);
|
|
$classifier->train($data, $targets);
|
|
|
|
$testSamples = [['sunny', 78, 72, 'false'], ['overcast', 60, 60, 'false']];
|
|
$predicted = $classifier->predict($testSamples);
|
|
|
|
$filename = 'decision-tree-test-'.random_int(100, 999).'-'.uniqid();
|
|
$filepath = tempnam(sys_get_temp_dir(), $filename);
|
|
$modelManager = new ModelManager();
|
|
$modelManager->saveToFile($classifier, $filepath);
|
|
|
|
$restoredClassifier = $modelManager->restoreFromFile($filepath);
|
|
$this->assertEquals($classifier, $restoredClassifier);
|
|
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
|
|
}
|
|
|
|
public function testTreeDepth(): void
|
|
{
|
|
[$data, $targets] = $this->getData($this->data);
|
|
$classifier = new DecisionTree(5);
|
|
$classifier->train($data, $targets);
|
|
$this->assertTrue($classifier->actualDepth <= 5);
|
|
}
|
|
|
|
private function getData($input)
|
|
{
|
|
$targets = array_column($input, 4);
|
|
array_walk($input, function (&$v): void {
|
|
array_splice($v, 4, 1);
|
|
});
|
|
|
|
return [$input, $targets];
|
|
}
|
|
}
|