2017-02-16 22:23:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Phpml\Classification\Linear;
|
|
|
|
|
|
|
|
use Phpml\Classification\DecisionTree;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\Classification\WeightedClassifier;
|
2018-03-06 22:26:36 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\Helper\OneVsRest;
|
|
|
|
use Phpml\Helper\Predictable;
|
2017-10-24 16:59:12 +00:00
|
|
|
use Phpml\Math\Comparison;
|
2017-02-16 22:23:55 +00:00
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
class DecisionStump extends WeightedClassifier
|
2017-02-16 22:23:55 +00:00
|
|
|
{
|
2017-03-05 08:43:19 +00:00
|
|
|
use Predictable, OneVsRest;
|
2017-02-16 22:23:55 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public const AUTO_SELECT = -1;
|
2017-02-28 20:45:18 +00:00
|
|
|
|
2017-02-16 22:23:55 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2017-02-28 20:45:18 +00:00
|
|
|
protected $givenColumnIndex;
|
2017-02-16 22:23:55 +00:00
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected $binaryLabels = [];
|
2017-02-16 22:23:55 +00:00
|
|
|
|
2017-02-21 09:38:18 +00:00
|
|
|
/**
|
|
|
|
* Lowest error rate obtained while training/optimizing the model
|
|
|
|
*
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $trainingErrorRate;
|
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $column;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $operator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected $columnTypes = [];
|
2017-02-28 20:45:18 +00:00
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $featureCount;
|
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
2017-03-05 08:43:19 +00:00
|
|
|
protected $numSplitCount = 100.0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Distribution of samples in the leaves
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected $prob = [];
|
2017-02-28 20:45:18 +00:00
|
|
|
|
2017-02-16 22:23:55 +00:00
|
|
|
/**
|
|
|
|
* A DecisionStump classifier is a one-level deep DecisionTree. It is generally
|
|
|
|
* used with ensemble algorithms as in the weak classifier role. <br>
|
|
|
|
*
|
|
|
|
* If columnIndex is given, then the stump tries to produce a decision node
|
|
|
|
* on this column, otherwise in cases given the value of -1, the stump itself
|
|
|
|
* decides which column to take for the decision (Default DecisionTree behaviour)
|
|
|
|
*/
|
2017-02-28 20:45:18 +00:00
|
|
|
public function __construct(int $columnIndex = self::AUTO_SELECT)
|
2017-02-16 22:23:55 +00:00
|
|
|
{
|
2017-02-28 20:45:18 +00:00
|
|
|
$this->givenColumnIndex = $columnIndex;
|
2017-02-16 22:23:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function __toString(): string
|
|
|
|
{
|
2018-01-06 20:25:47 +00:00
|
|
|
return "IF ${this}->column ${this}->operator ${this}->value ".
|
2017-11-22 21:16:10 +00:00
|
|
|
'THEN '.$this->binaryLabels[0].' '.
|
|
|
|
'ELSE '.$this->binaryLabels[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* While finding best split point for a numerical valued column,
|
|
|
|
* DecisionStump looks for equally distanced values between minimum and maximum
|
|
|
|
* values in the column. Given <i>$count</i> value determines how many split
|
|
|
|
* points to be probed. The more split counts, the better performance but
|
|
|
|
* worse processing time (Default value is 10.0)
|
|
|
|
*/
|
|
|
|
public function setNumericalSplitCount(float $count): void
|
|
|
|
{
|
|
|
|
$this->numSplitCount = $count;
|
|
|
|
}
|
|
|
|
|
2017-02-16 22:23:55 +00:00
|
|
|
/**
|
2018-03-06 22:26:36 +00:00
|
|
|
* @throws InvalidArgumentException
|
2017-02-16 22:23:55 +00:00
|
|
|
*/
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function trainBinary(array $samples, array $targets, array $labels): void
|
2017-02-16 22:23:55 +00:00
|
|
|
{
|
2017-04-19 20:26:31 +00:00
|
|
|
$this->binaryLabels = $labels;
|
|
|
|
$this->featureCount = count($samples[0]);
|
2017-02-16 22:23:55 +00:00
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
// If a column index is given, it should be among the existing columns
|
2017-04-19 20:26:31 +00:00
|
|
|
if ($this->givenColumnIndex > count($samples[0]) - 1) {
|
2017-02-28 20:45:18 +00:00
|
|
|
$this->givenColumnIndex = self::AUTO_SELECT;
|
2017-02-16 22:23:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
// Check the size of the weights given.
|
|
|
|
// If none given, then assign 1 as a weight to each sample
|
2018-02-16 06:25:24 +00:00
|
|
|
if (!empty($this->weights)) {
|
2017-02-21 09:38:18 +00:00
|
|
|
$numWeights = count($this->weights);
|
2017-04-19 20:26:31 +00:00
|
|
|
if ($numWeights != count($samples)) {
|
2018-03-06 22:26:36 +00:00
|
|
|
throw new InvalidArgumentException('Number of sample weights does not match with number of samples');
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-19 20:26:31 +00:00
|
|
|
$this->weights = array_fill(0, count($samples), 1);
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
// Determine type of each column as either "continuous" or "nominal"
|
2017-04-19 20:26:31 +00:00
|
|
|
$this->columnTypes = DecisionTree::getColumnTypes($samples);
|
2017-02-21 09:38:18 +00:00
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
// Try to find the best split in the columns of the dataset
|
|
|
|
// by calculating error rate for each split point in each column
|
2017-04-19 20:26:31 +00:00
|
|
|
$columns = range(0, count($samples[0]) - 1);
|
2017-02-28 20:45:18 +00:00
|
|
|
if ($this->givenColumnIndex != self::AUTO_SELECT) {
|
|
|
|
$columns = [$this->givenColumnIndex];
|
|
|
|
}
|
2017-02-21 09:38:18 +00:00
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
$bestSplit = [
|
2017-11-22 21:16:10 +00:00
|
|
|
'value' => 0,
|
|
|
|
'operator' => '',
|
|
|
|
'prob' => [],
|
|
|
|
'column' => 0,
|
|
|
|
'trainingErrorRate' => 1.0,
|
|
|
|
];
|
2017-02-28 20:45:18 +00:00
|
|
|
foreach ($columns as $col) {
|
2017-03-05 15:25:01 +00:00
|
|
|
if ($this->columnTypes[$col] == DecisionTree::CONTINUOUS) {
|
2017-04-19 20:26:31 +00:00
|
|
|
$split = $this->getBestNumericalSplit($samples, $targets, $col);
|
2017-02-28 20:45:18 +00:00
|
|
|
} else {
|
2017-04-19 20:26:31 +00:00
|
|
|
$split = $this->getBestNominalSplit($samples, $targets, $col);
|
2017-02-28 20:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($split['trainingErrorRate'] < $bestSplit['trainingErrorRate']) {
|
|
|
|
$bestSplit = $split;
|
|
|
|
}
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
// Assign determined best values to the stump
|
|
|
|
foreach ($bestSplit as $name => $value) {
|
|
|
|
$this->{$name} = $value;
|
|
|
|
}
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-28 20:45:18 +00:00
|
|
|
* Determines best split point for the given column
|
2017-02-21 09:38:18 +00:00
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected function getBestNumericalSplit(array $samples, array $targets, int $col): array
|
2017-02-21 09:38:18 +00:00
|
|
|
{
|
2017-04-19 20:26:31 +00:00
|
|
|
$values = array_column($samples, $col);
|
2017-03-05 08:43:19 +00:00
|
|
|
// Trying all possible points may be accomplished in two general ways:
|
|
|
|
// 1- Try all values in the $samples array ($values)
|
|
|
|
// 2- Artificially split the range of values into several parts and try them
|
|
|
|
// We choose the second one because it is faster in larger datasets
|
2017-02-21 09:38:18 +00:00
|
|
|
$minValue = min($values);
|
|
|
|
$maxValue = max($values);
|
2017-02-28 20:45:18 +00:00
|
|
|
$stepSize = ($maxValue - $minValue) / $this->numSplitCount;
|
2017-02-21 09:38:18 +00:00
|
|
|
|
2018-01-06 20:25:47 +00:00
|
|
|
$split = [];
|
2017-02-21 09:38:18 +00:00
|
|
|
|
|
|
|
foreach (['<=', '>'] as $operator) {
|
2017-02-28 20:45:18 +00:00
|
|
|
// Before trying all possible split points, let's first try
|
|
|
|
// the average value for the cut point
|
|
|
|
$threshold = array_sum($values) / (float) count($values);
|
2017-11-14 20:21:23 +00:00
|
|
|
[$errorRate, $prob] = $this->calculateErrorRate($targets, $threshold, $operator, $values);
|
2018-01-06 20:25:47 +00:00
|
|
|
if ($split === [] || $errorRate < $split['trainingErrorRate']) {
|
2017-11-22 21:16:10 +00:00
|
|
|
$split = [
|
|
|
|
'value' => $threshold,
|
|
|
|
'operator' => $operator,
|
|
|
|
'prob' => $prob,
|
|
|
|
'column' => $col,
|
|
|
|
'trainingErrorRate' => $errorRate,
|
|
|
|
];
|
2017-02-28 20:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try other possible points one by one
|
2017-08-17 06:50:37 +00:00
|
|
|
for ($step = $minValue; $step <= $maxValue; $step += $stepSize) {
|
|
|
|
$threshold = (float) $step;
|
2017-11-14 20:21:23 +00:00
|
|
|
[$errorRate, $prob] = $this->calculateErrorRate($targets, $threshold, $operator, $values);
|
2017-02-28 20:45:18 +00:00
|
|
|
if ($errorRate < $split['trainingErrorRate']) {
|
2017-11-22 21:16:10 +00:00
|
|
|
$split = [
|
|
|
|
'value' => $threshold,
|
|
|
|
'operator' => $operator,
|
|
|
|
'prob' => $prob,
|
|
|
|
'column' => $col,
|
|
|
|
'trainingErrorRate' => $errorRate,
|
|
|
|
];
|
2017-02-28 20:45:18 +00:00
|
|
|
}
|
|
|
|
}// for
|
|
|
|
}
|
|
|
|
|
|
|
|
return $split;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
protected function getBestNominalSplit(array $samples, array $targets, int $col): array
|
2017-02-28 20:45:18 +00:00
|
|
|
{
|
2017-04-19 20:26:31 +00:00
|
|
|
$values = array_column($samples, $col);
|
2017-02-28 20:45:18 +00:00
|
|
|
$valueCounts = array_count_values($values);
|
2017-08-17 06:50:37 +00:00
|
|
|
$distinctVals = array_keys($valueCounts);
|
2017-02-28 20:45:18 +00:00
|
|
|
|
2018-01-06 20:25:47 +00:00
|
|
|
$split = [];
|
2017-02-28 20:45:18 +00:00
|
|
|
|
|
|
|
foreach (['=', '!='] as $operator) {
|
|
|
|
foreach ($distinctVals as $val) {
|
2017-11-14 20:21:23 +00:00
|
|
|
[$errorRate, $prob] = $this->calculateErrorRate($targets, $val, $operator, $values);
|
2017-02-21 09:38:18 +00:00
|
|
|
|
2018-01-06 20:25:47 +00:00
|
|
|
if ($split === [] || $split['trainingErrorRate'] < $errorRate) {
|
2017-11-22 21:16:10 +00:00
|
|
|
$split = [
|
|
|
|
'value' => $val,
|
|
|
|
'operator' => $operator,
|
|
|
|
'prob' => $prob,
|
|
|
|
'column' => $col,
|
|
|
|
'trainingErrorRate' => $errorRate,
|
|
|
|
];
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
2017-03-05 15:45:48 +00:00
|
|
|
}
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:45:18 +00:00
|
|
|
return $split;
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:38:18 +00:00
|
|
|
/**
|
|
|
|
* Calculates the ratio of wrong predictions based on the new threshold
|
|
|
|
* value given as the parameter
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected function calculateErrorRate(array $targets, float $threshold, string $operator, array $values): array
|
2017-02-21 09:38:18 +00:00
|
|
|
{
|
2017-02-28 20:45:18 +00:00
|
|
|
$wrong = 0.0;
|
2017-03-05 08:43:19 +00:00
|
|
|
$prob = [];
|
|
|
|
$leftLabel = $this->binaryLabels[0];
|
2017-08-17 06:50:37 +00:00
|
|
|
$rightLabel = $this->binaryLabels[1];
|
2017-03-05 08:43:19 +00:00
|
|
|
|
2017-02-21 09:38:18 +00:00
|
|
|
foreach ($values as $index => $value) {
|
2017-10-24 16:59:12 +00:00
|
|
|
if (Comparison::compare($value, $threshold, $operator)) {
|
2017-02-28 20:45:18 +00:00
|
|
|
$predicted = $leftLabel;
|
|
|
|
} else {
|
|
|
|
$predicted = $rightLabel;
|
|
|
|
}
|
2017-02-21 09:38:18 +00:00
|
|
|
|
2017-04-19 20:26:31 +00:00
|
|
|
$target = $targets[$index];
|
2017-09-02 19:41:06 +00:00
|
|
|
if ((string) $predicted != (string) $targets[$index]) {
|
2017-02-21 09:38:18 +00:00
|
|
|
$wrong += $this->weights[$index];
|
|
|
|
}
|
2017-03-05 08:43:19 +00:00
|
|
|
|
2017-05-17 07:03:25 +00:00
|
|
|
if (!isset($prob[$predicted][$target])) {
|
2017-03-05 08:43:19 +00:00
|
|
|
$prob[$predicted][$target] = 0;
|
|
|
|
}
|
2017-11-22 21:16:10 +00:00
|
|
|
|
2017-05-17 07:03:25 +00:00
|
|
|
++$prob[$predicted][$target];
|
2017-02-21 09:38:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
// Calculate probabilities: Proportion of labels in each leaf
|
|
|
|
$dist = array_combine($this->binaryLabels, array_fill(0, 2, 0.0));
|
|
|
|
foreach ($prob as $leaf => $counts) {
|
2017-08-17 06:50:37 +00:00
|
|
|
$leafTotal = (float) array_sum($prob[$leaf]);
|
2017-03-05 08:43:19 +00:00
|
|
|
foreach ($counts as $label => $count) {
|
2017-09-02 19:41:06 +00:00
|
|
|
if ((string) $leaf == (string) $label) {
|
2017-03-05 08:43:19 +00:00
|
|
|
$dist[$leaf] = $count / $leafTotal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$wrong / (float) array_sum($this->weights), $dist];
|
2017-02-16 22:23:55 +00:00
|
|
|
}
|
2017-02-28 20:45:18 +00:00
|
|
|
|
|
|
|
/**
|
2017-03-05 08:43:19 +00:00
|
|
|
* Returns the probability of the sample of belonging to the given label
|
|
|
|
*
|
|
|
|
* Probability of a sample is calculated as the proportion of the label
|
|
|
|
* within the labels of the training samples in the decision node
|
|
|
|
*
|
|
|
|
* @param mixed $label
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected function predictProbability(array $sample, $label): float
|
2017-03-05 08:43:19 +00:00
|
|
|
{
|
|
|
|
$predicted = $this->predictSampleBinary($sample);
|
2017-09-02 19:41:06 +00:00
|
|
|
if ((string) $predicted == (string) $label) {
|
2017-03-05 08:43:19 +00:00
|
|
|
return $this->prob[$label];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-28 20:45:18 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-03-05 08:43:19 +00:00
|
|
|
protected function predictSampleBinary(array $sample)
|
2017-02-28 20:45:18 +00:00
|
|
|
{
|
2017-10-24 16:59:12 +00:00
|
|
|
if (Comparison::compare($sample[$this->column], $this->value, $this->operator)) {
|
2017-03-05 08:43:19 +00:00
|
|
|
return $this->binaryLabels[0];
|
2017-02-28 20:45:18 +00:00
|
|
|
}
|
2017-03-05 08:43:19 +00:00
|
|
|
|
|
|
|
return $this->binaryLabels[1];
|
2017-02-28 20:45:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function resetBinary(): void
|
2017-04-19 20:26:31 +00:00
|
|
|
{
|
|
|
|
}
|
2017-02-16 22:23:55 +00:00
|
|
|
}
|