Clustering K-Means

Clustering K-Means
k-means example output

Clustering K-Means merupakan algoritma yang di gunakan untuk mengklusterkan data. Algoritma k-means ini sangat cocok untuk data yang besar namun tingkat akurasinya tidak lebih dari 50%
Berikut sources codenya yang saya buat menggunakan codeigniter

Database
data_ruspini.sql



  
-- phpMyAdmin SQL Dump
-- version 4.8.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Sep 20, 2018 at 06:52 PM
-- Server version: 10.1.34-MariaDB
-- PHP Version: 7.2.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `data`
--

-- --------------------------------------------------------

--
-- Table structure for table `data_ruspini`
--

CREATE TABLE `data_ruspini` (
  `id_data_ruspini` int(11) NOT NULL,
  `data1` int(11) NOT NULL,
  `data2` int(11) NOT NULL,
  `data3` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `data_ruspini`
--

INSERT INTO `data_ruspini` (`id_data_ruspini`, `data1`, `data2`, `data3`) VALUES
(1, 4, 53, 1),
(2, 5, 63, 1),
(3, 10, 59, 1),
(4, 9, 77, 1),
(5, 13, 49, 1),
(6, 13, 69, 1),
(7, 12, 88, 1),
(8, 15, 75, 1),
(9, 18, 61, 1),
(10, 19, 65, 1),
(11, 22, 74, 1),
(12, 27, 72, 1),
(13, 28, 76, 1),
(14, 24, 58, 1),
(15, 27, 55, 1),
(16, 28, 60, 1),
(17, 30, 52, 1),
(18, 31, 60, 1),
(19, 32, 61, 1),
(20, 36, 72, 1),
(21, 28, 147, 2),
(22, 32, 149, 2),
(23, 35, 153, 2),
(24, 33, 154, 2),
(25, 38, 151, 2),
(26, 41, 150, 2),
(27, 38, 145, 2),
(28, 38, 143, 2),
(29, 32, 143, 2),
(30, 34, 141, 2),
(31, 44, 156, 2),
(32, 44, 149, 2),
(33, 44, 143, 2),
(34, 46, 142, 2),
(35, 47, 149, 2),
(36, 49, 152, 2),
(37, 50, 142, 2),
(38, 53, 144, 2),
(39, 52, 152, 2),
(40, 55, 155, 2),
(41, 54, 124, 2),
(42, 60, 136, 2),
(43, 63, 139, 2),
(44, 86, 132, 3),
(45, 85, 115, 3),
(46, 85, 96, 3),
(47, 78, 94, 3),
(48, 74, 96, 3),
(49, 97, 122, 3),
(50, 98, 116, 3),
(51, 98, 124, 3),
(52, 99, 119, 3),
(53, 99, 128, 3),
(54, 101, 115, 3),
(55, 108, 111, 3),
(56, 110, 111, 3),
(57, 108, 116, 3),
(58, 111, 126, 3),
(59, 115, 117, 3),
(60, 117, 115, 3),
(61, 70, 4, 4),
(62, 77, 12, 4),
(63, 83, 21, 4),
(64, 61, 15, 4),
(65, 69, 15, 4),
(66, 78, 16, 4),
(67, 66, 18, 4),
(68, 58, 13, 4),
(69, 64, 20, 4),
(70, 69, 21, 4),
(71, 66, 23, 4),
(72, 61, 25, 4),
(73, 76, 27, 4),
(74, 72, 31, 4),
(75, 64, 30, 4);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `data_ruspini`
--
ALTER TABLE `data_ruspini`
  ADD PRIMARY KEY (`id_data_ruspini`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `data_ruspini`
--
ALTER TABLE `data_ruspini`
  MODIFY `id_data_ruspini` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=76;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;




Controller
k-means.php


  


  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class K_means extends CI_Controller {

 /**
  * Index Page for this controller.
  *
  * Maps to the following URL
  *   http://example.com/index.php/welcome
  * - or -  
  *   http://example.com/index.php/welcome/index
  * - or -
  * Since this controller is set as the default controller in 
  * config/routes.php, it's displayed at http://example.com/
  *
  * So any other public methods not prefixed with an underscore will
  * map to /index.php/welcome/<method_name>
  * @see http://codeigniter.com/user_guide/general/urls.html
  */
 
 public function ujicoba(){
  $this->load->view('header');
  $this->load->view('kmeans/ujicoba');
  $this->load->view('footer');
 }
 
 public function hasil()
 {
  $jk=$_POST['jumklas'];
  $table='data_ruspini';
  $col=$this->dmodel->columncount($table)->num_rows();
  //echo $col;
  $colname=$this->dmodel->columname($table);
  //pembangkitan centroid
  for($i=0;$i<$jk;$i++){
   /*for($j=0;$j<$col;$j++){
   $newc[$i][$j]=rand(0,400);
   }*/
   $centroid[$i]=rand(0,100);
  }
  $output['jumklas']=$jk;
  
  //penghitungan jarak centroid
  /*for($k=0;$k<$jk;$k++){
    $jumcen=0;
    for($j=0;$j<$col;$j++){
     $jumcen=$jumcen+($newc[$k][$j]^2); 
    }
   $centroid[$k]=sqrt($jumcen);
  }*/
  $output['centroid']=$centroid;
  //mengambil data colum database
  $co=0;
  foreach($colname as $row){
  $namecol[$co]=$row->name;
  $co++;
  }
  $getdata=$this->dmodel->getdata($table)->result_array();
  $c=0;
  //mengambil nilai data pada database
  foreach($getdata as $row){
   $a=1;
   for($i=0;$i<$co-1;$i++){
   
    $dat[$c][$i]=$row[$namecol[$a]];
    //echo $dat[$c][$i];
    $a++;
    }
   //echo '';
   $c++;
  }
  //penghitungan jarak pada nilai data
  $k=0;
  $jardat=0;
  for($i=0;$i<$c;$i++){
   for($j=0;$j<$co-1;$j++){
   $jardat=$jardat+($dat[$i][$j]^2);
   }
   $datcen[$i]=sqrt($jardat);
   
   $k++;
  }
  
  //penghitungan jarak centroid baru dengan centroid data
  for($i=0;$i<$k;$i++){
   for($j=0;$j<$jk;$j++){
   $jarak=abs($centroid[$j]-$datcen[$i]);
   $datacen[$i][$j]=$jarak;
   $dataklas[$i][$j]=$j;
   $datacen[$i]['centroid']=$datcen[$i];
   }
  }
  
  
  
  //klustering berdasarkan jarak
  for($i=0;$i<$k;$i++){
   for($j=0;$j<$jk;$j++){
   for($l=$j+1;$l<$jk;$l++){
    if($datacen[$i][$j]>$datacen[$i][$l]){
    $temp=$datacen[$i][$j];
    $temp2=$dataklas[$i][$j];
    $datacen[$i][$j]=$datacen[$i][$l];
    $dataklas[$i][$j]=$dataklas[$i][$l];
    $datacen[$i][$l]=$temp;
    $dataklas[$i][$l]=$temp2;
    }
    }
   }
   $datakluster[$i]['kluster']=$dataklas[$i][0];
   $datakluster[$i]['jarak']=$datacen[$i][0];
   $datakluster[$i]['centroid']=$datacen[$i]['centroid'];
  }
  
  /*for($i=0;$i<$k;$i++){
   echo $datakluster[$i]['kluster'].'';
   echo $datakluster[$i]['jarak'].'';
   echo $datakluster[$i]['centroid'].'';
   
  
  }*/
  
  $output['jumlah']=$k;
  $output['datahasil']=$datakluster;
  
  $this->load->view('header');
  $this->load->view('kmeans/hasil',$output);
  $this->load->view('footer');
 }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

Model
dmodel.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


Class Dmodel extends CI_Model{
 
 public function columname($table){
  return $this->db->field_data($table);
 }
 // get data from model
 function getdata($table){
  return $this->db->query("select *from $table;");
 }
 
 public function columncount($table){
  return $this->db->query("SELECT column_name FROM information_schema.columns WHERE table_name='$table';");
 }
 
View
ujicoba.php

<h4>Tentukan Fakta yang ada:</h4>
<form action="<?php echo base_url();?>index.php/k_means/hasil" method="post" name="form1">
<table align="center">
 <tbody>
<tr>
 <--<label >Masukan Nama Table Database</label><input type="text" name="table"/></th></br>-->
 <th><label>Masukan Jumlah Kluster</label><input name="jumklas" type="text" /></th>
 </tr>
<tr>
  <th><input name="submit" type="submit" value="submit" /></th>
 </tr>
</tbody></table>
</form>



hasil.php
            

<aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        Data Tables
                        
                    </h1>
                    <ol class="breadcrumb">
                        <li>
                        <a href="#"><i class="fa fa-dashboard">
                        </i>Dashboard</a></li>
                        <li><a href="#">Algoritma Nearest Neighbor</a>
                        </li>
                        <li class="active">Learning Data</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box">
       
                                <div class="box-header">
                                    <h3 class="box-title">Clustering K-Means</h3>
                                </div>
                                <!-- /.box-header -->
                                <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                     <th>centroid</th>
                                     <th>jarak</th>
                                     <th>anggota kluster</th>
                                </thead>
                                <tbody>
                                <?php for($j=0;$j<$jumlah;$j++){?>
                                      <tr>
                                      <td><?php echo $datahasil[$j]['centroid'];?></td>
                                      <td><?php echo $datahasil[$j]['jarak'];?></td>
                                      <td><?php echo $datahasil[$j]['kluster'];?></td>
                                      </tr>
                                 <?php } ?>
                                 </tbody>
                                       
                                 </table>
                                 <div class="box-body table-responsive">
                                 <h1><h1>
                            </div>
                            <!-- /.box-body -->
                        </div>
                        <!-- /.box -->
                        <table id="example2" class="table table-bordered table-hover">                    
                        <thead>
                        <th>kluster</th>
                        <th>centroid pembangkit</th>
                           </thead>
                            <tbody>
                                  <?php for($j=0;$j<$jumklas;$j++){?>
                                     <tr>
                                     <td><?php echo $j?></td>
                                     <td><?php echo $centroid[$j]?></td>
                                     </tr>
                                  <?php } ?>
                            </tbody>  
                         </table>
                      </div>
                    </div>

                </section>
           <!-- /.content -->
            </aside>
      <!-- /.right-side -->
video untuk Demo Klustering K-means

11 komentar

Mas.. bisa kirim codingannya g mas ke sini murniasih.1508@gmail.com
Makasih mas,saya udh nyoba yg diatas tapi gagal terus

Reply

Assalamu'alaikum
Gan bisa dikirim source codenya ke alamat ini syani1271@gma

Reply

Assalamu'alaikum
Kak bisa kirim ke alamat ini syani1271@gmail.com
Klu sudah ada source codenya

Reply

kak boleh minta kodinganya ga ,, kalau boleh kirim kesini ya yusririzal10@gmail.com ,, terima kasih ka ..

Reply

permisi admin.
Bagi mahasiswa yang perlu source code php, natif maupun framework bermetode AHP, SAW, Smart, Topsis, Fuzzy Logic, K-Means, Bayes dan lain-lain bisa kunjungi situ saya di :
https://code-skripsi.blogspot.com/

Terima kasih

Reply

assalamualikum, boleh mas sedekahnya kirim email ke hadi.setiono@gmail.com

Reply

bagi yg butuh source code program silahkan email ke : rahazmustakim@gmail "gratis"

Reply

Asslammualaikum. Kk bisa kirim sort codenya kk ke email legasintia019@gmail.com

Reply

bang, bila berkenan saya minta source code nya kirim ke email : nafanisa911@gmail.com

Reply

Posting Komentar