A PHP implementation of the K-Means clustering algorithm.
- PHP 8.0 or higher
- Composer
Install the library using Composer:
composer require medansoftware/kmeans-algorithm-phpInclude Composer's autoloader:
require 'vendor/autoload.php';This example uses two attributes (A and B) and divides the data into two clusters.
<?php
require 'vendor/autoload.php';
$kmeans = new \Algorithm\KMeans;
$kmeans->setAttributes(array(
'A',
'B'
));
$kmeans->setDataFromArgs(1, 1);
$kmeans->setDataFromArgs(2, 1);
$kmeans->setDataFromArgs(4, 3);
$kmeans->setDataFromArgs(5, 4);
$kmeans->setClusterCount(2);
/*
* Use data points at indexes 0 and 1
* as the initial centroids.
*/
$kmeans->setCentroid(0, 1);
/*
* Run K-Means with a maximum of 100 iterations.
*/
$kmeans->setIteration(100);
$kmeans->run();
echo '<h2>Initial Centroids</h2>';
echo '<pre>';
print_r($kmeans->getInitialCentroid());
echo '</pre>';
echo '<h2>Final Centroids</h2>';
echo '<pre>';
print_r($kmeans->getCentroid());
echo '</pre>';
echo '<h2>Iterations</h2>';
echo 'Iterations executed: ' . $kmeans->countIterations();
echo '<h2>Results</h2>';
echo '<pre>';
print_r($kmeans->getAllResults());
echo '</pre>';This example uses a single attribute and divides the data into three clusters.
<?php
require 'vendor/autoload.php';
$kmeans = new \Algorithm\KMeans;
$kmeans->setAttributes(array(
'x'
));
$kmeans->setDataFromArgs(1);
$kmeans->setDataFromArgs(2);
$kmeans->setDataFromArgs(6);
$kmeans->setDataFromArgs(7);
$kmeans->setDataFromArgs(8);
$kmeans->setDataFromArgs(10);
$kmeans->setDataFromArgs(15);
$kmeans->setDataFromArgs(17);
$kmeans->setDataFromArgs(20);
$kmeans->setClusterCount(3);
/*
* Use data points at indexes 1, 5, and 7
* as the initial centroids.
*
* The selected values are 2, 10, and 17.
*/
$kmeans->setCentroid(1, 5, 7);
/*
* Run K-Means with a maximum of 100 iterations.
*/
$kmeans->setIteration(100);
$kmeans->run();
echo '<h2>Initial Centroids</h2>';
echo '<pre>';
print_r($kmeans->getInitialCentroid());
echo '</pre>';
echo '<h2>Final Centroids</h2>';
echo '<pre>';
print_r($kmeans->getCentroid());
echo '</pre>';
echo '<h2>Iterations</h2>';
echo 'Iterations executed: ' . $kmeans->countIterations();
echo '<h2>Logs</h2>';
echo '<pre>';
print_r($kmeans->catchLogs());
echo '</pre>';Initial centroids can be selected from existing data points by using their indexes:
$kmeans->setCentroid(0, 1);An array of indexes can also be used:
$kmeans->setCentroid(array(0, 1));Alternatively, centroid values can be provided directly:
$kmeans->setCentroid(array(
array('A' => 1, 'B' => 1),
array('A' => 5, 'B' => 4)
));If no centroids are provided, the library automatically selects distinct data points as initial centroids:
$kmeans->setCentroid();The maximum number of iterations can be configured with:
$kmeans->setIteration(100);The algorithm automatically stops when the centroids converge or when the maximum number of iterations is reached.
The number of iterations executed can be obtained using:
$kmeans->countIterations();You can check whether the algorithm has converged using:
$kmeans->isDone();Get the final centroids:
$kmeans->getCentroid();Get the initial centroids:
$kmeans->getInitialCentroid();Get the cluster logs:
$kmeans->getClusters();Get all logs:
$kmeans->catchLogs();Get all results:
$kmeans->getAllResults();Made with ❤️ + ☕ ~ Agung Dirgantara