bdelespierre / php-kmeans

PHP K-Means
MIT License
91 stars 41 forks source link

Call to undefined method "KMeans\Cluster::getPoints()" #11

Closed cikaldev closed 3 years ago

cikaldev commented 3 years ago

Environment

Problem

Fatal error: Uncaught Error: Call to undefined method KMeans\Cluster::getPoints()

Reconstructed

I just follow the code from Readme with a little change using 8 dimentions of array on $points variable, here is my code

require "vendor/autoload.php";

// dummy data
$points = [
  [79,75,75,85,76,78,76,80],
  [84,76,79,77,76,77,75,81],
  [77,84,78,85,92,89,77,82],
  [78,86,84,77,78,77,75,75],
  [82,82,81,91,90,82,79,91]
];

// create a 8-dimentions space
$space = new KMeans\Space(8);

// 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,%d,%d,%d,%d,%d]: %d points\n",
    $num,
    $coordinates[0],
    $coordinates[1],
    $coordinates[2],
    $coordinates[3],
    $coordinates[4],
    $coordinates[5],
    $coordinates[6],
    $coordinates[7],
    count($cluster->getPoints())
  );
}

The problem gone if i remove count($cluster->getPoints())

Regard's ian

bdelespierre commented 3 years ago

Hi @cikaldev, sorry for the late response.

The cluster class is already 1. a point "array" (it's an IteratorAggregate containing the points) and 2. countable.

Hence to determine how many points are in a cluster just do:

echo count($cluster) . " points are in cluster";

The readme.md is not up to date... Sorry about that I'll make changes right now :+1:

Thanks for using my lib :v: