상속: extends SplObjectStorag\SplObjectStorage
예제 #1
0
파일: KMeans.php 프로젝트: php-ai/php-ml
 /**
  * @param array $samples
  *
  * @return array
  */
 public function cluster(array $samples)
 {
     $space = new Space(count($samples[0]));
     foreach ($samples as $sample) {
         $space->addPoint($sample);
     }
     $clusters = [];
     foreach ($space->cluster($this->clustersNumber, $this->initialization) as $cluster) {
         $clusters[] = $cluster->getPoints();
     }
     return $clusters;
 }
예제 #2
0
파일: Cluster.php 프로젝트: php-ai/php-ml
 public function updateCentroid()
 {
     if (!($count = count($this->points))) {
         return;
     }
     $centroid = $this->space->newPoint(array_fill(0, $this->dimension, 0));
     foreach ($this->points as $point) {
         for ($n = 0; $n < $this->dimension; ++$n) {
             $centroid->coordinates[$n] += $point->coordinates[$n];
         }
     }
     for ($n = 0; $n < $this->dimension; ++$n) {
         $this->coordinates[$n] = $centroid->coordinates[$n] / $count;
     }
 }