*/ public function compatibility() : array { return [ DataType::categorical(), DataType::continuous(), ]; } /** * Return the settings of the hyper-parameters in an associative array. * * @internal * * @return mixed[] */ public function params() : array { return [ 'max height' => $this->maxHeight, 'max leaf size' => $this->maxLeafSize, 'max features' => $this->maxFeatures, 'min purity increase' => $this->minPurityIncrease, ]; } /** * Has the learner been trained? * * @return bool */ public function trained() : bool { return !$this->bare(); } /** * Train the learner with a dataset. * * @param Labeled $dataset */ public function train(Dataset $dataset) : void { SpecificationChain::with([ new DatasetIsLabeled($dataset), new DatasetIsNotEmpty($dataset), new SamplesAreCompatibleWithEstimator($dataset, $this), new LabelsAreCompatibleWithLearner($dataset, $this), ])->check(); $this->classes = $dataset->possibleOutcomes(); $this->grow($dataset); } /** * Make predictions from a dataset. * * @param Dataset $dataset * @throws RuntimeException * @return list */ public function predict(Dataset $dataset) : array { if ($this->bare() or !$this->featureCount) { throw new RuntimeException('Estimator has not been trained.'); } DatasetHasDimensionality::with($dataset, $this->featureCount)->check(); return array_map([$this, 'predictSample'], $dataset->samples()); } /** * Predict a single sample and return the result. * * @internal * * @param list $sample * @return string */ public function predictSample(array $sample) : string { /** @var Best $node */ $node = $this->search($sample); return $node->outcome(); } /** * Estimate the joint probabilities for each possible outcome. * * @param Dataset $dataset * @throws RuntimeException * @return list> */ public function proba(Dataset $dataset) : array { if ($this->bare() or !isset($this->classes, $this->featureCount)) { throw new RuntimeException('Estimator has not been trained.'); } DatasetHasDimensionality::with($dataset, $this->featureCount)->check(); $template = array_combine($this->classes, array_fill(0, count($this->classes), 0.0)) ?: []; $probabilities = []; foreach ($dataset->samples() as $sample) { /** @var Best $node */ $node = $this->search($sample); $probabilities[] = array_replace($template, $node->probabilities()); } return $probabilities; } /** * Terminate the branch by selecting the class outcome with the highest probability. * * @param Labeled $dataset * @return Best */ protected function terminate(Labeled $dataset) : Best { $n = $dataset->numSamples(); $counts = array_count_values($dataset->labels()); /** @var string $outcome */ $outcome = argmax($counts); $probabilities = []; foreach ($counts as $class => $count) { $probabilities[$class] = $count / $n; } $p = $counts[$outcome] / $n; $entropy = -($p * log($p)); return new Best($outcome, $probabilities, $entropy, $n); } /** * Calculate the impurity of a set of labels. * * @param list $labels * @return float */ protected function impurity(array $labels) : float { $n = count($labels); if ($n <= 1) { return 0.0; } $counts = array_count_values($labels); $entropy = 0.0; foreach ($counts as $count) { $p = $count / $n; $entropy -= $p * log($p); } return $entropy; } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'Extra Tree Classifier (' . Params::stringify($this->params()) . ')'; } }