2017-01-31 19:27:15 +00:00
|
|
|
<?php
|
2017-01-31 19:33:08 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Phpml\Clustering;
|
|
|
|
|
|
|
|
use Phpml\Clustering\KMeans\Cluster;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\Clustering\KMeans\Point;
|
2017-01-31 19:27:15 +00:00
|
|
|
use Phpml\Clustering\KMeans\Space;
|
2017-05-17 07:03:25 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2017-01-31 19:27:15 +00:00
|
|
|
use Phpml\Math\Distance\Euclidean;
|
|
|
|
|
|
|
|
class FuzzyCMeans implements Clusterer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $clustersNumber;
|
|
|
|
|
|
|
|
/**
|
2018-12-12 20:56:44 +00:00
|
|
|
* @var Cluster[]
|
2017-01-31 19:27:15 +00:00
|
|
|
*/
|
2018-02-16 06:25:24 +00:00
|
|
|
private $clusters = [];
|
2017-01-31 19:27:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Space
|
|
|
|
*/
|
|
|
|
private $space;
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
/**
|
2018-12-12 20:56:44 +00:00
|
|
|
* @var float[][]
|
2017-01-31 19:27:15 +00:00
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
private $membership = [];
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $fuzziness;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $epsilon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $maxIterations;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $sampleCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
private $samples = [];
|
2017-01-31 19:27:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function __construct(int $clustersNumber, float $fuzziness = 2.0, float $epsilon = 1e-2, int $maxIterations = 100)
|
|
|
|
{
|
|
|
|
if ($clustersNumber <= 0) {
|
2018-03-03 15:03:53 +00:00
|
|
|
throw new InvalidArgumentException('Invalid clusters number');
|
2017-01-31 19:27:15 +00:00
|
|
|
}
|
2017-11-22 21:16:10 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
$this->clustersNumber = $clustersNumber;
|
|
|
|
$this->fuzziness = $fuzziness;
|
|
|
|
$this->epsilon = $epsilon;
|
|
|
|
$this->maxIterations = $maxIterations;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getMembershipMatrix(): array
|
|
|
|
{
|
|
|
|
return $this->membership;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cluster(array $samples): array
|
|
|
|
{
|
|
|
|
// Initialize variables, clusters and membership matrix
|
|
|
|
$this->sampleCount = count($samples);
|
|
|
|
$this->samples = &$samples;
|
|
|
|
$this->space = new Space(count($samples[0]));
|
|
|
|
$this->initClusters();
|
|
|
|
|
|
|
|
// Our goal is minimizing the objective value while
|
|
|
|
// executing the clustering steps at a maximum number of iterations
|
|
|
|
$lastObjective = 0.0;
|
|
|
|
$iterations = 0;
|
|
|
|
do {
|
|
|
|
// Update the membership matrix and cluster centers, respectively
|
|
|
|
$this->updateMembershipMatrix();
|
|
|
|
$this->updateClusters();
|
|
|
|
|
|
|
|
// Calculate the new value of the objective function
|
|
|
|
$objectiveVal = $this->getObjective();
|
|
|
|
$difference = abs($lastObjective - $objectiveVal);
|
|
|
|
$lastObjective = $objectiveVal;
|
|
|
|
} while ($difference > $this->epsilon && $iterations++ <= $this->maxIterations);
|
|
|
|
|
|
|
|
// Attach (hard cluster) each data point to the nearest cluster
|
|
|
|
for ($k = 0; $k < $this->sampleCount; ++$k) {
|
|
|
|
$column = array_column($this->membership, $k);
|
|
|
|
arsort($column);
|
|
|
|
reset($column);
|
2018-10-28 06:44:52 +00:00
|
|
|
$cluster = $this->clusters[key($column)];
|
2017-11-22 21:16:10 +00:00
|
|
|
$cluster->attach(new Point($this->samples[$k]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return grouped samples
|
|
|
|
$grouped = [];
|
|
|
|
foreach ($this->clusters as $cluster) {
|
|
|
|
$grouped[] = $cluster->getPoints();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $grouped;
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function initClusters(): void
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
|
|
|
// Membership array is a matrix of cluster number by sample counts
|
|
|
|
// We initilize the membership array with random values
|
|
|
|
$dim = $this->space->getDimension();
|
|
|
|
$this->generateRandomMembership($dim, $this->sampleCount);
|
|
|
|
$this->updateClusters();
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function generateRandomMembership(int $rows, int $cols): void
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
|
|
|
$this->membership = [];
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($i = 0; $i < $rows; ++$i) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$row = [];
|
|
|
|
$total = 0.0;
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($k = 0; $k < $cols; ++$k) {
|
2017-11-22 21:16:10 +00:00
|
|
|
$val = random_int(1, 5) / 10.0;
|
2017-01-31 19:27:15 +00:00
|
|
|
$row[] = $val;
|
|
|
|
$total += $val;
|
|
|
|
}
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
$this->membership[] = array_map(function ($val) use ($total) {
|
|
|
|
return $val / $total;
|
|
|
|
}, $row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function updateClusters(): void
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
|
|
|
$dim = $this->space->getDimension();
|
2018-10-28 06:44:52 +00:00
|
|
|
if (count($this->clusters) === 0) {
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($i = 0; $i < $this->clustersNumber; ++$i) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$this->clusters[] = new Cluster($this->space, array_fill(0, $dim, 0.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($i = 0; $i < $this->clustersNumber; ++$i) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$cluster = $this->clusters[$i];
|
|
|
|
$center = $cluster->getCoordinates();
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($k = 0; $k < $dim; ++$k) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$a = $this->getMembershipRowTotal($i, $k, true);
|
|
|
|
$b = $this->getMembershipRowTotal($i, $k, false);
|
|
|
|
$center[$k] = $a / $b;
|
|
|
|
}
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
$cluster->setCoordinates($center);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
protected function getMembershipRowTotal(int $row, int $col, bool $multiply): float
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
|
|
|
$sum = 0.0;
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($k = 0; $k < $this->sampleCount; ++$k) {
|
2018-10-28 06:44:52 +00:00
|
|
|
$val = $this->membership[$row][$k] ** $this->fuzziness;
|
2017-01-31 19:27:15 +00:00
|
|
|
if ($multiply) {
|
|
|
|
$val *= $this->samples[$k][$col];
|
|
|
|
}
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
$sum += $val;
|
|
|
|
}
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
return $sum;
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
protected function updateMembershipMatrix(): void
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($i = 0; $i < $this->clustersNumber; ++$i) {
|
|
|
|
for ($k = 0; $k < $this->sampleCount; ++$k) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$distCalc = $this->getDistanceCalc($i, $k);
|
|
|
|
$this->membership[$i][$k] = 1.0 / $distCalc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
protected function getDistanceCalc(int $row, int $col): float
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
|
|
|
$sum = 0.0;
|
|
|
|
$distance = new Euclidean();
|
|
|
|
$dist1 = $distance->distance(
|
2017-11-22 21:16:10 +00:00
|
|
|
$this->clusters[$row]->getCoordinates(),
|
|
|
|
$this->samples[$col]
|
2017-05-17 07:03:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
for ($j = 0; $j < $this->clustersNumber; ++$j) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$dist2 = $distance->distance(
|
|
|
|
$this->clusters[$j]->getCoordinates(),
|
2017-05-17 07:03:25 +00:00
|
|
|
$this->samples[$col]
|
|
|
|
);
|
|
|
|
|
2019-03-25 13:55:14 +00:00
|
|
|
$val = (($dist1 / $dist2) ** 2.0) / ($this->fuzziness - 1);
|
2017-01-31 19:27:15 +00:00
|
|
|
$sum += $val;
|
|
|
|
}
|
2017-08-17 06:50:37 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
return $sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The objective is to minimize the distance between all data points
|
|
|
|
* and all cluster centers. This method returns the summation of all
|
|
|
|
* these distances
|
|
|
|
*/
|
2018-10-28 06:44:52 +00:00
|
|
|
protected function getObjective(): float
|
2017-01-31 19:27:15 +00:00
|
|
|
{
|
|
|
|
$sum = 0.0;
|
|
|
|
$distance = new Euclidean();
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($i = 0; $i < $this->clustersNumber; ++$i) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$clust = $this->clusters[$i]->getCoordinates();
|
2017-05-17 07:03:25 +00:00
|
|
|
for ($k = 0; $k < $this->sampleCount; ++$k) {
|
2017-01-31 19:27:15 +00:00
|
|
|
$point = $this->samples[$k];
|
|
|
|
$sum += $distance->distance($clust, $point);
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 07:03:25 +00:00
|
|
|
|
2017-01-31 19:27:15 +00:00
|
|
|
return $sum;
|
|
|
|
}
|
|
|
|
}
|