php-ml/src/Clustering/KMeans/Cluster.php

118 lines
2.5 KiB
PHP
Raw Normal View History

2016-05-01 21:17:09 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-05-01 21:17:09 +00:00
namespace Phpml\Clustering\KMeans;
use IteratorAggregate;
2016-05-01 21:36:33 +00:00
use LogicException;
use SplObjectStorage;
2016-05-01 21:17:09 +00:00
2018-10-28 06:44:52 +00:00
class Cluster extends Point implements IteratorAggregate
2016-05-01 21:17:09 +00:00
{
2016-05-01 21:36:33 +00:00
/**
* @var Space
*/
protected $space;
/**
* @var SplObjectStorage|Point[]
*/
protected $points;
public function __construct(Space $space, array $coordinates)
{
parent::__construct($coordinates);
$this->space = $space;
$this->points = new SplObjectStorage();
}
public function getPoints(): array
2016-05-01 21:36:33 +00:00
{
$points = [];
foreach ($this->points as $point) {
if ($point->label === null) {
$points[] = $point->toArray();
2018-10-28 06:44:52 +00:00
} else {
$points[$point->label] = $point->toArray();
}
2016-05-01 21:36:33 +00:00
}
return $points;
}
public function toArray(): array
2016-05-01 21:36:33 +00:00
{
2017-01-31 19:33:08 +00:00
return [
2016-05-01 21:36:33 +00:00
'centroid' => parent::toArray(),
'points' => $this->getPoints(),
2017-01-31 19:33:08 +00:00
];
2016-05-01 21:36:33 +00:00
}
public function attach(Point $point): Point
2016-05-01 21:36:33 +00:00
{
if ($point instanceof self) {
2018-03-04 16:06:46 +00:00
throw new LogicException('Cannot attach a cluster to another');
2016-05-01 21:36:33 +00:00
}
$this->points->attach($point);
return $point;
}
public function detach(Point $point): Point
2016-05-01 21:36:33 +00:00
{
$this->points->detach($point);
return $point;
}
public function attachAll(SplObjectStorage $points): void
2016-05-01 21:36:33 +00:00
{
$this->points->addAll($points);
}
public function detachAll(SplObjectStorage $points): void
2016-05-01 21:36:33 +00:00
{
$this->points->removeAll($points);
}
public function updateCentroid(): void
2016-05-01 21:36:33 +00:00
{
$count = count($this->points);
if ($count === 0) {
2016-05-01 21:36:33 +00:00
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;
}
2018-10-28 06:44:52 +00:00
public function count(): int
2016-05-01 21:36:33 +00:00
{
return count($this->points);
}
public function setCoordinates(array $newCoordinates): void
2017-01-23 08:24:50 +00:00
{
$this->coordinates = $newCoordinates;
2017-01-31 19:33:08 +00:00
}
2016-05-01 21:17:09 +00:00
}