蜡虫/聚类

使用K-means或空间划分对地图点进行聚类

0.9.3 2019-04-09 07:36 UTC

This package is auto-updated.

Last update: 2024-09-09 19:47:33 UTC


README

image_2019-03-10_09-19-21

聚类

本包专注于通过将地图划分为定义的方形空间并以空间为单位对每个点进行分配,以高效地对地图标记进行聚类。

说明

入门

使用composer在项目中引入此包,以下为命令:

$ composer require waxwink/clustering 

用法

用法非常直接

require __DIR__.'/vendor/autoload.php';

use Waxwink\Clustering\Clustering;

$points = [
	[
		'lat' => 35.821006,
		'lng' => 51.427388
	],
	[
		'lat' => 35.716912,
		'lng' => 51.439202
	],
	[
		'lat' => 35.67787,
		'lng' => 51.358735
	],
	[
		'lat' => 35.743621,
		'lng' => 51.530846
	],
];

为了将空间划分为相等区域并对空间内的点进行聚类,可以使用以下格式

$output = Clustering::getClusters($points, 0.1);
//The second parameter is the length of the square spaces by which the points are going to be clusterd.
//output :
/*
array (size=3)
  0 => 
    array (size=3)
      'lat' => float 35.821006
      'lng' => float 51.427388
      'total' => int 1
  1 => 
    array (size=3)
      'lat' => float 35.697391
      'lng' => float 51.3989685
      'total' => int 2
  2 => 
    array (size=3)
      'lat' => float 35.743621
      'lng' => float 51.530846
      'total' => int 1
*/

聚类的另一种方法是K-means算法,需要指定请求的聚类数。这种方法性能较慢,但能生成准确的聚类。

$output = Clustering::getClusters($points, 2, Clustering::K_MEANS_ALGORITHM, 5);
//the second parameter is the number of clusters
//the third parameter is the algorithm
//the forth parameter is the number of iterations used in the K-means algorithm which is 5 by default


//output:
/*
array (size=2)
  0 => 
    array (size=3)
      'lat' => float 35.821006
      'lng' => float 51.427388
      'total' => int 1
  1 => 
    array (size=3)
      'lat' => float 35.712801
      'lng' => float 51.442927666667
      'total' => int 3
*/