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
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace Phpml\Clustering;
|
namespace Phpml\Clustering;
|
||||||
|
|
||||||
interface Clusterer
|
interface Clusterer
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $samples
|
* @param array $samples
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function cluster(array $samples);
|
public function cluster(array $samples);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace Phpml\Clustering;
|
namespace Phpml\Clustering;
|
||||||
|
|
||||||
@ -24,8 +25,8 @@ class DBSCAN implements Clusterer
|
|||||||
private $distanceMetric;
|
private $distanceMetric;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param float $epsilon
|
* @param float $epsilon
|
||||||
* @param int $minSamples
|
* @param int $minSamples
|
||||||
* @param Distance $distanceMetric
|
* @param Distance $distanceMetric
|
||||||
*/
|
*/
|
||||||
public function __construct($epsilon = 0.5, $minSamples = 3, Distance $distanceMetric = null)
|
public function __construct($epsilon = 0.5, $minSamples = 3, Distance $distanceMetric = null)
|
||||||
@ -49,14 +50,14 @@ class DBSCAN implements Clusterer
|
|||||||
$clusters = [];
|
$clusters = [];
|
||||||
$visited = [];
|
$visited = [];
|
||||||
|
|
||||||
foreach($samples as $index => $sample) {
|
foreach ($samples as $index => $sample) {
|
||||||
if(isset($visited[$index])) {
|
if (isset($visited[$index])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$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) {
|
||||||
$clusters[] = $this->expandCluster($regionSamples, $visited);
|
$clusters[] = $this->expandCluster($regionSamples, $visited);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,11 +71,12 @@ class DBSCAN implements Clusterer
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function getSamplesInRegion($localSample, $samples) {
|
private function getSamplesInRegion($localSample, $samples)
|
||||||
|
{
|
||||||
$region = [];
|
$region = [];
|
||||||
|
|
||||||
foreach($samples as $index => $sample) {
|
foreach ($samples as $index => $sample) {
|
||||||
if($this->distanceMetric->distance($localSample, $sample) < $this->epsilon) {
|
if ($this->distanceMetric->distance($localSample, $sample) < $this->epsilon) {
|
||||||
$region[$index] = $sample;
|
$region[$index] = $sample;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,14 +90,15 @@ class DBSCAN implements Clusterer
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function expandCluster($samples, &$visited) {
|
private function expandCluster($samples, &$visited)
|
||||||
|
{
|
||||||
$cluster = [];
|
$cluster = [];
|
||||||
|
|
||||||
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);
|
$cluster = array_merge($regionSamples, $cluster);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,5 +108,4 @@ class DBSCAN implements Clusterer
|
|||||||
|
|
||||||
return $cluster;
|
return $cluster;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace tests\Clustering;
|
namespace tests\Clustering;
|
||||||
|
|
||||||
@ -7,14 +8,13 @@ use Phpml\Clustering\DBSCAN;
|
|||||||
|
|
||||||
class DBSCANTest extends \PHPUnit_Framework_TestCase
|
class DBSCANTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testDBSCANSamplesClustering()
|
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 = [
|
$clustered = [
|
||||||
[[1, 1], [1, 2], [2, 1]],
|
[[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);
|
$dbscan = new DBSCAN($epsilon = 2, $minSamples = 3);
|
||||||
@ -24,16 +24,15 @@ class DBSCANTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testDBSCANSamplesInCircleClustering()
|
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 = [
|
$clustered = [
|
||||||
[[1, 1],[1, -1],[-1, -1],[-1, 1]],
|
[[1, 1], [1, -1], [-1, -1], [-1, 1]],
|
||||||
[[6, 6],[5, 6],[7, 8],[7, 7]]
|
[[6, 6], [5, 6], [7, 8], [7, 7]],
|
||||||
];
|
];
|
||||||
|
|
||||||
$dbscan = new DBSCAN($epsilon = 3, $minSamples = 4);
|
$dbscan = new DBSCAN($epsilon = 3, $minSamples = 4);
|
||||||
|
|
||||||
$this->assertEquals($clustered, $dbscan->cluster($samples));
|
$this->assertEquals($clustered, $dbscan->cluster($samples));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user