php-kmeans/README.md

140 lines
3.5 KiB
Markdown
Raw Normal View History

2020-08-23 21:19:51 +02:00
# PHP Kmean
2014-10-08 17:03:58 +02:00
2020-08-23 21:21:34 +02:00
[![Latest Version on Packagist](https://img.shields.io/packagist/v/bdelespierre/php-kmeans.svg?style=flat-square)](https://packagist.org/packages/bdelespierre/php-kmeans)
[![Build Status](https://img.shields.io/travis/bdelespierre/php-kmeans/master.svg?style=flat-square)](https://travis-ci.org/bdelespierre/php-kmeans)
[![Quality Score](https://img.shields.io/scrutinizer/g/bdelespierre/php-kmeans.svg?style=flat-square)](https://scrutinizer-ci.com/g/bdelespierre/php-kmeans)
[![Total Downloads](https://img.shields.io/packagist/dt/bdelespierre/php-kmeans.svg?style=flat-square)](https://packagist.org/packages/bdelespierre/php-kmean)
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
[K-mean](http://en.wikipedia.org/wiki/K-means_clustering) clustering algorithm implementation in PHP.
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
Please also see the [FAQ](#faq)
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
## Installation
You can install the package via composer:
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
```bash
composer require bdelespierre/php-kmeans
```
## Usage
2014-10-08 17:03:58 +02:00
```PHP
2020-08-23 21:19:51 +02:00
require "vendor/autoload.php";
// prepare 50 points of 2D space to be clustered
2014-10-08 17:03:58 +02:00
$points = [
[80,55],[86,59],[19,85],[41,47],[57,58],
2020-08-23 21:19:51 +02:00
[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],
2014-10-08 17:03:58 +02:00
];
2020-08-23 21:19:51 +02:00
// create a 2-dimentions space
2014-10-08 17:03:58 +02:00
$space = new KMeans\Space(2);
2020-08-23 21:19:51 +02:00
// add points to space
foreach ($points as $i => $coordinates) {
$space->addPoint($coordinates);
}
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
// cluster these 50 points in 3 clusters
2014-10-08 17:03:58 +02:00
$clusters = $space->solve(3);
2020-08-23 21:19:51 +02:00
// 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->getPoints())
);
}
2014-10-08 17:03:58 +02:00
```
2020-08-23 21:19:51 +02:00
**Note:** the example is given with points of a 2D space but it will work with any dimention >1.
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
### Testing
``` bash
composer test
2014-10-08 17:03:58 +02:00
```
2020-08-23 21:19:51 +02:00
### Changelog
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
## Contributing
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
### Security
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
If you discover any security related issues, please email benjamin.delespierre@gmail.com instead of using the issue tracker.
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
## Credits
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
- [Benjamin Delespierre](https://github.com/bdelespierre)
- [Ron Cemer](https://github.com/roncemer)
- [All Contributors](../../contributors)
## License
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
Lesser General Public License (LGPL). Please see [License File](LICENSE.md) for more information.
2014-10-08 17:03:58 +02:00
2020-08-23 21:19:51 +02:00
## FAQ
### How to get coordinates of a point/cluster:
2014-10-08 17:03:58 +02:00
```PHP
$x = $point[0];
$y = $point[1];
// or
list($x,$y) = $point->getCoordinates();
```
### List all points of a space/cluster:
```PHP
2020-08-23 21:19:51 +02:00
foreach ($cluster as $point) {
2014-10-08 17:03:58 +02:00
printf('[%d,%d]', $point[0], $point[1]);
2020-08-23 21:19:51 +02:00
}
2014-10-08 17:03:58 +02:00
```
### Attach data to a point:
```PHP
2020-08-23 21:19:51 +02:00
$point = $space->addPoint([$x, $y, $z], "user #123");
2014-10-08 17:03:58 +02:00
```
### Retrieve point data:
```PHP
2020-08-23 21:19:51 +02:00
$data = $space[$point]; // e.g. "user #123"
2014-10-08 17:03:58 +02:00
```
### Watch the algorithm run
Each iteration step can be monitored using a callback function passed to `Kmeans\Space::solve`:
```PHP
2020-08-23 21:19:51 +02:00
$clusters = $space->solve(3, function($space, $clusters) {
2014-10-08 17:03:58 +02:00
static $iterations = 0;
printf("Iteration: %d\n", ++$iterations);
2020-08-23 21:19:51 +02:00
foreach ($clusters as $i => $cluster) {
2014-10-08 17:03:58 +02:00
printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster));
2020-08-23 21:19:51 +02:00
}
2014-10-08 17:03:58 +02:00
});
2020-08-23 21:19:51 +02:00
```