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;
|
|
|
|
|
2016-05-01 21:36:33 +00:00
|
|
|
use Countable;
|
2017-11-06 07:56:37 +00:00
|
|
|
use IteratorAggregate;
|
2016-05-01 21:36:33 +00:00
|
|
|
use LogicException;
|
2017-11-06 07:56:37 +00:00
|
|
|
use SplObjectStorage;
|
2016-05-01 21:17:09 +00:00
|
|
|
|
|
|
|
class Cluster extends Point implements IteratorAggregate, Countable
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getPoints(): array
|
2016-05-01 21:36:33 +00:00
|
|
|
{
|
|
|
|
$points = [];
|
|
|
|
foreach ($this->points as $point) {
|
|
|
|
$points[] = $point->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $points;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function attach(Point $point): Point
|
2016-05-01 21:36:33 +00:00
|
|
|
{
|
|
|
|
if ($point instanceof self) {
|
|
|
|
throw new LogicException('cannot attach a cluster to another');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->points->attach($point);
|
|
|
|
|
|
|
|
return $point;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function detach(Point $point): Point
|
2016-05-01 21:36:33 +00:00
|
|
|
{
|
|
|
|
$this->points->detach($point);
|
|
|
|
|
|
|
|
return $point;
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function attachAll(SplObjectStorage $points): void
|
2016-05-01 21:36:33 +00:00
|
|
|
{
|
|
|
|
$this->points->addAll($points);
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function detachAll(SplObjectStorage $points): void
|
2016-05-01 21:36:33 +00:00
|
|
|
{
|
|
|
|
$this->points->removeAll($points);
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function updateCentroid(): void
|
2016-05-01 21:36:33 +00:00
|
|
|
{
|
2018-02-17 23:09:24 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->points);
|
|
|
|
}
|
2017-11-06 07:56:37 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
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
|
|
|
}
|