mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-23 23:28:24 +00:00
DBSCAN fix for associative keys and array_merge performance optimization (#139)
This commit is contained in:
parent
61d2b7d115
commit
b48b82bd34
@ -94,17 +94,19 @@ class DBSCAN implements Clusterer
|
||||
{
|
||||
$cluster = [];
|
||||
|
||||
$clusterMerge = [[]];
|
||||
foreach ($samples as $index => $sample) {
|
||||
if (!isset($visited[$index])) {
|
||||
$visited[$index] = true;
|
||||
$regionSamples = $this->getSamplesInRegion($sample, $samples);
|
||||
if (count($regionSamples) > $this->minSamples) {
|
||||
$cluster = array_merge($regionSamples, $cluster);
|
||||
$clusterMerge[] = $regionSamples;
|
||||
}
|
||||
}
|
||||
|
||||
$cluster[] = $sample;
|
||||
$cluster[$index] = $sample;
|
||||
}
|
||||
$cluster = \array_merge($cluster, ...$clusterMerge);
|
||||
|
||||
return $cluster;
|
||||
}
|
||||
|
@ -31,4 +31,17 @@ class DBSCANTest extends TestCase
|
||||
|
||||
$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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user