Skip to content

Commit

Permalink
update algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
agoenks29D committed Feb 13, 2022
1 parent 3ac83ef commit 27e2ef8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 45 deletions.
20 changes: 6 additions & 14 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ for (; ; ) {
}
$i++;
}

echo "<pre>";
print_r ($Kmeans);
echo "</pre>";
```

### Example 2 [Video Refrence](https://www.youtube.com/watch?v=FUwxw9Rv4Ls)
Expand Down Expand Up @@ -91,22 +87,18 @@ for (; ; ) {
}
$i++;
}

echo "<pre>";
print_r ($Kmeans);
echo "</pre>";
```

---

Langkah-langkah & Catatan :

1.) Hitung jumlah data.
2.) Tentukan jumlah "K" atau cluster (tidak boleh lebih dari jumlah data).
3.) Pilih titik secara acak sebanyak "K", dimana titik ini akan menjadi pusat (centroid) dari masing-masing kelompok (clusters).
4.) Hitung jarak dan alokasikan masing-masing data ke centroid atau rata-rata terdekat.
5.) Tentukan centroid baru / rata-rata yang ada di masing-masing cluster.
6.) Kembali ke step 3, apabila masih ada data yang berpindah cluster atau ada perubahan nilai centroid atau jika tidak ada perubahan maka hentikan proses clustering.
1. Hitung jumlah data.
2. Tentukan jumlah "K" atau cluster (tidak boleh lebih dari jumlah data).
3. Pilih titik secara acak sebanyak "K", dimana titik ini akan menjadi pusat (centroid) dari masing-masing kelompok (clusters).
4. Hitung jarak dan alokasikan masing-masing data ke centroid atau rata-rata terdekat.
5. Tentukan centroid baru / rata-rata yang ada di masing-masing cluster.
6. Kembali ke step 3, apabila masih ada data yang berpindah cluster atau ada perubahan nilai centroid atau jika tidak ada perubahan maka hentikan proses clustering.

---

Expand Down
24 changes: 19 additions & 5 deletions example/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,41 @@
$Kmeans->setClusterCount(2); // Set amount of cluster
$Kmeans->setCentroid(0, 1); // Choose centroid from array key or left blank will auto fill

// $Kmeans->generateCentroids(); // Generate random centroid

// initialized centroid
echo "<pre>";
print_r (array('initial_centroid' => $Kmeans->getInitialCentroid()));
echo "</pre>";

// Looping for iteration
$i = 1;
for (; ; ) {
$Kmeans->setIteration($i);
$Kmeans->run();
if ($Kmeans->isDone()) {
echo '<h1>Result</h1>';
echo '<hr>';

echo '<h2>Centroid</h2>';
echo "<pre>";
print_r ($Kmeans->getCentroid());
echo "</pre>";

echo '<h2>Iteration Total</h2>';
echo 'Iteration ended on : '.$Kmeans->countIterations();

echo '<h2>Iteration Logs</h2>';
echo "<pre>";
print_r (array('logs' => $Kmeans->catchLogs()));
echo "</pre>";

echo '<h2>All Result</h2>';
echo "<pre>";
print_r ($Kmeans->getAllResults());
echo "</pre>";
break;
}
$i++;
}

echo "<pre>";
print_r ($Kmeans);
echo "</pre>";
$i++;
}
11 changes: 8 additions & 3 deletions example/example2.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@
$Kmeans->setDataFromArgs(20);

$Kmeans->setClusterCount(3); // Set amount of cluster
$Kmeans->setCentroid(1, 5, 7); // Choose centroid from array key or left blank will auto fill
$Kmeans->setCentroid([1, 5, 7]); // Choose centroid from array key or left blank will auto fill

// Looping for iteration
$i = 1;
for (; ; ) {
$Kmeans->setIteration($i);
$Kmeans->run();
if ($Kmeans->isDone()) {
echo '<h1>Result</h1>';

echo '<h2>Centroid</h2>';
echo "<pre>";
print_r ($Kmeans->getCentroid());
print_r (array('centroid_result' => $Kmeans->getCentroid()));
echo "</pre>";

echo '<h2>Iteration Total</h2>';
echo 'Iteration ended on : '.$Kmeans->countIterations();

echo '<h2>Iteration Logs</h2>';
echo "<pre>";
print_r ($Kmeans->catchLogs());
print_r (array('logs' => $Kmeans->catchLogs()));
echo "</pre>";
break;
}

$i++;
}

