medansoftware / kmeans-algorithm-php
Package info
github.com/medansoftware/KMeans-Algorithm-PHP
pkg:composer/medansoftware/kmeans-algorithm-php
Requires
- php: >=5.5
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-08-16 14:25:28 UTC
README
A PHP implementation of the K-Means clustering algorithm.
Requirements
- PHP 8.0 or higher
- Composer
Installation
Install the library using Composer:
composer require medansoftware/kmeans-algorithm-php
Usage
Include Composer's autoloader:
require 'vendor/autoload.php';
Example 1: Two-dimensional data
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>';
Example 2: One-dimensional data
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
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();
Iterations and Convergence
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();
Results
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();
References
Made with ❤️ + ☕ ~ Agung Dirgantara