Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
50.00% |
5 / 10 |
CRAP | |
67.74% |
21 / 31 |
| Cluster | |
0.00% |
0 / 1 |
|
50.00% |
5 / 10 |
24.59 | |
67.74% |
21 / 31 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| getPoints | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| toArray | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| attach | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 4 |
|||
| detach | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| attachAll | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| detachAll | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| updateCentroid | |
0.00% |
0 / 1 |
5.03 | |
88.89% |
8 / 9 |
|||
| getIterator | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| count | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| <?php | |
| declare (strict_types = 1); | |
| namespace Phpml\Clustering\KMeans; | |
| use IteratorAggregate; | |
| use Countable; | |
| use SplObjectStorage; | |
| use LogicException; | |
| class Cluster extends Point implements IteratorAggregate, Countable | |
| { | |
| /** | |
| * @var Space | |
| */ | |
| protected $space; | |
| /** | |
| * @var SplObjectStorage|Point[] | |
| */ | |
| protected $points; | |
| /** | |
| * @param Space $space | |
| * @param array $coordinates | |
| */ | |
| public function __construct(Space $space, array $coordinates) | |
| { | |
| parent::__construct($coordinates); | |
| $this->space = $space; | |
| $this->points = new SplObjectStorage(); | |
| } | |
| /** | |
| * @return array | |
| */ | |
| public function getPoints() | |
| { | |
| $points = []; | |
| foreach ($this->points as $point) { | |
| $points[] = $point->toArray(); | |
| } | |
| return $points; | |
| } | |
| /** | |
| * @return array | |
| */ | |
| public function toArray() | |
| { | |
| return array( | |
| 'centroid' => parent::toArray(), | |
| 'points' => $this->getPoints(), | |
| ); | |
| } | |
| /** | |
| * @param Point $point | |
| * | |
| * @return Point | |
| */ | |
| public function attach(Point $point) | |
| { | |
| if ($point instanceof self) { | |
| throw new LogicException('cannot attach a cluster to another'); | |
| } | |
| $this->points->attach($point); | |
| return $point; | |
| } | |
| /** | |
| * @param Point $point | |
| * | |
| * @return Point | |
| */ | |
| public function detach(Point $point) | |
| { | |
| $this->points->detach($point); | |
| return $point; | |
| } | |
| /** | |
| * @param SplObjectStorage $points | |
| */ | |
| public function attachAll(SplObjectStorage $points) | |
| { | |
| $this->points->addAll($points); | |
| } | |
| /** | |
| * @param SplObjectStorage $points | |
| */ | |
| public function detachAll(SplObjectStorage $points) | |
| { | |
| $this->points->removeAll($points); | |
| } | |
| public function updateCentroid() | |
| { | |
| if (!$count = count($this->points)) { | |
| return; | |
| } | |
| $centroid = $this->space->newPoint(array_fill(0, $this->dimension, 0)); | |
| foreach ($this->points as $point) { | |
| for ($n = 0; $n < $this->dimension; ++$n) { | |
| $centroid->coordinates[$n] += $point->coordinates[$n]; | |
| } | |
| } | |
| for ($n = 0; $n < $this->dimension; ++$n) { | |
| $this->coordinates[$n] = $centroid->coordinates[$n] / $count; | |
| } | |
| } | |
| /** | |
| * @return Point[]|SplObjectStorage | |
| */ | |
| public function getIterator() | |
| { | |
| return $this->points; | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| public function count() | |
| { | |
| return count($this->points); | |
| } | |
| } |