php-ml/src/Phpml/Classification/Ensemble/RandomForest.php

153 lines
4.3 KiB
PHP
Raw Normal View History

2017-02-07 11:37:56 +00:00
<?php
declare(strict_types=1);
namespace Phpml\Classification\Ensemble;
use Phpml\Classification\DecisionTree;
class RandomForest extends Bagging
{
/**
* @var float|string
*/
protected $featureSubsetRatio = 'log';
/**
* @var array
*/
protected $columnNames = null;
/**
* Initializes RandomForest with the given number of trees. More trees
* may increase the prediction performance while it will also substantially
* increase the processing time and the required memory
*/
public function __construct(int $numClassifier = 50)
2017-02-07 11:37:56 +00:00
{
parent::__construct($numClassifier);
$this->setSubsetRatio(1.0);
}
/**
* This method is used to determine how many of the original columns (features)
2017-02-07 11:37:56 +00:00
* will be used to construct subsets to train base classifiers.<br>
*
* Allowed values: 'sqrt', 'log' or any float number between 0.1 and 1.0 <br>
*
* Default value for the ratio is 'log' which results in log(numFeatures, 2) + 1
* features to be taken into consideration while selecting subspace of features
2017-02-07 11:37:56 +00:00
*
* @param mixed $ratio string or float should be given
*
2017-02-07 11:37:56 +00:00
* @return $this
*
* @throws \Exception
2017-02-07 11:37:56 +00:00
*/
public function setFeatureSubsetRatio($ratio)
{
if (is_float($ratio) && ($ratio < 0.1 || $ratio > 1.0)) {
throw new \Exception('When a float given, feature subset ratio should be between 0.1 and 1.0');
2017-02-07 11:37:56 +00:00
}
2017-02-07 11:37:56 +00:00
if (is_string($ratio) && $ratio != 'sqrt' && $ratio != 'log') {
throw new \Exception("When a string given, feature subset ratio can only be 'sqrt' or 'log' ");
}
2017-02-07 11:37:56 +00:00
$this->featureSubsetRatio = $ratio;
2017-02-07 11:37:56 +00:00
return $this;
}
/**
* RandomForest algorithm is usable *only* with DecisionTree
*
* @return $this
*
* @throws \Exception
2017-02-07 11:37:56 +00:00
*/
public function setClassifer(string $classifier, array $classifierOptions = [])
{
if ($classifier != DecisionTree::class) {
throw new \Exception('RandomForest can only use DecisionTree as base classifier');
2017-02-07 11:37:56 +00:00
}
return parent::setClassifer($classifier, $classifierOptions);
}
/**
* This will return an array including an importance value for
* each column in the given dataset. Importance values for a column
* is the average importance of that column in all trees in the forest
*/
public function getFeatureImportances() : array
{
// Traverse each tree and sum importance of the columns
$sum = [];
foreach ($this->classifiers as $tree) {
/* @var $tree DecisionTree */
$importances = $tree->getFeatureImportances();
foreach ($importances as $column => $importance) {
if (array_key_exists($column, $sum)) {
$sum[$column] += $importance;
} else {
$sum[$column] = $importance;
}
}
}
// Normalize & sort the importance values
$total = array_sum($sum);
foreach ($sum as &$importance) {
$importance /= $total;
}
arsort($sum);
return $sum;
}
/**
* A string array to represent the columns is given. They are useful
* when trying to print some information about the trees such as feature importances
*
* @return $this
*/
public function setColumnNames(array $names)
{
$this->columnNames = $names;
return $this;
}
2017-02-07 11:37:56 +00:00
/**
* @param DecisionTree $classifier
*
2017-02-07 11:37:56 +00:00
* @return DecisionTree
*/
protected function initSingleClassifier($classifier)
2017-02-07 11:37:56 +00:00
{
if (is_float($this->featureSubsetRatio)) {
$featureCount = (int) ($this->featureSubsetRatio * $this->featureCount);
2017-02-07 11:37:56 +00:00
} elseif ($this->featureCount == 'sqrt') {
$featureCount = (int) sqrt($this->featureCount) + 1;
2017-02-07 11:37:56 +00:00
} else {
$featureCount = (int) log($this->featureCount, 2) + 1;
2017-02-07 11:37:56 +00:00
}
if ($featureCount >= $this->featureCount) {
$featureCount = $this->featureCount;
}
if ($this->columnNames === null) {
$this->columnNames = range(0, $this->featureCount - 1);
}
return $classifier
->setColumnNames($this->columnNames)
->setNumFeatures($featureCount);
2017-02-07 11:37:56 +00:00
}
}