radius = $radius; $this->weighted = $weighted; $this->outlierClass = $outlierClass; $this->tree = $tree ?? new BallTree(); } /** * 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 [ 'radius' => $this->radius, 'weighted' => $this->weighted, 'outlier class' => $this->outlierClass, '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 * @throws RuntimeException */ public function train(Dataset $dataset) : void { SpecificationChain::with([ new DatasetIsLabeled($dataset), new DatasetIsNotEmpty($dataset), new SamplesAreCompatibleWithEstimator($dataset, $this), new LabelsAreCompatibleWithLearner($dataset, $this), ])->check(); $classes = $dataset->possibleOutcomes(); if (in_array($this->outlierClass, $classes)) { throw new RuntimeException('Training set cannot contain' . ' labels of the outlier class.'); } $classes[] = $this->outlierClass; $this->classes = array_fill_keys($classes, 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->range($sample, $this->radius); if (empty($labels)) { return $this->outlierClass; } 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->range($sample, $this->radius); $dist = $this->classes; if (empty($labels)) { $dist[$this->outlierClass] = 1.0; return $dist; } 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); 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 'Radius Neighbors (' . Params::stringify($this->params()) . ')'; } }