PHPackages                             bdelespierre/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. bdelespierre/php-kmeans

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

bdelespierre/php-kmeans
=======================

K-Means algorithm for PHP

v2.2.0(4y ago)91134.1k↑15.6%42[7 issues](https://github.com/bdelespierre/php-kmeans/issues)[1 PRs](https://github.com/bdelespierre/php-kmeans/pulls)3MITPHPPHP ^7.3|^8.0

Since Feb 18Pushed 3y ago7 watchersCompare

[ Source](https://github.com/bdelespierre/php-kmeans)[ Packagist](https://packagist.org/packages/bdelespierre/php-kmeans)[ RSS](/packages/bdelespierre-php-kmeans/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (7)Used By (3)

PHP Kmean
=========

[](#php-kmean)

[![Latest Version on Packagist](https://camo.githubusercontent.com/17fe8397ad7890fff52bb892f3b24d6ca98bfbdcb9f422d7fd0f28bc4e35265a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6264656c65737069657272652f7068702d6b6d65616e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bdelespierre/php-kmeans)[![Build Status](https://camo.githubusercontent.com/9b6a7563840ccdf85a24c8ede63c6b4ab7b5f11bcadb95f73c2b2673aaf07ba6/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6264656c65737069657272652f7068702d6b6d65616e732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/bdelespierre/php-kmeans)[![Quality Score](https://camo.githubusercontent.com/8556332768b95f64c0e509d7e64886f03a26d38a4b1d270838fd37994662aeb5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6264656c65737069657272652f7068702d6b6d65616e732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bdelespierre/php-kmeans)[![Total Downloads](https://camo.githubusercontent.com/3e9eb5a4fafbede2e61ec2f3a51175cbf99dce32aa7d54735a36023633ce3c02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6264656c65737069657272652f7068702d6b6d65616e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bdelespierre/php-kmean)

[K-mean](http://en.wikipedia.org/wiki/K-means_clustering) clustering algorithm implementation in PHP.

Please also see the [FAQ](#faq)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require bdelespierre/php-kmeans
```

Usage
-----

[](#usage)

```
require "vendor/autoload.php";

// prepare 50 points of 2D space to be clustered
$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],
];

// create a 2-dimentions space
$space = new KMeans\Space(2);

// add points to space
foreach ($points as $i => $coordinates) {
    $space->addPoint($coordinates);
}

// cluster these 50 points in 3 clusters
$clusters = $space->solve(3);

// display the cluster centers and attached points
foreach ($clusters as $num => $cluster) {
    $coordinates = $cluster->getCoordinates();
    printf(
        "Cluster %s [%d,%d]: %d points\n",
        $num,
        $coordinates[0],
        $coordinates[1],
        count($cluster)
    );
}
```

**Note:** the example is given with points of a 2D space but it will work with any dimention &gt;1.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Benjamin Delespierre](https://github.com/bdelespierre)
- [Ron Cemer](https://github.com/roncemer)
- [All Contributors](../../contributors)

License
-------

[](#license)

Lesser General Public License (LGPL). Please see [License File](LICENSE.md) for more information.

FAQ
---

[](#faq)

### How to get coordinates of a point/cluster:

[](#how-to-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)

```
$point = $space->addPoint([$x, $y, $z], "user #123");
```

### Retrieve point data:

[](#retrieve-point-data)

```
$data = $space[$point]; // e.g. "user #123"
```

### 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, 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

45

—

FairBetter than 93% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 72.4% 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

Every ~307 days

Recently: every ~155 days

Total

6

Last Release

1474d ago

Major Versions

v1.0.0 → v2.0.02020-08-23

v2.2.0 → v3.x-dev2022-05-06

PHP version history (4 changes)v1.0.0PHP &gt;=5.4.0

v2.0.0PHP ^7.3

v2.1.1PHP ^7.3|^8.0

v3.x-devPHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/49e2b252b26a5d1644723184b0f832ba927620af3b9b628bd7c60f08909ad736?d=identicon)[bdelespierre](/maintainers/bdelespierre)

---

Top Contributors

[![bdelespierre](https://avatars.githubusercontent.com/u/1086339?v=4)](https://github.com/bdelespierre "bdelespierre (21 commits)")[![battlecook](https://avatars.githubusercontent.com/u/9781396?v=4)](https://github.com/battlecook "battlecook (6 commits)")[![ramzeng](https://avatars.githubusercontent.com/u/38133602?v=4)](https://github.com/ramzeng "ramzeng (1 commits)")[![roncemer](https://avatars.githubusercontent.com/u/5806658?v=4)](https://github.com/roncemer "roncemer (1 commits)")

---

Tags

looking-for-contributorsmachine-learning-algorithmsphpphpclusteringkmeans

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[rubix/ml

A high-level machine learning and deep learning library for the PHP language.

2.2k1.4M28](/packages/rubix-ml)[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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