国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 邏輯算法 > 正文

k-means clustering K平均算法

2020-03-22 19:48:03
字體:
來源:轉載
供稿:網友
  • 此算法的主要作用:屏幕上很多的點,把相鄰的點聚到離他最近的點。

    k-means algorithm算法是一個聚類算法,把n個對象根據他們的屬性分為k個分割,k < n。它與處理混合正態分布的最大期望算法很相似,因為他們都試圖找到數據中自然聚類的中心。

    php實現算法代碼如下:


    html' target='_blank'>class Cluster {   public $points;   public $avgPoint;   function calculateAverage($maxX, $maxY)   {     if (count($this->points)==0)     {         $this->avgPoint->x = rand(0, $maxX);         $this->avgPoint->y =  rand(0,$maxY);         //we didn't get any clues at all :( lets just randomize and hope for better...         return;     }      foreach($this->points as $p)         {          $xsum += $p->x;          $ysum += $p->y;         }         $count = count($this->points);       $this->avgPoint->x =  $xsum / $count;       $this->avgPoint->y =  $ysum / $count;   } }   class Point {   public $x;   public $y;   function getDistance($p)         {          $x1 = $this->x - $p->x;          $y1 = $this->y - $p->y;          return sqrt($x1*$x1 + $y1*$y1);         } }   function distributeOverClusters($k, $arr) {  foreach($arr as $p)         { if ($p->x > $maxX)                 $maxX = $p->x;           if ($p->y > $maxY)                 $maxY = $p->y;         }   $clusters = array();   for($i = 0; $i < $k; $i++)         {          $clusters[] = new Cluster();          $tmpP = new Point();          $tmpP->x=rand(0,$maxX);          $tmpP->y=rand(0,$maxY);          $clusters[$i]->avgPoint = $tmpP;         }   #deploy points to closest center.   #recalculate centers   for ($a = 0; $a < 200; $a++) # run it 200 times   {         foreach($clusters as $cluster)                 $cluster->points = array(); //reinitialize         foreach($arr as $pnt)         {            $bestcluster=$clusters[0];            $bestdist = $clusters[0]->avgPoint->getDistance($pnt);              foreach($clusters as $cluster)                 {                         if ($cluster->avgPoint->getDistance($pnt) < $bestdist)                         {                                 $bestcluster = $cluster;                                 $bestdist = $cluster->avgPoint->getDistance($pnt);                         }                 }                 $bestcluster->points[] = $pnt;//add the point to the best cluster.         }         //recalculate the centers.         foreach($clusters as $cluster)                 $cluster->calculateAverage($maxX, $maxY);     }   return $clusters; }   $p = new Point(); $p->x = 2; $p->y = 2; $p2 = new Point(); $p2->x = 3; $p2->y = 2; $p3 = new  Point(); $p3->x = 8; $p3->y = 2; $arr[] = $p; $arr[] = $p2; $arr[] = $p3; var_dump(distributeOverClusters(2, $arr));

    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 乌鲁木齐县| 鄂州市| 宽甸| 博客| 汤阴县| 宜昌市| 平潭县| 汕尾市| 和龙市| 呼和浩特市| 天峻县| 东宁县| 万安县| 佛冈县| 安西县| 家居| 介休市| 五原县| 和顺县| 宜良县| 苍溪县| 永春县| 凯里市| 平湖市| 合作市| 贞丰县| 海淀区| 基隆市| 芜湖市| 克拉玛依市| 延吉市| 宁波市| 南阳市| 阿勒泰市| 土默特右旗| 武乡县| 城固县| 东乡县| 忻城县| 大港区| 高淳县|