Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32172
  • 博文数量: 15
  • 博客积分: 386
  • 博客等级: 一等列兵
  • 技术积分: 185
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-25 15:22
文章分类

全部博文(15)

文章存档

2012年(15)

我的朋友
最近访客

分类: C/C++

2012-07-09 15:14:18

此算法的主要作用:屏幕上很多的点,把相邻的点聚到离他最近的点。

 

k-means algorithm算法是一个聚类算法,把n个对象根据他们的属性分为k个分割,k < n。它与处理混合正态分布的最大期望算法很相似,因为他们都试图找到数据中自然聚类的中心。

php实现算法代码如下:


点击(此处)折叠或打开

  1. class Cluster
  2. {
  3.   public $points;
  4.   public $avgPoint;
  5.   function calculateAverage($maxX, $maxY)
  6.   {
  7.     if (count($this->points)==0)
  8.     {
  9.         $this->avgPoint->x = rand(0, $maxX);
  10.         $this->avgPoint->y = rand(0,$maxY);
  11.         //we didn't get any clues at all :( lets just randomize and hope for better...
  12.         return;
  13.     }
  14.      foreach($this->points as $p)
  15.         {
  16.          $xsum += $p->x;
  17.          $ysum += $p->y;
  18.         }
  19.   
  20.       $count = count($this->points);
  21.       $this->avgPoint->x = $xsum / $count;
  22.       $this->avgPoint->y = $ysum / $count;
  23.   }
  24. }
  25.   
  26. class Point
  27. {
  28.   public $x;
  29.   public $y;
  30.   function getDistance($p)
  31.         {
  32.          $x1 = $this->x - $p->x;
  33.          $y1 = $this->y - $p->y;
  34.          return sqrt($x1*$x1 + $y1*$y1);
  35.         }
  36. }
  37.   
  38. function distributeOverClusters($k, $arr)
  39. {
  40.  foreach($arr as $p)
  41.         { if ($p->x > $maxX)
  42.                 $maxX = $p->x;
  43.           if ($p->y > $maxY)
  44.                 $maxY = $p->y;
  45.         }
  46.   $clusters = array();
  47.   for($i = 0; $i < $k; $i++)
  48.         {
  49.          $clusters[] = new Cluster();
  50.          $tmpP = new Point();
  51.          $tmpP->x=rand(0,$maxX);
  52.          $tmpP->y=rand(0,$maxY);
  53.          $clusters[$i]->avgPoint = $tmpP;
  54.         }
  55.   #deploy points to closest center.
  56.   #recalculate centers
  57.   for ($a = 0; $a < 200; $a++) # run it 200 times
  58.   {
  59.         foreach($clusters as $cluster)
  60.                 $cluster->points = array(); //reinitialize
  61.         foreach($arr as $pnt)
  62.         {
  63.            $bestcluster=$clusters[0];
  64.            $bestdist = $clusters[0]->avgPoint->getDistance($pnt);
  65.   
  66.            foreach($clusters as $cluster)
  67.                 {
  68.                         if ($cluster->avgPoint->getDistance($pnt) < $bestdist)
  69.                         {
  70.                                 $bestcluster = $cluster;
  71.                                 $bestdist = $cluster->avgPoint->getDistance($pnt);
  72.                         }
  73.                 }
  74.                 $bestcluster->points[] = $pnt;//add the point to the best cluster.
  75.         }
  76.         //recalculate the centers.
  77.         foreach($clusters as $cluster)
  78.                 $cluster->calculateAverage($maxX, $maxY);
  79.   
  80.   }
  81.   return $clusters;
  82. }
  83.   
  84. $p = new Point();
  85. $p->x = 2;
  86. $p->y = 2;
  87. $p2 = new Point();
  88. $p2->x = 3;
  89. $p2->y = 2;
  90. $p3 = new Point();
  91. $p3->x = 8;
  92. $p3->y = 2;
  93. $arr[] = $p;
  94. $arr[] = $p2;
  95. $arr[] = $p3;
  96. var_dump(distributeOverClusters(2, $arr));


参考文献:
阅读(630) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~