DBSCAN fix for associative keys and array_merge performance optimization (#139)

This commit is contained in:
Maxim Kasatkin 2017-10-18 15:59:37 +07:00 committed by Arkadiusz Kondas
parent 61d2b7d115
commit b48b82bd34
2 changed files with 17 additions and 2 deletions

View File

@ -94,17 +94,19 @@ class DBSCAN implements Clusterer
{ {
$cluster = []; $cluster = [];
$clusterMerge = [[]];
foreach ($samples as $index => $sample) { foreach ($samples as $index => $sample) {
if (!isset($visited[$index])) { if (!isset($visited[$index])) {
$visited[$index] = true; $visited[$index] = true;
$regionSamples = $this->getSamplesInRegion($sample, $samples); $regionSamples = $this->getSamplesInRegion($sample, $samples);
if (count($regionSamples) > $this->minSamples) { if (count($regionSamples) > $this->minSamples) {
$cluster = array_merge($regionSamples, $cluster); $clusterMerge[] = $regionSamples;
} }
} }
$cluster[] = $sample; $cluster[$index] = $sample;
} }
$cluster = \array_merge($cluster, ...$clusterMerge);
return $cluster; return $cluster;
} }

View File

@ -31,4 +31,17 @@ class DBSCANTest extends TestCase
$this->assertEquals($clustered, $dbscan->cluster($samples)); $this->assertEquals($clustered, $dbscan->cluster($samples));
} }
public function testDBSCANSamplesClusteringAssociative()
{
$samples = ['a' => [1, 1], 'b' => [9, 9], 'c' => [1, 2], 'd' => [9, 8], 'e' => [7, 7], 'f' => [8, 7]];
$clustered = [
['a' => [1, 1], 'c' => [1, 2]],
['b' => [9, 9], 'd' => [9, 8], 'e' => [7, 7], 'f' => [8, 7]],
];
$dbscan = new DBSCAN($epsilon = 3, $minSamples = 2);
$this->assertEquals($clustered, $dbscan->cluster($samples));
}
} }