mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-10 00:37:55 +00:00
cs-fixer
This commit is contained in:
parent
26be771668
commit
01a2499754
@ -1,16 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Clustering;
|
||||
|
||||
interface Clusterer
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function cluster(array $samples);
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Clustering;
|
||||
|
||||
@ -24,8 +25,8 @@ class DBSCAN implements Clusterer
|
||||
private $distanceMetric;
|
||||
|
||||
/**
|
||||
* @param float $epsilon
|
||||
* @param int $minSamples
|
||||
* @param float $epsilon
|
||||
* @param int $minSamples
|
||||
* @param Distance $distanceMetric
|
||||
*/
|
||||
public function __construct($epsilon = 0.5, $minSamples = 3, Distance $distanceMetric = null)
|
||||
@ -49,14 +50,14 @@ class DBSCAN implements Clusterer
|
||||
$clusters = [];
|
||||
$visited = [];
|
||||
|
||||
foreach($samples as $index => $sample) {
|
||||
if(isset($visited[$index])) {
|
||||
foreach ($samples as $index => $sample) {
|
||||
if (isset($visited[$index])) {
|
||||
continue;
|
||||
}
|
||||
$visited[$index] = true;
|
||||
|
||||
$regionSamples = $this->getSamplesInRegion($sample, $samples);
|
||||
if(count($regionSamples) >= $this->minSamples) {
|
||||
if (count($regionSamples) >= $this->minSamples) {
|
||||
$clusters[] = $this->expandCluster($regionSamples, $visited);
|
||||
}
|
||||
}
|
||||
@ -70,11 +71,12 @@ class DBSCAN implements Clusterer
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getSamplesInRegion($localSample, $samples) {
|
||||
private function getSamplesInRegion($localSample, $samples)
|
||||
{
|
||||
$region = [];
|
||||
|
||||
foreach($samples as $index => $sample) {
|
||||
if($this->distanceMetric->distance($localSample, $sample) < $this->epsilon) {
|
||||
foreach ($samples as $index => $sample) {
|
||||
if ($this->distanceMetric->distance($localSample, $sample) < $this->epsilon) {
|
||||
$region[$index] = $sample;
|
||||
}
|
||||
}
|
||||
@ -88,14 +90,15 @@ class DBSCAN implements Clusterer
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function expandCluster($samples, &$visited) {
|
||||
private function expandCluster($samples, &$visited)
|
||||
{
|
||||
$cluster = [];
|
||||
|
||||
foreach($samples as $index => $sample) {
|
||||
if(!isset($visited[$index])) {
|
||||
foreach ($samples as $index => $sample) {
|
||||
if (!isset($visited[$index])) {
|
||||
$visited[$index] = true;
|
||||
$regionSamples = $this->getSamplesInRegion($sample, $samples);
|
||||
if(count($regionSamples) > $this->minSamples) {
|
||||
if (count($regionSamples) > $this->minSamples) {
|
||||
$cluster = array_merge($regionSamples, $cluster);
|
||||
}
|
||||
}
|
||||
@ -105,5 +108,4 @@ class DBSCAN implements Clusterer
|
||||
|
||||
return $cluster;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Clustering;
|
||||
|
||||
@ -7,14 +8,13 @@ use Phpml\Clustering\DBSCAN;
|
||||
|
||||
class DBSCANTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testDBSCANSamplesClustering()
|
||||
{
|
||||
$samples = [[1, 1],[8, 7],[1, 2],[7, 8],[2, 1],[8, 9]];
|
||||
$samples = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];
|
||||
|
||||
$clustered = [
|
||||
[[1, 1], [1, 2], [2, 1]],
|
||||
[[8, 7], [7, 8], [8, 9]]
|
||||
[[8, 7], [7, 8], [8, 9]],
|
||||
];
|
||||
|
||||
$dbscan = new DBSCAN($epsilon = 2, $minSamples = 3);
|
||||
@ -24,16 +24,15 @@ class DBSCANTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testDBSCANSamplesInCircleClustering()
|
||||
{
|
||||
$samples = [[1, 1],[6, 6],[1, -1],[5, 6],[-1, -1],[7, 8],[-1, 1],[7, 7]];
|
||||
$samples = [[1, 1], [6, 6], [1, -1], [5, 6], [-1, -1], [7, 8], [-1, 1], [7, 7]];
|
||||
|
||||
$clustered = [
|
||||
[[1, 1],[1, -1],[-1, -1],[-1, 1]],
|
||||
[[6, 6],[5, 6],[7, 8],[7, 7]]
|
||||
[[1, 1], [1, -1], [-1, -1], [-1, 1]],
|
||||
[[6, 6], [5, 6], [7, 8], [7, 7]],
|
||||
];
|
||||
|
||||
$dbscan = new DBSCAN($epsilon = 3, $minSamples = 4);
|
||||
|
||||
$this->assertEquals($clustered, $dbscan->cluster($samples));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user