Skip to content

medansoftware/KMeans-Algorithm-PHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP K-Means Library

Installation via composer

composer require medansoftware/kmeans-algorithm-php

How to use

Example 1 Video Refrence

$Kmeans = new \Algorithm\Kmeans;
$Kmeans->setAttributes(array(
	'A', 'B'
));

$Kmeans->setDataFromArgs(1, 1);  // "First Argument" assign to "First Attribute" and "Second Argument" assign to "Second Attribute" etc.
$Kmeans->setDataFromArgs(2, 1);
$Kmeans->setDataFromArgs(4, 3);
$Kmeans->setDataFromArgs(5, 4);

$Kmeans->setClusterCount(2); // Set amount of cluster
$Kmeans->setCentroid(0, 1); // 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 "<pre>";
		print_r ($Kmeans->getCentroid());
		echo "</pre>";

		echo 'Iteration ended on : '.$Kmeans->countIterations();

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

Example 2 Video Refrence

$Kmeans = new \Algorithm\Kmeans;
$Kmeans->setAttributes(array(
	'x'
));

$Kmeans->setDataFromArgs(1);
$Kmeans->setDataFromArgs(2);
$Kmeans->setDataFromArgs(6);
$Kmeans->setDataFromArgs(7);
$Kmeans->setDataFromArgs(8);
$Kmeans->setDataFromArgs(10);
$Kmeans->setDataFromArgs(15);
$Kmeans->setDataFromArgs(17);
$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

// Looping for iteration
$i = 1;
for (; ; ) {
	$Kmeans->setIteration($i);
	$Kmeans->run();
	if ($Kmeans->isDone()) {

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

		echo 'Iteration ended on : '.$Kmeans->countIterations();

		echo "<pre>";
		print_r ($Kmeans->catchLogs());
		echo "</pre>";
		break;
	}
	$i++;
}

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.

Refrence :


PATREON

PAYPAL

ETH : 0x251b36840557cCe9A245f07E1b834bCfb7354FDb

DOGE : DFmES6KZLQXimXduXwKmooykMsjhWmT1tU

BITCOIN : 1MEqUeg7fXTkBMFWfJZE9yJREsKZ4SUxQM

BITCOIN CASH : qzrllcyrjwvpnuur5kpeyp03p246fzsgzvhleswr6f

Social Media :

Made with ❤️ + ☕ ~ Agung Dirgantara