# K-means tering K average Algorithm

Source: Internet
Author: User

The main function of this algorithm is to aggregate neighboring points to the nearest point on the screen.

K-means algorithmAn algorithm is a clustering algorithm that divides n objects into k segments based on their attributes, k <n. It is similar to the maximum Expectation Algorithm for processing mixed normal distribution because they all try to find the center of natural clustering in the data.

The php algorithm code is as follows:

`</pre><pre name="code" class="php">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));`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.