php-ml/src/Clustering/DBSCAN.php

121 lines
2.9 KiB
PHP
Raw Normal View History

2016-04-30 22:47:44 +00:00
<?php
2016-04-30 22:56:43 +00:00
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-04-30 22:47:44 +00:00
namespace Phpml\Clustering;
use Phpml\Math\Distance;
2016-04-30 22:56:03 +00:00
use Phpml\Math\Distance\Euclidean;
2016-04-30 22:47:44 +00:00
class DBSCAN implements Clusterer
{
private const NOISE = -1;
2016-04-30 22:47:44 +00:00
/**
* @var float
*/
private $epsilon;
/**
* @var int
*/
private $minSamples;
/**
* @var Distance
*/
private $distanceMetric;
public function __construct(float $epsilon = 0.5, int $minSamples = 3, ?Distance $distanceMetric = null)
2016-04-30 22:47:44 +00:00
{
if ($distanceMetric === null) {
2016-04-30 22:56:03 +00:00
$distanceMetric = new Euclidean();
}
2016-04-30 22:47:44 +00:00
$this->epsilon = $epsilon;
$this->minSamples = $minSamples;
2016-04-30 22:56:03 +00:00
$this->distanceMetric = $distanceMetric;
2016-04-30 22:47:44 +00:00
}
public function cluster(array $samples): array
2016-04-30 22:47:44 +00:00
{
$labels = [];
$n = 0;
2016-04-30 22:47:44 +00:00
2016-04-30 22:56:43 +00:00
foreach ($samples as $index => $sample) {
if (isset($labels[$index])) {
2016-04-30 22:47:44 +00:00
continue;
}
$neighborIndices = $this->getIndicesInRegion($sample, $samples);
if (count($neighborIndices) < $this->minSamples) {
$labels[$index] = self::NOISE;
2016-04-30 22:47:44 +00:00
continue;
2016-04-30 22:47:44 +00:00
}
$labels[$index] = $n;
$this->expandCluster($samples, $neighborIndices, $labels, $n);
++$n;
2016-04-30 22:47:44 +00:00
}
return $this->groupByCluster($samples, $labels, $n);
2016-04-30 22:47:44 +00:00
}
private function expandCluster(array $samples, array $seeds, array &$labels, int $n): void
2016-04-30 22:56:43 +00:00
{
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 = [];
2016-04-30 22:47:44 +00:00
2016-04-30 22:56:43 +00:00
foreach ($samples as $index => $sample) {
if ($this->distanceMetric->distance($center, $sample) < $this->epsilon) {
$indices[] = $index;
2016-04-30 22:47:44 +00:00
}
}
return $indices;
2016-04-30 22:47:44 +00:00
}
private function groupByCluster(array $samples, array $labels, int $n): array
2016-04-30 22:56:43 +00:00
{
$clusters = array_fill(0, $n, []);
2016-04-30 22:47:44 +00:00
2016-04-30 22:56:43 +00:00
foreach ($samples as $index => $sample) {
if ($labels[$index] !== self::NOISE) {
$clusters[$labels[$index]][$index] = $sample;
2016-04-30 22:47:44 +00:00
}
}
// Reindex (i.e. to 0, 1, 2, ...) integer indices for backword compatibility
foreach ($clusters as $index => $cluster) {
$clusters[$index] = array_merge($cluster, []);
}
2016-04-30 22:47:44 +00:00
return $clusters;
2016-04-30 22:47:44 +00:00
}
}