Expand Down
88 changes: 65 additions & 23 deletions src/Kmeans.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* @package Algorithm
* @subpackage Kmeans
* @subpackage Kmeans
* @category Library
* @author Agung Dirgantara <[email protected]>
*/
Expand Down Expand Up @@ -32,7 +32,7 @@ class Kmeans {

/**
* Set attributes
*
*
* @param array $attributes
*/
public function setAttributes($attributes = array()) {
Expand Down Expand Up @@ -61,7 +61,7 @@ public function setDataFromArgs() {

/**
* Set cluster count
*
*
* @param integer $cluster
*/
public function setClusterCount($cluster = NULL) {
Expand All @@ -75,7 +75,7 @@ public function setClusterCount($cluster = NULL) {

/**
* Generate cluster
*
*
* @return array
*/
public function generateCluster() {
Expand Down Expand Up @@ -105,7 +105,7 @@ public function generateCluster() {

/**
* Set iteration
*
*
* @param integer $iteration
*/
public function setIteration($iteration = 0) {
Expand All @@ -115,7 +115,7 @@ public function setIteration($iteration = 0) {

/**
* Set centroids
*
*
* @param mixed $centroids
* @return \Kmeans
*/
Expand All @@ -131,7 +131,13 @@ public function setCentroid($centroids = NULL) {
}
} else {
if (is_array($centroids)) {
$this->centroids = $centroids;
if ($this->array_has_values($centroids)) {
$this->centroids = $centroids;
} else {
foreach ($centroids as $centroid) {
$this->centroids[] = $this->data[$centroid];
}
}
} else {
$this->generateCentroids();
}
Expand All @@ -140,24 +146,53 @@ public function setCentroid($centroids = NULL) {
return $this;
}

/**
* Determines whether the specified array is associative array.
*
* @param array $array
*
* @return bool
*/
private function array_has_values(array $array)
{
$array = array_map(function($value) {
if (is_array($value) OR is_object($value)) {
return TRUE;
}

return FALSE;
}, $array);

$array = array_filter($array);
return count($array) > 0;
}


/**
* Generate centroids
*
*
* @return \Kmeans
*/
public function generateCentroids() {

for ($i = 0; $i < $this->cluster; $i++) {
$centroids = array();

$this->centroids[] = $this->data[$i];
for ($i = 0; $i < $this->cluster; $i++) {
$data = array_diff(array_keys($this->data), array_keys($centroids));
$random_choose = array_rand($data);
$centroids[$random_choose] = $this->data[$random_choose];
}

array_map(function($centroid) {
array_push($this->centroids, $centroid);
}, $centroids);

return $this;
}

/**
* Create new centroid
*
*
* @param array $centroid
* @param array $group
* @return array
Expand Down Expand Up @@ -197,7 +232,7 @@ public function newCentroid($centroid, $group) {

/**
* Count distance
*
*
* @param integer $dataInK [description]
* @param integer $clusterInK [description]
* @return numeric
Expand All @@ -215,7 +250,7 @@ public function countDistance($dataInK, $clusterInK) {

/**
* Run Kmeans
*
*
* @param integer $cluster
* @return array
*/
Expand Down Expand Up @@ -288,7 +323,7 @@ public function run() {

/**
* Count iterations
*
*
* @return integer
*/
public function countIterations() {
Expand All @@ -297,7 +332,7 @@ public function countIterations() {

/**
* Get attributes;
*
*
* @return array
*/
public function getAttributes() {
Expand All @@ -306,7 +341,7 @@ public function getAttributes() {

/**
* Get data
*
*
* @return array
*/
public function getData() {
Expand All @@ -315,16 +350,23 @@ public function getData() {

/**
* Get last centroid
*
*
* @return array
*/
public function getCentroid() {
return $this->result;
}

/**
* Get initial centroid
*/
public function getInitialCentroid() {
return $this->centroids;
}

/**
* Get clusters
*
*
* @return array
*/
public function getClusters() {
Expand All @@ -333,7 +375,7 @@ public function getClusters() {

/**
* Get logs
*
*
* @param string $key one of : iterations, clusters, centroids
* @return array
*/
Expand All @@ -345,7 +387,7 @@ public function getLogs($key = NULL) {

/**
* Get all results
*
*
* @return array
*/
public function getAllResults() {
Expand All @@ -360,7 +402,7 @@ public function getAllResults() {

/**
* Catch logs
*
*
* @return array
*/
public function catchLogs() {
Expand All @@ -369,7 +411,7 @@ public function catchLogs() {

/**
* Reset centroid data
*
*
* @return \Kmeans
*/
public function resetCentroid() {
Expand All @@ -379,7 +421,7 @@ public function resetCentroid() {

/**
* Check kmeans is success
*
*
* @return boolean
*/
public function isDone() {
Expand Down

0 comments on commit 27e2ef8

Please sign in to comment.