Skip to content

Commit

Permalink
KMeans associative clustering (#262)
Browse files Browse the repository at this point in the history
* KMeans associative clustering added

* fix travis error

* KMeans will return provided keys as point label if they are provided

* fix travis

* fix travis
  • Loading branch information
IcaVestica authored and akondas committed Mar 8, 2018
1 parent 0d80c78 commit af2d732
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
4 changes: 3 additions & 1 deletion docs/machine-learning/clustering/k-means.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ To divide the samples into clusters simply use `cluster` method. It's return the

```
$samples = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];
Or if you need to keep your indentifiers along with yours samples you can use array keys as labels.
$samples = [ 'Label1' => [1, 1], 'Label2' => [8, 7], 'Label3' => [1, 2]];
$kmeans = new KMeans(2);
$kmeans->cluster($samples);
// return [0=>[[1, 1], ...], 1=>[[8, 7], ...]]
// return [0=>[[1, 1], ...], 1=>[[8, 7], ...]] or [0=>['Label1' => [1, 1], 'Label3' => [1, 2], ...], 1=>['Label2' => [8, 7], ...]]
```

### Initialization methods
Expand Down
6 changes: 3 additions & 3 deletions src/Clustering/KMeans.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function __construct(int $clustersNumber, int $initialization = self::INI

public function cluster(array $samples): array
{
$space = new Space(count($samples[0]));
foreach ($samples as $sample) {
$space->addPoint($sample);
$space = new Space(count(reset($samples)));
foreach ($samples as $key => $sample) {
$space->addPoint($sample, $key);
}

$clusters = [];
Expand Down
6 changes: 5 additions & 1 deletion src/Clustering/KMeans/Cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public function getPoints(): array
{
$points = [];
foreach ($this->points as $point) {
$points[] = $point->toArray();
if (!empty($point->label)) {
$points[$point->label] = $point->toArray();
} else {
$points[] = $point->toArray();
}
}

return $points;
Expand Down
8 changes: 7 additions & 1 deletion src/Clustering/KMeans/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ class Point implements ArrayAccess
*/
protected $coordinates = [];

public function __construct(array $coordinates)
/**
* @var mixed
*/
protected $label;

public function __construct(array $coordinates, $label = null)
{
$this->dimension = count($coordinates);
$this->coordinates = $coordinates;
$this->label = $label;
}

public function toArray(): array
Expand Down
8 changes: 4 additions & 4 deletions src/Clustering/KMeans/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public function toArray(): array
return ['points' => $points];
}

public function newPoint(array $coordinates): Point
public function newPoint(array $coordinates, $label = null): Point
{
if (count($coordinates) != $this->dimension) {
throw new LogicException('('.implode(',', $coordinates).') is not a point of this space');
}

return new Point($coordinates);
return new Point($coordinates, $label);
}

/**
* @param null $data
*/
public function addPoint(array $coordinates, $data = null): void
public function addPoint(array $coordinates, $label = null, $data = null): void
{
$this->attach($this->newPoint($coordinates), $data);
$this->attach($this->newPoint($coordinates, $label), $data);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Clustering/KMeansTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ public function testKMeansSamplesClustering(): void
$this->assertCount(0, $samples);
}

public function testKMeansSamplesLabeledClustering(): void
{
$samples = [
'555' => [1, 1],
'666' => [8, 7],
'ABC' => [1, 2],
'DEF' => [7, 8],
668 => [2, 1],
[8, 9],
];

$kmeans = new KMeans(2);
$clusters = $kmeans->cluster($samples);

$this->assertCount(2, $clusters);

foreach ($samples as $index => $sample) {
if (in_array($sample, $clusters[0], true) || in_array($sample, $clusters[1], true)) {
$this->assertArrayHasKey($index, $clusters[0] + $clusters[1]);
unset($samples[$index]);
}
}

$this->assertCount(0, $samples);
}

public function testKMeansInitializationMethods(): void
{
$samples = [
Expand Down

0 comments on commit af2d732

Please sign in to comment.