**Note:** This learner is considered a *lazy* learner because it does the majority * of its computation during inference. For a fast spatial tree-accelerated version, see * KD Neighbors. * * @category Machine Learning * @package Rubix/ML * @author Andrew DalPino */ class KNearestNeighbors implements Estimator, Learner, Online, Probabilistic, Persistable { use AutotrackRevisions; /** * The number of neighbors to consider when making a prediction. * * @var int */ protected int $k; /** * Should we consider the distances of our nearest neighbors when making predictions? * * @var bool */ protected bool $weighted; /** * The distance function to use when computing the distances. * * @var Distance */ protected Distance $kernel; /** * The zero vector for the possible class outcomes. * * @var float[] */ protected array $classes = [ // ]; /** * The training samples. * * @var list> */ protected array $samples = [ // ]; /** * The training labels. * * @var string[] */ protected array $labels = [ // ]; /** * @param int $k * @param bool $weighted * @param Distance|null $kernel * @throws InvalidArgumentException */ public function __construct(int $k = 5, bool $weighted = false, ?Distance $kernel = null) { if ($k < 1) { throw new InvalidArgumentException('At least 1 neighbor' . " is required to make a prediction, $k given."); } $this->k = $k; $this->weighted = $weighted; $this->kernel = $kernel ?? new Euclidean(); } /** * 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->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, 'kernel' => $this->kernel, ]; } /** * Has the learner been trained? * * @return bool */ public function trained() : bool { return $this->samples and $this->labels; } /** * Train the learner with a dataset. * * @param \Rubix\ML\Datasets\Labeled $dataset */ public function train(Dataset $dataset) : void { $this->samples = $this->labels = []; $this->partial($dataset); } /** * Perform a partial train on the learner. * * @param \Rubix\ML\Datasets\Labeled $dataset */ public function partial(Dataset $dataset) : void { SpecificationChain::with([ new DatasetIsLabeled($dataset), new DatasetIsNotEmpty($dataset), new SamplesAreCompatibleWithEstimator($dataset, $this), new LabelsAreCompatibleWithLearner($dataset, $this), ])->check(); foreach ($dataset->possibleOutcomes() as $class) { if (!isset($this->classes[$class])) { $this->classes[$class] = 0.0; } } $this->samples = array_merge($this->samples, $dataset->samples()); $this->labels = array_merge($this->labels, $dataset->labels()); } /** * Make predictions from a dataset. * * @param Dataset $dataset * @throws RuntimeException * @return string[] */ public function predict(Dataset $dataset) : array { if (!$this->samples or !$this->labels) { throw new RuntimeException('Estimator has not been trained.'); } DatasetHasDimensionality::with($dataset, count(current($this->samples)))->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 { [$labels, $distances] = $this->nearest($sample); if ($this->weighted) { $weights = array_fill_keys($labels, 0.0); foreach ($distances as $i => $distance) { $weights[$labels[$i]] += 1.0 / (1.0 + $distance); } } 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->samples or !$this->labels or !$this->classes) { throw new RuntimeException('Estimator has not been trained.'); } DatasetHasDimensionality::with($dataset, count(current($this->samples)))->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 array */ public function probaSample(array $sample) : array { [$labels, $distances] = $this->nearest($sample); 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; } /** * Find the K nearest neighbors to the given sample vector using the brute force method. * * @param list $sample * @return array{list,list} */ protected function nearest(array $sample) : array { $distances = []; foreach ($this->samples as $neighbor) { $distances[] = $this->kernel->compute($sample, $neighbor); } asort($distances); $distances = array_slice($distances, 0, $this->k, true); $labels = array_intersect_key($this->labels, $distances); return [$labels, $distances]; } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'K Nearest Neighbors (' . Params::stringify($this->params()) . ')'; } }