diff --git a/src/Phpml/Clustering/KMeans/Space.php b/src/Phpml/Clustering/KMeans/Space.php index 2904e2f..89a0d09 100644 --- a/src/Phpml/Clustering/KMeans/Space.php +++ b/src/Phpml/Clustering/KMeans/Space.php @@ -61,7 +61,7 @@ class Space extends SplObjectStorage */ public function addPoint(array $coordinates, $data = null) { - return $this->attach($this->newPoint($coordinates), $data); + $this->attach($this->newPoint($coordinates), $data); } /** @@ -74,7 +74,7 @@ class Space extends SplObjectStorage throw new InvalidArgumentException('can only attach points to spaces'); } - return parent::attach($point, $data); + parent::attach($point, $data); } /** @@ -230,8 +230,8 @@ class Space extends SplObjectStorage protected function initializeKMPPClusters(int $clustersNumber) { $clusters = []; - $position = rand(1, count($this)); - for ($i = 1, $this->rewind(); $i < $position && $this->valid(); $i++, $this->next()); + $this->rewind(); + $clusters[] = new Cluster($this, $this->current()->getCoordinates()); $distances = new SplObjectStorage(); diff --git a/src/Phpml/Math/Matrix.php b/src/Phpml/Math/Matrix.php index 208b10d..808472c 100644 --- a/src/Phpml/Math/Matrix.php +++ b/src/Phpml/Math/Matrix.php @@ -193,7 +193,8 @@ class Matrix $product = []; $multiplier = $matrix->toArray(); for ($i = 0; $i < $this->rows; ++$i) { - for ($j = 0; $j < $matrix->getColumns(); ++$j) { + $columns = $matrix->getColumns(); + for ($j = 0; $j < $columns; ++$j) { $product[$i][$j] = 0; for ($k = 0; $k < $this->columns; ++$k) { $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j];