Skip to content

Commit

Permalink
Rewrite DBSCAN (#185)
Browse files Browse the repository at this point in the history
* Add testcases to DBSCAN

* Fix DBSCAN implementation

* Refactoring DBSCAN implementation

* Fix coding style
  • Loading branch information
y-uti authored and akondas committed Jan 9, 2018
1 parent 5a69163 commit 9938cf2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 27 deletions.
80 changes: 53 additions & 27 deletions src/Phpml/Clustering/DBSCAN.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace Phpml\Clustering;

use array_merge;
use Phpml\Math\Distance;
use Phpml\Math\Distance\Euclidean;

class DBSCAN implements Clusterer
{
private const NOISE = -1;

/**
* @var float
*/
Expand Down Expand Up @@ -38,57 +39,82 @@ public function __construct(float $epsilon = 0.5, int $minSamples = 3, ?Distance

public function cluster(array $samples): array
{
$clusters = [];
$visited = [];
$labels = [];
$n = 0;

foreach ($samples as $index => $sample) {
if (isset($visited[$index])) {
if (isset($labels[$index])) {
continue;
}

$visited[$index] = true;
$neighborIndices = $this->getIndicesInRegion($sample, $samples);

if (count($neighborIndices) < $this->minSamples) {
$labels[$index] = self::NOISE;

$regionSamples = $this->getSamplesInRegion($sample, $samples);
if (count($regionSamples) >= $this->minSamples) {
$clusters[] = $this->expandCluster($regionSamples, $visited);
continue;
}

$labels[$index] = $n;

$this->expandCluster($samples, $neighborIndices, $labels, $n);

++$n;
}

return $clusters;
return $this->groupByCluster($samples, $labels, $n);
}

private function getSamplesInRegion(array $localSample, array $samples): array
private function expandCluster(array $samples, array $seeds, array &$labels, int $n): void
{
$region = [];
while (($index = array_pop($seeds)) !== null) {
if (isset($labels[$index])) {
if ($labels[$index] === self::NOISE) {
$labels[$index] = $n;
}

continue;
}

$labels[$index] = $n;

$sample = $samples[$index];
$neighborIndices = $this->getIndicesInRegion($sample, $samples);

if (count($neighborIndices) >= $this->minSamples) {
$seeds = array_unique(array_merge($seeds, $neighborIndices));
}
}
}

private function getIndicesInRegion(array $center, array $samples): array
{
$indices = [];

foreach ($samples as $index => $sample) {
if ($this->distanceMetric->distance($localSample, $sample) < $this->epsilon) {
$region[$index] = $sample;
if ($this->distanceMetric->distance($center, $sample) < $this->epsilon) {
$indices[] = $index;
}
}

return $region;
return $indices;
}

private function expandCluster(array $samples, array &$visited): array
private function groupByCluster(array $samples, array $labels, int $n): array
{
$cluster = [];
$clusters = array_fill(0, $n, []);

$clusterMerge = [[]];
foreach ($samples as $index => $sample) {
if (!isset($visited[$index])) {
$visited[$index] = true;
$regionSamples = $this->getSamplesInRegion($sample, $samples);
if (count($regionSamples) > $this->minSamples) {
$clusterMerge[] = $regionSamples;
}
if ($labels[$index] !== self::NOISE) {
$clusters[$labels[$index]][$index] = $sample;
}

$cluster[$index] = $sample;
}

$cluster = array_merge($cluster, ...$clusterMerge);
// Reindex (i.e. to 0, 1, 2, ...) integer indices for backword compatibility
foreach ($clusters as $index => $cluster) {
$clusters[$index] = array_merge($cluster, []);
}

return $cluster;
return $clusters;
}
}
34 changes: 34 additions & 0 deletions tests/Phpml/Clustering/DBSCANTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,38 @@ public function testDBSCANSamplesClusteringAssociative(): void

$this->assertEquals($clustered, $dbscan->cluster($samples));
}

public function testClusterEpsilonSmall(): void
{
$samples = [[0], [1], [2]];
$clustered = [
];

$dbscan = new DBSCAN($epsilon = 0.5, $minSamples = 2);

$this->assertEquals($clustered, $dbscan->cluster($samples));
}

public function testClusterEpsilonBoundary(): void
{
$samples = [[0], [1], [2]];
$clustered = [
];

$dbscan = new DBSCAN($epsilon = 1.0, $minSamples = 2);

$this->assertEquals($clustered, $dbscan->cluster($samples));
}

public function testClusterEpsilonLarge(): void
{
$samples = [[0], [1], [2]];
$clustered = [
[[0], [1], [2]],
];

$dbscan = new DBSCAN($epsilon = 1.5, $minSamples = 2);

$this->assertEquals($clustered, $dbscan->cluster($samples));
}
}

0 comments on commit 9938cf2

Please sign in to comment.