Implementasi Algoritma Nearest Neighbor

Algoritma Nearest Neighbor
ilustrasi nearest neighbor
algoritma ini menggunakan prinsip ketetanggaan dengan melakukan pendekatan jarak antar data
berikut sources code untuk membuat algoritma nearest neighbor di Codeigniter

untuk deskripsi tentang algoritma nearest neighbor bisa di baca situs wikipedia

Table SQL

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 19 Apr 2016 pada 13.19
-- Versi Server: 10.1.10-MariaDB
-- PHP Version: 5.6.19

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
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`
--

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

--
-- Struktur dari tabel `data_training`
--

CREATE TABLE `data_training` (
  `id_data` int(11) NOT NULL,
  `Sky` text NOT NULL,
  `AirTemp` text NOT NULL,
  `Humidity` text NOT NULL,
  `Wind` text NOT NULL,
  `Water` text NOT NULL,
  `Forecast` text NOT NULL,
  `EnjoySport` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `data_training`
--

INSERT INTO `data_training` (`id_data`, `Sky`, `AirTemp`, `Humidity`, `Wind`, `Water`, `Forecast`, `EnjoySport`) VALUES
(1, 'sunny', 'warm', 'normal', 'strong', 'warm', 'same', 'yes'),
(2, 'sunny', 'warm', 'high', 'strong', 'warm', 'same', 'yes'),
(3, 'rainy', 'cold', 'high', 'strong', 'warm', 'change', 'no'),
(4, 'sunny', 'warm', 'high', 'strong', 'cool', 'change', 'yes');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `data_training`
--
ALTER TABLE `data_training`
  ADD PRIMARY KEY (`id_data`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `data_training`
--
ALTER TABLE `data_training`
  MODIFY `id_data` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
/*!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
nearest.php




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

class Nearest 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 index(){
  $this->load->view('header');
  $this->load->view('nearest/ujicoba');
  $this->load->view('footer');
 }
 public function hasil(){
 $data=$this->dmodel->getDataLearning()->result();
 $j=0;
 foreach($data as $row){
  if($row->EnjoySport=='yes'){
  $datalearning[$j][0]=$row->Sky;
  $datalearning[$j][1]=$row->AirTemp;
  $datalearning[$j][2]=$row->Humidity;
  $datalearning[$j][3]=$row->Wind;
  $datalearning[$j][4]=$row->Water;
  $datalearning[$j][5]=$row->Forecast;
  $datalearning[$j][6]=$row->EnjoySport;
  }else{
  $datalearning[$j][0]=$row->Sky;
  $datalearning[$j][1]=$row->AirTemp;
  $datalearning[$j][2]=$row->Humidity;
  $datalearning[$j][3]=$row->Wind;
  $datalearning[$j][4]=$row->Water;
  $datalearning[$j][5]=$row->Forecast;
  $datalearning[$j][6]=$row->EnjoySport;
  }
  $j=$j+1;
  }
  $jumdat=$this->dmodel->countData("")->num_rows();
  for ($i = 0; $i < $jumdat; $i++) {
            if($datalearning[$i][0] == "sunny" ){
   $datainisial[$i][0]=10;
   }
   else{
   $datainisial[$i][0]=0;
   }
   if($datalearning[$i][1] == "warm"){
   $datainisial[$i][1]=20;
   }
   else{
   $datainisial[$i][1]=0;
   }
   if($datalearning[$i][2] == "normal"){
   $datainisial[$i][2]=30;
   }
   else{
   $datainisial[$i][2]=0;
   }
   if($datalearning[$i][3] == "strong"){
   $datainisial[$i][3]=40;
   }
   else{
   $datainisial[$i][3]=0;
   }
   if($datalearning[$i][4] == "warm"){
   $datainisial[$i][4]=50;
   }
   else{
   $datainisial[$i][4]=0;
   }
   if($datalearning[$i][5] == "same"){
   $datainisial[$i][5]=60;
   }
   else{
   $datainisial[$i][5]=0;
   }
   $datainisial[$i][6]=$datalearning[$i][6];
        }
  //inputan data from user
   if($_POST['sky']== "sunny" ){
   $sky=10;
   }
   else{
   $sky=0;
   }
   if($_POST['airtemp']== "warm"){
   $airtemp=20;
   }
   else{
   $airtemp=0;
   }
   if($_POST['humidity']== "normal"){
   $humidity=30;
   }
   else{
   $humidity=0;
   }
   if($_POST['wind']== "strong"){
   $wind=40;
   }
   else{
   $wind=0;
   }
   if($_POST['water']== "warm"){
   $water=50;
   }
   else{
   $water=0;
   }
   if($_POST['forecast']== "same"){
   $forecast=60;
   }
   else{
   $forecast=0;
   }
   $jumteg=$_POST['jumlah_tetangga'];
  
  $nilainput = sqrt(($sky^2)+($airtemp^2)+($humidity^2)+($wind^2)+($water^2)+($forecast^2));
  for($k = 0 ; $k < $jumdat ; $k++){
  $nilaidata[$k][0]=sqrt(($datainisial[$k][0]^2)+($datainisial[$k][1]^2)+($datainisial[$k][2]^2)+($datainisial[$k][3]^2)+($datainisial[$k][4]^2)+($datainisial[$k][5]^2));
  $nilaidata[$k][1]=$datainisial[$k][6];
  }
  //hitung jarak antar dengan tetangga
  for($l=0;$l<$jumdat;$l++){
  $nilaijarak[$l][0]=abs($nilaidata[$l][0]-$nilainput);
  $nilaijarak[$l][1]=$nilaidata[$l][1];
  //echo $nilaidata[$l][0].' '.$nilainput.'';
  //echo $nilaijarak[$l][0].' '.$nilaijarak[$l][1].'';
  }
  
  //pengurutan atau sorting
  for($m=0;$m<$jumdat;$m++){
   for($n=$m+1;$n<$jumdat;$n++){
    if($nilaijarak[$m][0]>$nilaijarak[$n][0]){
    $temp=$nilaijarak[$m][0];
    $temp1=$nilaijarak[$m][1];
    $nilaijarak[$m][0]=$nilaijarak[$n][0];
    $nilaijarak[$m][1]=$nilaijarak[$n][1];
    $nilaijarak[$n][0]=$temp;
    $nilaijarak[$n][1]=$temp1;
    }
   }
  }
  //echo "";
  //for($s=0;$s<$jumdat;$s++){
  //echo $nilaijarak[$s][0].' '.$nilaijarak[$s][1].'';
  //}
  $ya=0;
  $tidak=0;
  for($o=0;$o<$jumteg;$o++){
   if($nilaijarak[$o][1]=="yes"){
    $ya=$ya+1;
   }
   else{
    $tidak=$tidak+1;
   }
   $hasilteg['jarak'][$o]=$nilaijarak[$o][0];
   $hasilteg['status'][$o]=$nilaijarak[$o][1];
  }
  
  //hasil nearest neighbor
  if ($ya > $tidak) {
  $datahasil['hasil']="sangat baik untuk olahraga";
  $datahasil['jaraktetangga']= $hasilteg;
  }
  else{
  $datahasil['hasil']="tidak baik untuk olahraga";
  $datahasil['jaraktetangga']= $hasilteg;
  }
  $datahasil['jumteg']=$jumteg;
  $this->load->view('header');
  $this->load->view('nearest/hasil',$datahasil);
  $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{

 function getDataLearning(){

  return $this->db->query("select *from data_training;");

 }

 public function countData($where=''){

 return $this->db->query("SELECT *FROM data_training $where;");


 }

View 
Ujicoba.php

<h4>Tentukan Fakta yang ada:</h4>
<form action="<?php echo base_url();>index.php/nearest_neighbor/hasil" method="post" name="form1">
<table align="center" border="0">
 <tbody>
<tr>
  <th colspan="3">Sky</th>
  <th colspan="3">Airtemp</th>  
  <th colspan="3">Humidity</th>
  <th colspan="3">Wind</th>  
  <th colspan="3">Water</th>  
  <th colspan="3">Forecast</th>
 </tr>
<tr> 
  <th colspan="3"><select name="sky">
  <option value="sunny">Sunny
  </option><option value="rainy">Rainy
  </option></select></th>
 
  <th colspan="3"><select name="airtemp">
  <option value="warm">Warm
  </option><option value="cold">Cold
  </option></select></th>
 
  <th colspan="3"><select name="humidity">
  <option value="normal">Normal
  </option><option value="high">High
  </option></select></th>

  <th colspan="3"><select name="wind">
  <option value="strong">Strong
  </option><option value="slow">Slow
  </option></select></th>

  <th colspan="3"><select name="water">
  <option value="warm">Warm
  </option><option value="cool">Cool
  </option></select></th>

  <th colspan="3"><select name="forecast">
  <option value="same">Same
  </option><option value="change">Change
  </option></select></th>
 </tr>
</tbody></table>
<br />
<br />
<br />
<table align="center">
 <tbody>
<tr>
 <th><label>Input jumlah tetangga</label><input name="jumlah_tetangga" type="text" /></th>
 </tr>
<tr>
  <th><input name="submit" type="submit" value="submit" /></th>
 </tr>
</tbody></table>
</form>
<div>
<br /></div>

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">Learning Nearest Neighbor</h3>
                        </div>
   <!-- /.box-header -->
   <table id="example2" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                            <th>Jarak</th>
                            <th>EnjoySport</th>
                            </tr>
                            </thead>
                            <tbody>
       <?php for($i=0;$i<$jumteg;$i++){?>
                            <tr>
                            <td><?php echo $jaraktetangga['jarak'][$i];?></td>
                            <td><?php echo $jaraktetangga['status'][$i];?></td>
       </tr>
       <?php }?>
                            </tbody>
                        </table>
                    <div class="box-body table-responsive">
  <h1><?php echo $hasil?><h1>
  </div>
  <!-- /.box-body -->
            </div>
 <!-- /.box -->
 </div>    
 </div>
</section><!-- /.content -->
</aside><!-- /.right-side -->

Posting Komentar