php-ml/src/Phpml/Clustering/KMeans.php

61 lines
1.3 KiB
PHP
Raw Normal View History

2016-05-01 21:17:09 +00:00
<?php
2016-05-01 21:36:33 +00:00
declare (strict_types = 1);
2016-05-01 21:17:09 +00:00
namespace Phpml\Clustering;
use Phpml\Clustering\KMeans\Space;
use Phpml\Exception\InvalidArgumentException;
class KMeans implements Clusterer
{
2016-05-01 21:36:33 +00:00
const INIT_RANDOM = 1;
const INIT_KMEANS_PLUS_PLUS = 2;
2016-05-01 21:17:09 +00:00
/**
* @var int
*/
private $clustersNumber;
2016-05-01 21:36:33 +00:00
/**
* @var int
*/
private $initialization;
2016-05-01 21:17:09 +00:00
/**
* @param int $clustersNumber
2016-05-01 21:36:33 +00:00
* @param int $initialization
2016-05-01 21:17:09 +00:00
*
* @throws InvalidArgumentException
*/
2016-05-01 21:36:33 +00:00
public function __construct(int $clustersNumber, int $initialization = self::INIT_KMEANS_PLUS_PLUS)
2016-05-01 21:17:09 +00:00
{
2016-05-01 21:36:33 +00:00
if ($clustersNumber <= 0) {
2016-05-01 21:17:09 +00:00
throw InvalidArgumentException::invalidClustersNumber();
}
2016-05-01 21:36:33 +00:00
2016-05-01 21:17:09 +00:00
$this->clustersNumber = $clustersNumber;
2016-05-01 21:36:33 +00:00
$this->initialization = $initialization;
2016-05-01 21:17:09 +00:00
}
/**
* @param array $samples
*
* @return array
*/
public function cluster(array $samples)
{
$space = new Space(count($samples[0]));
foreach ($samples as $sample) {
$space->addPoint($sample);
}
2016-05-01 21:36:33 +00:00
2016-05-01 21:17:09 +00:00
$clusters = [];
2016-05-01 21:36:33 +00:00
foreach ($space->solve($this->clustersNumber, $this->initialization) as $cluster) {
2016-05-01 21:17:09 +00:00
$clusters[] = $cluster->getPoints();
}
2016-05-01 21:36:33 +00:00
2016-05-01 21:17:09 +00:00
return $clusters;
}
}