mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-11 08:10:56 +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 = [];
|
$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;
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user