2016-04-30 22:47:44 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $epsilon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $minSamples;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Distance
|
|
|
|
*/
|
|
|
|
private $distanceMetric;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float $epsilon
|
|
|
|
* @param int $minSamples
|
2016-04-30 22:56:03 +00:00
|
|
|
* @param Distance $distanceMetric
|
2016-04-30 22:47:44 +00:00
|
|
|
*/
|
2016-04-30 22:56:03 +00:00
|
|
|
public function __construct($epsilon = 0.5, $minSamples = 3, Distance $distanceMetric = null)
|
2016-04-30 22:47:44 +00:00
|
|
|
{
|
2016-04-30 22:56:03 +00:00
|
|
|
if (null === $distanceMetric) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function cluster(array $samples)
|
|
|
|
{
|
|
|
|
$clusters = [];
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
foreach($samples as $index => $sample) {
|
|
|
|
if(isset($visited[$index])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$visited[$index] = true;
|
|
|
|
|
|
|
|
$regionSamples = $this->getSamplesInRegion($sample, $samples);
|
|
|
|
if(count($regionSamples) >= $this->minSamples) {
|
|
|
|
$clusters[] = $this->expandCluster($regionSamples, $visited);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clusters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $localSample
|
|
|
|
* @param array $samples
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getSamplesInRegion($localSample, $samples) {
|
|
|
|
$region = [];
|
|
|
|
|
|
|
|
foreach($samples as $index => $sample) {
|
|
|
|
if($this->distanceMetric->distance($localSample, $sample) < $this->epsilon) {
|
|
|
|
$region[$index] = $sample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $region;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
* @param array $visited
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function expandCluster($samples, &$visited) {
|
|
|
|
$cluster = [];
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cluster[] = $sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cluster;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|