From f5494f4b319532155f796f6a67d13c35f5e56623 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 31 Oct 2019 21:16:33 +0100 Subject: [PATCH] hopefully fixed devision by 0 --- Algorithm/Clustering/Kmeans.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Algorithm/Clustering/Kmeans.php b/Algorithm/Clustering/Kmeans.php index afd245879..97ef0d2f7 100644 --- a/Algorithm/Clustering/Kmeans.php +++ b/Algorithm/Clustering/Kmeans.php @@ -91,11 +91,11 @@ final class Kmeans * * @param PointInterface $point Point to find the cluster for * - * @return PointInterface + * @return null|PointInterface * * @since 1.0.0 */ - public function cluster(PointInterface $point) : PointInterface + public function cluster(PointInterface $point) : ?PointInterface { $bestCluster = null; $bestDistance = \PHP_FLOAT_MAX; @@ -132,7 +132,7 @@ final class Kmeans $center->setCoordinate($i, 0); } - $center->setGroup(0); + //$center->setGroup(0); done because of bug below?! } foreach ($points as $point) { @@ -149,6 +149,7 @@ final class Kmeans foreach ($clusterCenters as $center) { for ($i = 0; $i < $coordinates; ++$i) { + // todo: here is a bug sometimes center->getGroup() is 0 $center->setCoordinate($i, $center->getCoordinate($i) / $center->getGroup()); } }