77
88[ K-mean] ( http://en.wikipedia.org/wiki/K-means_clustering ) clustering algorithm implementation in PHP.
99
10- Please also see the [ FAQ] ( #faq )
11-
1210## Installation
1311
1412You can install the package via composer:
@@ -22,8 +20,7 @@ composer require bdelespierre/php-kmeans
2220``` PHP
2321require "vendor/autoload.php";
2422
25- // prepare 50 points of 2D space to be clustered
26- $points = [
23+ $data = [
2724 [80,55],[86,59],[19,85],[41,47],[57,58],
2825 [76,22],[94,60],[13,93],[90,48],[52,54],
2926 [62,46],[88,44],[85,24],[63,14],[51,40],
@@ -37,30 +34,35 @@ $points = [
3734];
3835
3936// create a 2-dimentions space
40- $space = new KMeans\Space(2);
37+ $space = new Kmeans\Space(2);
38+
39+ // prepare the points
40+ $points = new Kmeans\PointCollection($space);
4141
42- // add points to space
43- foreach ($points as $i => $coordinates) {
44- $space->addPoint($coordinates);
42+ foreach ($data as $coordinates) {
43+ $points->attach(new Kmeans\Point($space, $coordinates));
4544}
4645
46+ // prepare the algorithm
47+ $algorithm = new Kmeans\Algorithm(new Kmeans\RandomInitialization());
48+
4749// cluster these 50 points in 3 clusters
48- $clusters = $space->solve( 3);
50+ $clusters = $algorithm->clusterize($points, 3);
4951
5052// display the cluster centers and attached points
5153foreach ($clusters as $num => $cluster) {
52- $coordinates = $cluster->getCoordinates();
54+ $coordinates = $cluster->getCentroid()-> getCoordinates();
5355 printf(
54- "Cluster %s [%d,%d]: %d points\n",
56+ "Cluster # %s [%d,%d] has %d points\n",
5557 $num,
5658 $coordinates[0],
5759 $coordinates[1],
58- count($cluster)
60+ count($cluster->getPoints() )
5961 );
6062}
6163```
6264
63- ** Note:** the example is given with points of a 2D space but it will work with any dimention > 1.
65+ ** Note:** the example is given with points of a 2D space but it will work with any dimention greater than or equal to 1.
6466
6567### Testing
6668
@@ -89,51 +91,3 @@ If you discover any security related issues, please email benjamin.delespierre@g
8991## License
9092
9193Lesser General Public License (LGPL). Please see [ License File] ( LICENSE.md ) for more information.
92-
93- ## FAQ
94-
95- ### How to get coordinates of a point/cluster:
96- ``` PHP
97- $x = $point[0];
98- $y = $point[1];
99-
100- // or
101-
102- list($x,$y) = $point->getCoordinates();
103- ```
104-
105- ### List all points of a space/cluster:
106-
107- ``` PHP
108- foreach ($cluster as $point) {
109- printf('[%d,%d]', $point[0], $point[1]);
110- }
111- ```
112-
113- ### Attach data to a point:
114-
115- ``` PHP
116- $point = $space->addPoint([$x, $y, $z], "user #123");
117- ```
118-
119- ### Retrieve point data:
120-
121- ``` PHP
122- $data = $space[$point]; // e.g. "user #123"
123- ```
124-
125- ### Watch the algorithm run
126-
127- Each iteration step can be monitored using a callback function passed to ` Kmeans\Space::solve ` :
128-
129- ``` PHP
130- $clusters = $space->solve(3, function($space, $clusters) {
131- static $iterations = 0;
132-
133- printf("Iteration: %d\n", ++$iterations);
134-
135- foreach ($clusters as $i => $cluster) {
136- printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster));
137- }
138- });
139- ```
0 commit comments