> */ protected array $centroids; /** * The dimensionality of the predefined centroids. * * @var int */ protected $dimensions; /** * @param array<(string|int|float)[]> $centroids * @throws InvalidArgumentException */ public function __construct(array $centroids) { if (empty($centroids)) { throw new InvalidArgumentException('Number of centroids' . ' must be greater than 1, 0 given.'); } $dimensions = count(current($centroids)); $centroids = array_unique($centroids, SORT_REGULAR); foreach ($centroids as &$centroid) { if (count($centroid) !== $dimensions) { throw new InvalidArgumentException('Centroid must' . " have $dimensions dimensions, " . count($centroid) . ' given.'); } $centroid = array_values($centroid); } $this->centroids = array_values($centroids); $this->dimensions = $dimensions; } /** * Seed k cluster centroids from a dataset. * * @internal * * @param Dataset $dataset * @param int $k * @throws RuntimeException * @return list> */ public function seed(Dataset $dataset, int $k) : array { DatasetHasDimensionality::with($dataset, $this->dimensions)->check(); if ($k > count($this->centroids)) { throw new RuntimeException('Not enough unique' . " presets to generate $k centroids."); } return array_slice($this->centroids, 0, $k); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'Preset'; } }