Sandbird

Phpml lib (KMeans) clustering

Jul 18th, 2022 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.55 KB | None | 0 0
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3.  
  4. use Phpml\Clustering\KMeans;
  5.  
  6. $locations = file('results.csv');
  7. foreach ($locations as &$line) {
  8.     $row = explode(',', $line);
  9.     $line = [(float) $row[0], (float) $row[1]];
  10. }
  11.  
  12.  
  13. $clusterer = new KMeans(18);
  14. $clusters = $clusterer->cluster($locations);
  15. $lines = [];
  16. foreach ($clusters as $key => $cluster) {
  17.     foreach ($cluster as $sample) {
  18.         $lines[] = sprintf('%s;%s;%s', $key, $sample[0], $sample[1]);
  19.     }
  20. }
  21. file_put_contents('results-clusters.csv', implode(PHP_EOL, $lines));
  22.  
Add Comment
Please, Sign In to add comment