k = $k; $this->weighted = $weighted; $this->tree = $tree ?? new KDTree(); } /** * Return the estimator type. * * @internal * * @return EstimatorType */ public function type() : EstimatorType { return EstimatorType::classifier(); } /** * Return the data types that the estimator is compatible with. * * @internal * * @return list<\Rubix\ML\DataType> */ public function compatibility() : array { return $this->tree->kernel()->compatibility(); } /** * Return the settings of the hyper-parameters in an associative array. * * @internal * * @return mixed[] */ public function params() : array { return [ 'k' => $this->k, 'weighted' => $this->weighted, 'tree' => $this->tree, ]; } /** * Has the learner been trained? * * @return bool */ public function trained() : bool { return !$this->tree->bare(); } /** * Return the base spatial tree instance. * * @return Spatial */ public function tree() : Spatial { return $this->tree; } /** * Train the learner with a dataset. * * @param \Rubix\ML\Datasets\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 = array_fill_keys($dataset->possibleOutcomes(), 0.0); $this->featureCount = $dataset->numFeatures(); $this->tree->grow($dataset); } /** * Make predictions from a dataset. * * @param Dataset $dataset * @throws RuntimeException * @return list */ public function predict(Dataset $dataset) : array { if ($this->tree->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 { [$samples, $labels, $distances] = $this->tree->nearest($sample, $this->k); if ($this->weighted) { $weights = array_fill_keys($labels, 0.0); foreach ($labels as $i => $label) { $weights[$label] += 1.0 / (1.0 + $distances[$i]); } } else { $weights = array_count_values($labels); } /** @var array $weights */ return argmax($weights); } /** * Estimate the joint probabilities for each possible outcome. * * @param Dataset $dataset * @throws RuntimeException * @return list> */ public function proba(Dataset $dataset) : array { if ($this->tree->bare() or !$this->classes or !$this->featureCount) { throw new RuntimeException('Estimator has not been trained.'); } DatasetHasDimensionality::with($dataset, $this->featureCount)->check(); return array_map([$this, 'probaSample'], $dataset->samples()); } /** * Predict the probabilities of a single sample and return the joint distribution. * * @internal * * @param list $sample * @return float[] */ public function probaSample(array $sample) : array { [$samples, $labels, $distances] = $this->tree->nearest($sample, $this->k); if ($this->weighted) { $weights = array_fill_keys($labels, 0.0); foreach ($labels as $i => $label) { $weights[$label] += 1.0 / (1.0 + $distances[$i]); } } else { $weights = array_count_values($labels); } $total = array_sum($weights); $dist = $this->classes; foreach ($weights as $class => $weight) { $dist[$class] = $weight / $total; } return $dist; } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'K-d Neighbors (' . Params::stringify($this->params()) . ')'; } }