1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Phpml\Clustering; |
6
|
|
|
|
7
|
|
|
use Phpml\Math\Distance; |
8
|
|
|
use Phpml\Math\Distance\Euclidean; |
9
|
|
|
|
10
|
|
|
class DBSCAN implements Clusterer |
11
|
|
|
{ |
12
|
|
|
private const NOISE = -1; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var float |
16
|
|
|
*/ |
17
|
|
|
private $epsilon; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private $minSamples; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Distance |
26
|
|
|
*/ |
27
|
|
|
private $distanceMetric; |
28
|
|
|
|
29
|
|
|
public function __construct(float $epsilon = 0.5, int $minSamples = 3, ?Distance $distanceMetric = null) |
30
|
|
|
{ |
31
|
|
|
if ($distanceMetric === null) { |
32
|
|
|
$distanceMetric = new Euclidean(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->epsilon = $epsilon; |
36
|
|
|
$this->minSamples = $minSamples; |
37
|
|
|
$this->distanceMetric = $distanceMetric; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function cluster(array $samples): array |
41
|
|
|
{ |
42
|
|
|
$labels = []; |
43
|
|
|
$n = 0; |
44
|
|
|
|
45
|
|
|
foreach ($samples as $index => $sample) { |
46
|
|
|
if (isset($labels[$index])) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$neighborIndices = $this->getIndicesInRegion($sample, $samples); |
51
|
|
|
|
52
|
|
|
if (count($neighborIndices) < $this->minSamples) { |
53
|
|
|
$labels[$index] = self::NOISE; |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$labels[$index] = $n; |
58
|
|
|
|
59
|
|
|
$this->expandCluster($samples, $neighborIndices, $labels, $n); |
60
|
|
|
|
61
|
|
|
++$n; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->groupByCluster($samples, $labels, $n); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function expandCluster(array $samples, array $seeds, array &$labels, int $n): void |
68
|
|
|
{ |
69
|
|
|
while (($index = array_pop($seeds)) !== null) { |
70
|
|
|
if (isset($labels[$index])) { |
71
|
|
|
if ($labels[$index] === self::NOISE) { |
72
|
|
|
$labels[$index] = $n; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$labels[$index] = $n; |
79
|
|
|
|
80
|
|
|
$sample = $samples[$index]; |
81
|
|
|
$neighborIndices = $this->getIndicesInRegion($sample, $samples); |
82
|
|
|
|
83
|
|
|
if (count($neighborIndices) >= $this->minSamples) { |
84
|
|
|
$seeds = array_unique(array_merge($seeds, $neighborIndices)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getIndicesInRegion(array $center, array $samples): array |
90
|
|
|
{ |
91
|
|
|
$indices = []; |
92
|
|
|
|
93
|
|
|
foreach ($samples as $index => $sample) { |
94
|
|
|
if ($this->distanceMetric->distance($center, $sample) < $this->epsilon) { |
95
|
|
|
$indices[] = $index; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $indices; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function groupByCluster(array $samples, array $labels, int $n): array |
103
|
|
|
{ |
104
|
|
|
$clusters = array_fill(0, $n, []); |
105
|
|
|
|
106
|
|
|
foreach ($samples as $index => $sample) { |
107
|
|
|
if ($labels[$index] !== self::NOISE) { |
108
|
|
|
$clusters[$labels[$index]][$index] = $sample; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Reindex (i.e. to 0, 1, 2, ...) integer indices for backword compatibility |
113
|
|
|
foreach ($clusters as $index => $cluster) { |
114
|
|
|
$clusters[$index] = array_merge($cluster); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $clusters; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|