1<?php
2
3require "vendor/autoload.php";
4
5// prepare 50 points of 2D space to be clustered
6$points = [
7    [80,55],[86,59],[19,85],[41,47],[57,58],
8    [76,22],[94,60],[13,93],[90,48],[52,54],
9    [62,46],[88,44],[85,24],[63,14],[51,40],
10    [75,31],[86,62],[81,95],[47,22],[43,95],
11    [71,19],[17,65],[69,21],[59,60],[59,12],
12    [15,22],[49,93],[56,35],[18,20],[39,59],
13    [50,15],[81,36],[67,62],[32,15],[75,65],
14    [10,47],[75,18],[13,45],[30,62],[95,79],
15    [64,11],[92,14],[94,49],[39,13],[60,68],
16    [62,10],[74,44],[37,42],[97,60],[47,73],
17];
18
19// create a 2-dimentions space
20$space = new KMeans\Space(2);
21
22// add points to space
23foreach ($points as $i => $coordinates) {
24    $space->addPoint($coordinates);
25}
26
27// cluster these 50 points in 3 clusters
28$clusters = $space->solve(3);
29
30// display the cluster centers and attached points
31foreach ($clusters as $num => $cluster) {
32    $coordinates = $cluster->getCoordinates();
33    printf(
34        "Cluster %s [%d,%d]: %d points\n",
35        $num,
36        $coordinates[0],
37        $coordinates[1],
38        count($cluster)
39    );
40}
41