bdelespierre / php-kmeans
PHP的K-Means算法
v2.2.0
2021-08-31 11:54 UTC
Requires
- php: ^7.3|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.3
README
PHP中的K均值聚类算法实现。
请参阅常见问题解答
安装
您可以通过composer安装此包
composer require bdelespierre/php-kmeans
用法
require "vendor/autoload.php"; // prepare 50 points of 2D space to be clustered $points = [ [80,55],[86,59],[19,85],[41,47],[57,58], [76,22],[94,60],[13,93],[90,48],[52,54], [62,46],[88,44],[85,24],[63,14],[51,40], [75,31],[86,62],[81,95],[47,22],[43,95], [71,19],[17,65],[69,21],[59,60],[59,12], [15,22],[49,93],[56,35],[18,20],[39,59], [50,15],[81,36],[67,62],[32,15],[75,65], [10,47],[75,18],[13,45],[30,62],[95,79], [64,11],[92,14],[94,49],[39,13],[60,68], [62,10],[74,44],[37,42],[97,60],[47,73], ]; // create a 2-dimentions space $space = new KMeans\Space(2); // add points to space foreach ($points as $i => $coordinates) { $space->addPoint($coordinates); } // cluster these 50 points in 3 clusters $clusters = $space->solve(3); // display the cluster centers and attached points foreach ($clusters as $num => $cluster) { $coordinates = $cluster->getCoordinates(); printf( "Cluster %s [%d,%d]: %d points\n", $num, $coordinates[0], $coordinates[1], count($cluster) ); }
注意:示例使用的是2维空间中的点,但它也可以用于任何维度 >1。
测试
composer test
变更日志
请参阅变更日志以获取最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全性
如果您发现任何安全问题,请通过电子邮件发送至benjamin.delespierre@gmail.com,而不是使用问题跟踪器。
鸣谢
许可证
Lesser General Public License (LGPL)。请参阅许可证文件以获取更多信息。
常见问题解答
如何获取点/聚类的坐标
$x = $point[0]; $y = $point[1]; // or list($x,$y) = $point->getCoordinates();
列出空间/聚类的所有点
foreach ($cluster as $point) { printf('[%d,%d]', $point[0], $point[1]); }
将数据附加到点
$point = $space->addPoint([$x, $y, $z], "user #123");
检索点数据
$data = $space[$point]; // e.g. "user #123"
观察算法运行
可以通过传递给Kmeans\Space::solve
的回调函数来监控每个迭代步骤。
$clusters = $space->solve(3, function($space, $clusters) { static $iterations = 0; printf("Iteration: %d\n", ++$iterations); foreach ($clusters as $i => $cluster) { printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster)); } });