*/ 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 regression tree by learning the optimal splits in the * training set. * * @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->grow($dataset); } /** * Make a prediction based on the value of a terminal node in the tree. * * @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 int|float */ public function predictSample(array $sample) { /** @var Average $node */ $node = $this->search($sample); return $node->outcome(); } /** * Terminate the branch with the most likely Average. * * @param Labeled $dataset * @return Average */ protected function terminate(Labeled $dataset) : Average { [$mean, $variance] = Stats::meanVar($dataset->labels()); return new Average($mean, $variance, $dataset->numSamples()); } /** * Calculate the impurity of a set of labels. * * @param list $labels * @return float */ protected function impurity(array $labels) : float { return Stats::variance($labels); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'Extra Tree Regressor (' . Params::stringify($this->params()) . ')'; } }