bdelespierre /
php-kmeans
| 1 | <?php |
||
| 2 | |||
| 3 | require "vendor/autoload.php"; |
||
| 4 | |||
| 5 | $data = [ |
||
| 6 | [80,55],[86,59],[19,85],[41,47],[57,58], |
||
| 7 | [76,22],[94,60],[13,93],[90,48],[52,54], |
||
| 8 | [62,46],[88,44],[85,24],[63,14],[51,40], |
||
| 9 | [75,31],[86,62],[81,95],[47,22],[43,95], |
||
| 10 | [71,19],[17,65],[69,21],[59,60],[59,12], |
||
| 11 | [15,22],[49,93],[56,35],[18,20],[39,59], |
||
| 12 | [50,15],[81,36],[67,62],[32,15],[75,65], |
||
| 13 | [10,47],[75,18],[13,45],[30,62],[95,79], |
||
| 14 | [64,11],[92,14],[94,49],[39,13],[60,68], |
||
| 15 | [62,10],[74,44],[37,42],[97,60],[47,73], |
||
| 16 | ]; |
||
| 17 | |||
| 18 | // create a 2-dimentions space |
||
| 19 | $space = new Kmeans\Space(2); |
||
| 20 | |||
| 21 | // prepare the points |
||
| 22 | $points = new Kmeans\PointCollection($space); |
||
| 23 | |||
| 24 | foreach ($data as $coordinates) { |
||
| 25 | $points->attach(new Kmeans\Point($space, $coordinates)); |
||
| 26 | } |
||
| 27 | |||
| 28 | // prepare the algorithm |
||
| 29 | $algorithm = new Kmeans\Algorithm(new Kmeans\RandomInitialization()); |
||
| 30 | |||
| 31 | // cluster these 50 points in 3 clusters |
||
| 32 | $clusters = $algorithm->clusterize($points, 3); |
||
| 33 | |||
| 34 | // display the cluster centers and attached points |
||
| 35 | foreach ($clusters as $num => $cluster) { |
||
| 36 | $coordinates = $cluster->getCentroid()->getCoordinates(); |
||
| 37 | printf( |
||
| 38 | "Cluster #%s [%d,%d] has %d points\n", |
||
| 39 | $num, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 40 | $coordinates[0], |
||
| 41 | $coordinates[1], |
||
| 42 | count($cluster->getPoints()) |
||
| 43 | ); |
||
| 44 | } |
||
| 45 |