2017-01-31 19:27:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Classification;
|
2017-01-31 19:27:15 +00:00
|
|
|
|
|
|
|
use Phpml\Classification\DecisionTree;
|
2017-02-02 08:03:09 +00:00
|
|
|
use Phpml\ModelManager;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-01-31 19:27:15 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class DecisionTreeTest extends TestCase
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
2018-10-28 06:44:52 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-02-01 18:06:38 +00:00
|
|
|
private $data = [
|
2017-08-17 06:50:37 +00:00
|
|
|
['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'],
|
2017-11-22 21:16:10 +00:00
|
|
|
['rain', 71, 80, 'true', 'Dont_play'],
|
2017-01-31 19:27:15 +00:00
|
|
|
];
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-02-01 18:06:38 +00:00
|
|
|
private $extraData = [
|
|
|
|
['scorching', 90, 95, 'false', 'Dont_play'],
|
|
|
|
['scorching', 100, 93, 'true', 'Dont_play'],
|
|
|
|
];
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
public function testPredictSingleSample(): void
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
[$data, $targets] = $this->getData($this->data);
|
2017-01-31 19:27:15 +00:00
|
|
|
$classifier = new DecisionTree(5);
|
|
|
|
$classifier->train($data, $targets);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals('Dont_play', $classifier->predict(['sunny', 78, 72, 'false']));
|
|
|
|
self::assertEquals('Play', $classifier->predict(['overcast', 60, 60, 'false']));
|
|
|
|
self::assertEquals('Dont_play', $classifier->predict(['rain', 60, 60, 'true']));
|
2017-01-31 19:27:15 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
[$data, $targets] = $this->getData($this->extraData);
|
2017-02-01 18:06:38 +00:00
|
|
|
$classifier->train($data, $targets);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals('Dont_play', $classifier->predict(['scorching', 95, 90, 'true']));
|
|
|
|
self::assertEquals('Play', $classifier->predict(['overcast', 60, 60, 'false']));
|
2017-01-31 19:27:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSaveAndRestore(): void
|
2017-02-02 08:03:09 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
[$data, $targets] = $this->getData($this->data);
|
2017-02-02 08:03:09 +00:00
|
|
|
$classifier = new DecisionTree(5);
|
|
|
|
$classifier->train($data, $targets);
|
|
|
|
|
|
|
|
$testSamples = [['sunny', 78, 72, 'false'], ['overcast', 60, 60, 'false']];
|
|
|
|
$predicted = $classifier->predict($testSamples);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
$filename = 'decision-tree-test-'.random_int(100, 999).'-'.uniqid('', false);
|
|
|
|
$filepath = (string) tempnam(sys_get_temp_dir(), $filename);
|
2017-02-02 08:03:09 +00:00
|
|
|
$modelManager = new ModelManager();
|
|
|
|
$modelManager->saveToFile($classifier, $filepath);
|
|
|
|
|
|
|
|
$restoredClassifier = $modelManager->restoreFromFile($filepath);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($classifier, $restoredClassifier);
|
|
|
|
self::assertEquals($predicted, $restoredClassifier->predict($testSamples));
|
2017-02-02 08:03:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testTreeDepth(): void
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
[$data, $targets] = $this->getData($this->data);
|
2017-01-31 19:27:15 +00:00
|
|
|
$classifier = new DecisionTree(5);
|
|
|
|
$classifier->train($data, $targets);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertTrue($classifier->actualDepth <= 5);
|
2017-11-22 21:16:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
private function getData(array $input): array
|
2017-11-22 21:16:10 +00:00
|
|
|
{
|
|
|
|
$targets = array_column($input, 4);
|
|
|
|
array_walk($input, function (&$v): void {
|
|
|
|
array_splice($v, 4, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
return [$input, $targets];
|
2017-01-31 19:27:15 +00:00
|
|
|
}
|
|
|
|
}
|