2016-05-01 21:17:09 +00:00
|
|
|
<?php
|
2016-05-01 21:36:33 +00:00
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-05-01 21:17:09 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Clustering;
|
2016-05-01 21:17:09 +00:00
|
|
|
|
|
|
|
use Phpml\Clustering\KMeans;
|
2017-11-28 07:00:13 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-05-01 21:17:09 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class KMeansTest extends TestCase
|
2016-05-01 21:17:09 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testKMeansSamplesClustering(): void
|
2016-05-01 21:17:09 +00:00
|
|
|
{
|
|
|
|
$samples = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];
|
|
|
|
|
|
|
|
$kmeans = new KMeans(2);
|
|
|
|
$clusters = $kmeans->cluster($samples);
|
|
|
|
|
2016-12-12 18:31:30 +00:00
|
|
|
$this->assertCount(2, $clusters);
|
2016-05-01 21:17:09 +00:00
|
|
|
|
|
|
|
foreach ($samples as $index => $sample) {
|
2018-02-16 06:25:24 +00:00
|
|
|
if (in_array($sample, $clusters[0], true) || in_array($sample, $clusters[1], true)) {
|
2016-05-01 21:17:09 +00:00
|
|
|
unset($samples[$index]);
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 21:16:10 +00:00
|
|
|
|
2016-12-12 18:31:30 +00:00
|
|
|
$this->assertCount(0, $samples);
|
2016-05-01 21:17:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testKMeansInitializationMethods(): void
|
2016-05-01 21:17:09 +00:00
|
|
|
{
|
|
|
|
$samples = [
|
2016-05-01 21:44:04 +00:00
|
|
|
[180, 155], [186, 159], [119, 185], [141, 147], [157, 158],
|
|
|
|
[176, 122], [194, 160], [113, 193], [190, 148], [152, 154],
|
|
|
|
[162, 146], [188, 144], [185, 124], [163, 114], [151, 140],
|
|
|
|
[175, 131], [186, 162], [181, 195], [147, 122], [143, 195],
|
|
|
|
[171, 119], [117, 165], [169, 121], [159, 160], [159, 112],
|
|
|
|
[115, 122], [149, 193], [156, 135], [118, 120], [139, 159],
|
|
|
|
[150, 115], [181, 136], [167, 162], [132, 115], [175, 165],
|
|
|
|
[110, 147], [175, 118], [113, 145], [130, 162], [195, 179],
|
|
|
|
[164, 111], [192, 114], [194, 149], [139, 113], [160, 168],
|
|
|
|
[162, 110], [174, 144], [137, 142], [197, 160], [147, 173],
|
2016-05-01 21:17:09 +00:00
|
|
|
];
|
|
|
|
|
2016-05-01 21:44:04 +00:00
|
|
|
$kmeans = new KMeans(4, KMeans::INIT_KMEANS_PLUS_PLUS);
|
2016-05-01 21:17:09 +00:00
|
|
|
$clusters = $kmeans->cluster($samples);
|
2016-12-12 18:31:30 +00:00
|
|
|
$this->assertCount(4, $clusters);
|
2016-05-01 21:17:09 +00:00
|
|
|
|
2016-05-01 21:44:04 +00:00
|
|
|
$kmeans = new KMeans(4, KMeans::INIT_RANDOM);
|
|
|
|
$clusters = $kmeans->cluster($samples);
|
2016-12-12 18:31:30 +00:00
|
|
|
$this->assertCount(4, $clusters);
|
2016-05-01 21:17:09 +00:00
|
|
|
}
|
2016-07-24 12:01:17 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidClusterNumber(): void
|
2016-07-24 12:01:17 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2016-07-24 12:01:17 +00:00
|
|
|
new KMeans(0);
|
|
|
|
}
|
2016-05-01 21:17:09 +00:00
|
|
|
}
|