2017-02-07 11:37:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Classification\Ensemble;
|
2017-02-07 11:37:56 +00:00
|
|
|
|
|
|
|
use Phpml\Classification\DecisionTree;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\Classification\Ensemble\RandomForest;
|
2017-02-07 11:37:56 +00:00
|
|
|
use Phpml\Classification\NaiveBayes;
|
2018-03-04 16:02:36 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2017-02-07 11:37:56 +00:00
|
|
|
|
|
|
|
class RandomForestTest extends BaggingTest
|
|
|
|
{
|
2018-03-04 16:02:36 +00:00
|
|
|
public function testThrowExceptionWithInvalidClassifier(): void
|
2017-11-22 21:16:10 +00:00
|
|
|
{
|
2018-03-04 16:02:36 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('RandomForest can only use DecisionTree as base classifier');
|
|
|
|
|
|
|
|
$classifier = new RandomForest();
|
|
|
|
$classifier->setClassifer(NaiveBayes::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowExceptionWithInvalidFeatureSubsetRatioType(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Feature subset ratio must be a string or a float');
|
|
|
|
|
|
|
|
$classifier = new RandomForest();
|
|
|
|
$classifier->setFeatureSubsetRatio(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowExceptionWithInvalidFeatureSubsetRatioFloat(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('When a float is given, feature subset ratio should be between 0.1 and 1.0');
|
|
|
|
|
|
|
|
$classifier = new RandomForest();
|
|
|
|
$classifier->setFeatureSubsetRatio(1.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowExceptionWithInvalidFeatureSubsetRatioString(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage("When a string is given, feature subset ratio can only be 'sqrt' or 'log'");
|
|
|
|
|
|
|
|
$classifier = new RandomForest();
|
|
|
|
$classifier->setFeatureSubsetRatio('pow');
|
2017-11-22 21:16:10 +00:00
|
|
|
}
|
2018-01-06 12:09:33 +00:00
|
|
|
|
2017-02-07 11:37:56 +00:00
|
|
|
protected function getClassifier($numBaseClassifiers = 50)
|
|
|
|
{
|
|
|
|
$classifier = new RandomForest($numBaseClassifiers);
|
|
|
|
$classifier->setFeatureSubsetRatio('log');
|
2017-08-17 06:50:37 +00:00
|
|
|
|
2017-02-07 11:37:56 +00:00
|
|
|
return $classifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAvailableBaseClassifiers()
|
|
|
|
{
|
2017-08-17 06:50:37 +00:00
|
|
|
return [DecisionTree::class => ['depth' => 5]];
|
2017-02-07 11:37:56 +00:00
|
|
|
}
|
|
|
|
}
|