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

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

jacobemerick/kmeans
===================

k-means clustering implemented in PHP

1.0.0(11y ago)126.4k3[2 issues](https://github.com/jacobemerick/kmeans/issues)[1 PRs](https://github.com/jacobemerick/kmeans/pulls)MITPHPPHP &gt;=5.3

Since Jul 31Pushed 7y agoCompare

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

READMEChangelogDependenciesVersions (2)Used By (0)

kmeans via PHP
==============

[](#kmeans-via-php)

This handly little class will calculate the k-means for a set of observations using PHP. k-means is a cool way to cluster data into groups based on relation - like clustering geographical data (using lat/lng) into a digestible summary. It is useful for detecting patterns in large data sets.

Usage
-----

[](#usage)

Let's say that you wanted to cluster a data set. The data must be in a multi-dimensional array, each value a numeric, though the size of each row has no constraint (n-dimensions ftw).

```
$array = [
    [1, 1, 3],
    [3, 7, 6],
    [5, 8, 3],
    [1, 2, 1],
    [9, 10, 8],
    [4, 4, 4],
];
```

By observation you may suspect that this data can be clustered into 3 separate sets. To test, run the class.

```
$kmeans = new Jacobemerick\KMeans\Kmeans($array);
$kmeans->cluster(3); // cluster into three sets

$clustered_data = $kmeans->getClusteredData();
// $clustered_data = [
//     [[1, 1, 3], [1, 2, 1]],
//     [[3, 5, 6], [5, 4, 3], [4, 4, 4]],
//     [[9, 10, 8]],
// ];

$centroids = $kmeans->getCentroids();
// $centroids = [
//     [1, 1.5, 2],
//     [4, 4.33, 4.33],
//     [9, 10, 8],
// ];
```

Note: larger data sets will be more consistent - if you run this example multiple times your results may vary.

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

[](#installation)

Through [composer](http://getcomposer.org):

```
$ composer require jacobemerick/kmeans:~1.0
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

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

4310d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a4eb553ed7677c6aa2c5adb9d20191557e64369c14a9d4b2371c924c4517dff?d=identicon)[jacobemerick](/maintainers/jacobemerick)

---

Top Contributors

[![jacobemerick](https://avatars.githubusercontent.com/u/527814?v=4)](https://github.com/jacobemerick "jacobemerick (11 commits)")

---

Tags

statisticsdata-analysisclusteringk-means

### Embed Badge

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

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

###  Alternatives

[rubix/ml

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

2.2k1.4M28](/packages/rubix-ml)[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k7.1M40](/packages/markrogoyski-math-php)[wnx/laravel-stats

Get insights about your Laravel Project

1.8k1.8M7](/packages/wnx-laravel-stats)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2751.4M5](/packages/rubix-tensor)[hi-folks/statistics

PHP package that provides functions for calculating mathematical statistics of numeric data.

398103.5k](/packages/hi-folks-statistics)[bdelespierre/php-kmeans

K-Means algorithm for PHP

91134.1k3](/packages/bdelespierre-php-kmeans)

PHPackages © 2026

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