2017-03-05 08:43:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Phpml\Helper;
|
|
|
|
|
2017-10-24 16:59:12 +00:00
|
|
|
use Phpml\Classification\Classifier;
|
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
trait OneVsRest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-04-19 20:26:31 +00:00
|
|
|
protected $classifiers = [];
|
2017-03-05 08:43:19 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-19 20:26:31 +00:00
|
|
|
* All provided training targets' labels.
|
|
|
|
*
|
2017-03-05 08:43:19 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2017-04-19 20:26:31 +00:00
|
|
|
protected $allLabels = [];
|
2017-03-05 08:43:19 +00:00
|
|
|
|
2017-03-27 21:46:53 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-04-19 20:26:31 +00:00
|
|
|
protected $costValues = [];
|
2017-03-27 21:46:53 +00:00
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
/**
|
|
|
|
* Train a binary classifier in the OvR style
|
|
|
|
*/
|
2017-11-14 20:21:23 +00:00
|
|
|
public function train(array $samples, array $targets): void
|
2017-03-05 08:43:19 +00:00
|
|
|
{
|
2017-04-19 20:26:31 +00:00
|
|
|
// Clears previous stuff.
|
|
|
|
$this->reset();
|
|
|
|
|
2017-05-17 07:03:25 +00:00
|
|
|
$this->trainBylabel($samples, $targets);
|
2017-04-19 20:26:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
/**
|
|
|
|
* Resets the classifier and the vars internally used by OneVsRest to create multiple classifiers.
|
|
|
|
*/
|
|
|
|
public function reset(): void
|
|
|
|
{
|
|
|
|
$this->classifiers = [];
|
|
|
|
$this->allLabels = [];
|
|
|
|
$this->costValues = [];
|
|
|
|
|
|
|
|
$this->resetBinary();
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function trainByLabel(array $samples, array $targets, array $allLabels = []): void
|
2017-04-19 20:26:31 +00:00
|
|
|
{
|
|
|
|
// Overwrites the current value if it exist. $allLabels must be provided for each partialTrain run.
|
|
|
|
if (!empty($allLabels)) {
|
|
|
|
$this->allLabels = $allLabels;
|
|
|
|
} else {
|
|
|
|
$this->allLabels = array_keys(array_count_values($targets));
|
|
|
|
}
|
2017-11-22 21:16:10 +00:00
|
|
|
|
2017-04-19 20:26:31 +00:00
|
|
|
sort($this->allLabels, SORT_STRING);
|
2017-03-05 08:43:19 +00:00
|
|
|
|
|
|
|
// If there are only two targets, then there is no need to perform OvR
|
2017-04-19 20:26:31 +00:00
|
|
|
if (count($this->allLabels) == 2) {
|
|
|
|
// Init classifier if required.
|
|
|
|
if (empty($this->classifiers)) {
|
|
|
|
$this->classifiers[0] = $this->getClassifierCopy();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->classifiers[0]->trainBinary($samples, $targets, $this->allLabels);
|
2017-03-05 08:43:19 +00:00
|
|
|
} else {
|
|
|
|
// Train a separate classifier for each label and memorize them
|
2017-04-19 20:26:31 +00:00
|
|
|
|
|
|
|
foreach ($this->allLabels as $label) {
|
|
|
|
// Init classifier if required.
|
|
|
|
if (empty($this->classifiers[$label])) {
|
|
|
|
$this->classifiers[$label] = $this->getClassifierCopy();
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
[$binarizedTargets, $classifierLabels] = $this->binarizeTargets($targets, $label);
|
2017-04-19 20:26:31 +00:00
|
|
|
$this->classifiers[$label]->trainBinary($samples, $binarizedTargets, $classifierLabels);
|
2017-03-05 08:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 20:26:31 +00:00
|
|
|
|
2017-03-27 21:46:53 +00:00
|
|
|
// If the underlying classifier is capable of giving the cost values
|
|
|
|
// during the training, then assign it to the relevant variable
|
2017-04-19 20:26:31 +00:00
|
|
|
// Adding just the first classifier cost values to avoid complex average calculations.
|
|
|
|
$classifierref = reset($this->classifiers);
|
|
|
|
if (method_exists($classifierref, 'getCostValues')) {
|
|
|
|
$this->costValues = $classifierref->getCostValues();
|
2017-03-27 21:46:53 +00:00
|
|
|
}
|
2017-03-05 08:43:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:26:31 +00:00
|
|
|
/**
|
|
|
|
* Returns an instance of the current class after cleaning up OneVsRest stuff.
|
|
|
|
*
|
2017-10-24 16:59:12 +00:00
|
|
|
* @return Classifier|OneVsRest
|
2017-04-19 20:26:31 +00:00
|
|
|
*/
|
|
|
|
protected function getClassifierCopy()
|
|
|
|
{
|
|
|
|
// Clone the current classifier, so that
|
|
|
|
// we don't mess up its variables while training
|
|
|
|
// multiple instances of this classifier
|
|
|
|
$classifier = clone $this;
|
|
|
|
$classifier->reset();
|
2017-08-17 06:50:37 +00:00
|
|
|
|
2017-04-19 20:26:31 +00:00
|
|
|
return $classifier;
|
|
|
|
}
|
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function predictSample(array $sample)
|
|
|
|
{
|
2017-04-19 20:26:31 +00:00
|
|
|
if (count($this->allLabels) == 2) {
|
2017-03-05 08:43:19 +00:00
|
|
|
return $this->classifiers[0]->predictSampleBinary($sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
$probs = [];
|
|
|
|
|
|
|
|
foreach ($this->classifiers as $label => $predictor) {
|
|
|
|
$probs[$label] = $predictor->predictProbability($sample, $label);
|
|
|
|
}
|
|
|
|
|
|
|
|
arsort($probs, SORT_NUMERIC);
|
2017-08-17 06:50:37 +00:00
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
return key($probs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each classifier should implement this method instead of train(samples, targets)
|
2017-04-19 20:26:31 +00:00
|
|
|
*/
|
|
|
|
abstract protected function trainBinary(array $samples, array $targets, array $labels);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To be overwritten by OneVsRest classifiers.
|
2017-03-05 08:43:19 +00:00
|
|
|
*/
|
2017-11-14 20:21:23 +00:00
|
|
|
abstract protected function resetBinary(): void;
|
2017-03-05 08:43:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Each classifier that make use of OvR approach should be able to
|
|
|
|
* return a probability for a sample to belong to the given label.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract protected function predictProbability(array $sample, string $label);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each classifier should implement this method instead of predictSample()
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract protected function predictSampleBinary(array $sample);
|
2017-11-22 21:16:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups all targets into two groups: Targets equal to
|
|
|
|
* the given label and the others
|
|
|
|
*
|
|
|
|
* $targets is not passed by reference nor contains objects so this method
|
|
|
|
* changes will not affect the caller $targets array.
|
|
|
|
*
|
|
|
|
* @param mixed $label
|
|
|
|
*
|
|
|
|
* @return array Binarized targets and target's labels
|
|
|
|
*/
|
|
|
|
private function binarizeTargets(array $targets, $label): array
|
|
|
|
{
|
|
|
|
$notLabel = "not_$label";
|
|
|
|
foreach ($targets as $key => $target) {
|
|
|
|
$targets[$key] = $target == $label ? $label : $notLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
$labels = [$label, $notLabel];
|
|
|
|
|
|
|
|
return [$targets, $labels];
|
|
|
|
}
|
2017-03-05 08:43:19 +00:00
|
|
|
}
|