This commit is contained in:
Arkadiusz Kondas 2016-05-01 00:56:43 +02:00
parent 26be771668
commit 01a2499754
3 changed files with 25 additions and 25 deletions

View File

@ -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);
} }

View File

@ -1,4 +1,5 @@
<?php <?php
declare (strict_types = 1); declare (strict_types = 1);
namespace Phpml\Clustering; namespace Phpml\Clustering;
@ -70,7 +71,8 @@ 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) {
@ -88,7 +90,8 @@ 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) {
@ -105,5 +108,4 @@ class DBSCAN implements Clusterer
return $cluster; return $cluster;
} }
} }

View File

@ -1,4 +1,5 @@
<?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);
@ -28,12 +28,11 @@ class DBSCANTest extends \PHPUnit_Framework_TestCase
$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));
} }
} }