PHPackages                             crwgregory/php-kmeans - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. crwgregory/php-kmeans

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

crwgregory/php-kmeans
=====================

K-Means algorithm for PHP

1.0.0(10y ago)032LGPLPHPPHP &gt;=5.4.0

Since Dec 3Pushed 9y ago1 watchersCompare

[ Source](https://github.com/crwgregory/php-kmeans)[ Packagist](https://packagist.org/packages/crwgregory/php-kmeans)[ RSS](/packages/crwgregory-php-kmeans/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

PHP K-Means
===========

[](#php-k-means)

*Clustering made simple*

k-means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. k-means clustering aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. This results in a partitioning of the data space into Voronoi cells.

Read more on [Wikipedia](http://en.wikipedia.org/wiki/K-means_clustering)

PHP K-Means, like its name suggest, is an implementation of K-Means and K-Means++ algorithms for the PHP plateform. It works with an unlimited number of dimentions.

Usage
-----

[](#usage)

Given the following points of R²

```
$points = [
    [80,55],[86,59],[19,85],[41,47],[57,58],
	[76,22],[94,60],[13,93],[90,48],[52,54],
	[62,46],[88,44],[85,24],[63,14],[51,40],
	[75,31],[86,62],[81,95],[47,22],[43,95],
	[71,19],[17,65],[69,21],[59,60],[59,12],
	[15,22],[49,93],[56,35],[18,20],[39,59],
	[50,15],[81,36],[67,62],[32,15],[75,65],
	[10,47],[75,18],[13,45],[30,62],[95,79],
	[64,11],[92,14],[94,49],[39,13],[60,68],
	[62,10],[74,44],[37,42],[97,60],[47,73],
];
```

We want to find 3 clusters:

```
// create a 2 dimentionnal space and fill it
$space = new KMeans\Space(2);

foreach ($points as $point)
    $space->addPoint($point);

 // resolve 3 clusters
$clusters = $space->solve(3);
```

Now we can retrieve each cluster's centroid (the average meaning amongts its points) and all the points in it:

```
foreach ($clusters as $i => $cluster)
    printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster));
```

Example of output:

```
Cluster 0 [79,58]: 18 points
Cluster 1 [57,19]: 19 points
Cluster 2 [31,66]: 13 points

```

### Heads up!

[](#heads-up)

K-Means algorithm is non-deterministic so you may get different results when running it multiple times with the same data. The more points you add in the space, the more accurate the result will be.

You are strongly advised to read the Wikipedia article thoroughly before using this library.

K-Means++
---------

[](#k-means)

When triggering the `Kmeans\Space::solve` method, you may provide an alternative seeding method in order to initialize the clusters with the [David Arthur and Sergei Vassilvitskii algorithm](http://en.wikipedia.org/wiki/K-means%2B%2B) which avoids poor clustering results.

```
// resolve 3 clusters using David Arthur and Sergei Vassilvitskii seeding algorithm
$clusters = $space->solve(3, KMeans\Space::SEED_DASV);
```

Howto
-----

[](#howto)

### Get coordinates of a point/cluster:

[](#get-coordinates-of-a-pointcluster)

```
$x = $point[0];
$y = $point[1];

// or

list($x,$y) = $point->getCoordinates();
```

### List all points of a space/cluster:

[](#list-all-points-of-a-spacecluster)

```
foreach ($cluster as $point)
    printf('[%d,%d]', $point[0], $point[1]);
```

### Attach data to a point:

[](#attach-data-to-a-point)

```
$space->addPoint($coordinate, $data);
```

### Retrieve point data:

[](#retrieve-point-data)

```
$data = $space[$point];
```

### Watch the algorithm run

[](#watch-the-algorithm-run)

Each iteration step can be monitored using a callback function passed to `Kmeans\Space::solve`:

```
$clusters = $space->solve(3, KMeans\Space::SEED_DEFAULT, function($space, $clusters) {
    static $iterations = 0;

    printf("Iteration: %d\n", ++$iterations);

    foreach ($clusters as $i => $cluster)
        printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster));
});
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3862d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/77e952d494f6ed1b6156ddc80e357e314a2c9f731acd3e5a720c8fb90bcfc8fa?d=identicon)[crwgregory](/maintainers/crwgregory)

---

Top Contributors

[![bdelespierre](https://avatars.githubusercontent.com/u/1086339?v=4)](https://github.com/bdelespierre "bdelespierre (2 commits)")[![mtio](https://avatars.githubusercontent.com/u/9405969?v=4)](https://github.com/mtio "mtio (1 commits)")[![roncemer](https://avatars.githubusercontent.com/u/5806658?v=4)](https://github.com/roncemer "roncemer (1 commits)")

---

Tags

phputilitykmeanskmeans plus plus

### Embed Badge

![Health badge](/badges/crwgregory-php-kmeans/health.svg)

```
[![Health](https://phpackages.com/badges/crwgregory-php-kmeans/health.svg)](https://phpackages.com/packages/crwgregory-php-kmeans)
```

###  Alternatives

[bdelespierre/php-kmeans

K-Means algorithm for PHP

91140.6k3](/packages/bdelespierre-php-kmeans)[bdelespierre/underscore

Underscore.js port in PHP

6744.0k1](/packages/bdelespierre-underscore)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
