-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ac83ef
commit 27e2ef8
Showing
4 changed files
with
98 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
/** | ||
* @package Algorithm | ||
* @subpackage Kmeans | ||
* @subpackage Kmeans | ||
* @category Library | ||
* @author Agung Dirgantara <[email protected]> | ||
*/ | ||
|
@@ -32,7 +32,7 @@ class Kmeans { | |
|
||
/** | ||
* Set attributes | ||
* | ||
* | ||
* @param array $attributes | ||
*/ | ||
public function setAttributes($attributes = array()) { | ||
|
@@ -61,7 +61,7 @@ public function setDataFromArgs() { | |
|
||
/** | ||
* Set cluster count | ||
* | ||
* | ||
* @param integer $cluster | ||
*/ | ||
public function setClusterCount($cluster = NULL) { | ||
|
@@ -75,7 +75,7 @@ public function setClusterCount($cluster = NULL) { | |
|
||
/** | ||
* Generate cluster | ||
* | ||
* | ||
* @return array | ||
*/ | ||
public function generateCluster() { | ||
|
@@ -105,7 +105,7 @@ public function generateCluster() { | |
|
||
/** | ||
* Set iteration | ||
* | ||
* | ||
* @param integer $iteration | ||
*/ | ||
public function setIteration($iteration = 0) { | ||
|
@@ -115,7 +115,7 @@ public function setIteration($iteration = 0) { | |
|
||
/** | ||
* Set centroids | ||
* | ||
* | ||
* @param mixed $centroids | ||
* @return \Kmeans | ||
*/ | ||
|
@@ -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(); | ||
} | ||
|
@@ -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 | ||
|
@@ -197,7 +232,7 @@ public function newCentroid($centroid, $group) { | |
|
||
/** | ||
* Count distance | ||
* | ||
* | ||
* @param integer $dataInK [description] | ||
* @param integer $clusterInK [description] | ||
* @return numeric | ||
|
@@ -215,7 +250,7 @@ public function countDistance($dataInK, $clusterInK) { | |
|
||
/** | ||
* Run Kmeans | ||
* | ||
* | ||
* @param integer $cluster | ||
* @return array | ||
*/ | ||
|
@@ -288,7 +323,7 @@ public function run() { | |
|
||
/** | ||
* Count iterations | ||
* | ||
* | ||
* @return integer | ||
*/ | ||
public function countIterations() { | ||
|
@@ -297,7 +332,7 @@ public function countIterations() { | |
|
||
/** | ||
* Get attributes; | ||
* | ||
* | ||
* @return array | ||
*/ | ||
public function getAttributes() { | ||
|
@@ -306,7 +341,7 @@ public function getAttributes() { | |
|
||
/** | ||
* Get data | ||
* | ||
* | ||
* @return array | ||
*/ | ||
public function getData() { | ||
|
@@ -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() { | ||
|
@@ -333,7 +375,7 @@ public function getClusters() { | |
|
||
/** | ||
* Get logs | ||
* | ||
* | ||
* @param string $key one of : iterations, clusters, centroids | ||
* @return array | ||
*/ | ||
|
@@ -345,7 +387,7 @@ public function getLogs($key = NULL) { | |
|
||
/** | ||
* Get all results | ||
* | ||
* | ||
* @return array | ||
*/ | ||
public function getAllResults() { | ||
|
@@ -360,7 +402,7 @@ public function getAllResults() { | |
|
||
/** | ||
* Catch logs | ||
* | ||
* | ||
* @return array | ||
*/ | ||
public function catchLogs() { | ||
|
@@ -369,7 +411,7 @@ public function catchLogs() { | |
|
||
/** | ||
* Reset centroid data | ||
* | ||
* | ||
* @return \Kmeans | ||
*/ | ||
public function resetCentroid() { | ||
|
@@ -379,7 +421,7 @@ public function resetCentroid() { | |
|
||
/** | ||
* Check kmeans is success | ||
* | ||
* | ||
* @return boolean | ||
*/ | ||
public function isDone() { | ||
|