Algoritma Bayes

teori bayes
Implementasi Algoritma Bayes.
Algoritma Bayes atau lebih di kenal Bayesian merupakan algoritma yang menggunakan perhitungan probabilitas. untuk mengetahui tentang algoritma bayes lebih detail dan sejarahnya  dapat di lihat di wikipedia

Berikut adalah contoh sources Algoritma Bayes menggunakan codeigniter

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 */;


bayesController.php

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

class Bayes extends CI_Controller {

 public function index(){
 //load first view
  
  $this->load->view('bayes/ujicoba');
  
 }
 public function hasil()
 {
 //get data from method POST
 $sky=$_POST['sky'];
 $airtemp=$_POST['airtemp'];
 $humidity=$_POST['humidity'];
 $wind=$_POST['wind'];
 $water=$_POST['water'];
 $forecast=$_POST['forecast'];
 
 //data enjoy sport yes
 $SkyY=$this->dmodel->countData("where Sky='$sky' and EnjoySport='yes'")->num_rows();
 $AirTempY=$this->dmodel->countData("where AirTemp='$airtemp' and EnjoySport='yes'")->num_rows();
 $HumidityY=$this->dmodel->countData("where Humidity='$humidity' and EnjoySport='yes'")->num_rows();
 $WindY=$this->dmodel->countData("where Wind='$wind' and EnjoySport='yes'")->num_rows();
 $WaterY=$this->dmodel->countData("where Water='$water' and EnjoySport='yes'")->num_rows();
 $ForecastY=$this->dmodel->countData("where Forecast='$forecast' and EnjoySport='yes'")->num_rows();
 //data enjoy sport no
 $SkyN=$this->dmodel->countData("where Sky='$sky' and EnjoySport='no'")->num_rows();
 $AirTempN=$this->dmodel->countData("where AirTemp='$airtemp' and EnjoySport='no'")->num_rows();
 $HumidityN=$this->dmodel->countData("where Humidity='$humidity' and EnjoySport='no'")->num_rows();
 $WindN=$this->dmodel->countData("where Wind='$wind' and EnjoySport='no'")->num_rows();
 $WaterN=$this->dmodel->countData("where Water='$water' and EnjoySport='no'")->num_rows();
 $ForecastN=$this->dmodel->countData("where Forecast='$forecast' and EnjoySport='no'")->num_rows();
 
 //data all Yes
 $AllY=$this->dmodel->countData("where EnjoySport='yes'")->num_rows();
 //data all no
 $AllN=$this->dmodel->countData("where EnjoySport='no'")->num_rows();
 // data all
 $All=$this->dmodel->countData("")->num_rows();
 //perhitungan bayes
 
 //count probability per field for vaule Yes
 if(($SkyY/$AllY) == 0){
  $hasSkyY =  0.01;
 }else{
  $hasSkyY = $SkyY/$AllY;
 }
 if(($AirTempY/$AllY) == 0){
  $hasTempY = 0.01;
 }else{
  $hasTempY = $AirTempY/$AllY;
 }
 if(($HumidityY/$AllY) == 0){
  $hasHumY = 0.01;
 }else{
  $hasHumY = $HumidityY/$AllY;
 }
 if(($WindY/$AllY) == 0){
  $hasWindY = 0.01;
 }else{
  $hasWindY = $WindY/$AllY;
 }
 if(($WaterY/$AllY) == 0){
  $hasWatY = 0.01;
 }else{
  $hasWatY = $WaterY/$AllY;
 }
 if(($ForecastY/$AllY) == 0){
  $hasForeY = 0.01;
 }else{
  $hasForeY = $ForecastY/$AllY;
 }
 
 //count probability per field untuk nilai No
 if(($SkyN/$AllN) == 0){
  $hasSkyN =  0.01;
 }else{
  $hasSkyN = $SkyN/$AllN;
 }
 if(($AirTempN/$AllN) == 0){
  $hasTempN = 0.01;
 }else{
  $hasTempN = $AirTempN/$AllN;
 }
 if(($HumidityN/$AllN) == 0){
  $hasHumN = 0.01;
 }else{
  $hasHumN = $HumidityN/$AllN;
 }
 if(($WindN/$AllN) == 0){
  $hasWindN = 0.01;
 }else{
  $hasWindN = $WindN/$AllN;
 }
 if(($WaterN/$AllN) == 0){
  $hasWatN = 0.01;
 }else{
  $hasWatN = $WaterN/$AllN;
 }
 if(($ForecastN/$AllN) == 0){
  $hasForeN = 0.01;
 }else{
  $hasForeN = $ForecastN/$AllN;
 }
 // probability for data yes
 $PY=($hasSkyY)*($hasTempY)*($hasHumY)*($hasWindY)*($hasWatY)*($hasForeY)*($AllY/$All);
 //probability for data no
 $PN=($hasSkyN)*($hasTempN)*($hasHumN)*($hasWindN)*($hasWatN)*($hasForeN)*($AllN/$All);
 //if py > than pn result is good for sport
 if($PY>$PN){
 $data['SkyY']=$hasSkyY;
 $data['AirTempY']=$hasTempY;
 $data['HumidityY']=$hasHumY;
 $data['WindY']=$hasWindY;
 $data['WaterY']=$hasWatY;
 $data['ForecastY']=$hasForeY;
 $data['EnjoyY']= ($AllY/$All);
 $data['SkyN']=$hasSkyN;
 $data['AirTempN']=$hasTempN;
 $data['HumidityN']=$hasHumN;
 $data['WindN']=$hasWindN;
 $data['WaterN']=$hasWatN;
 $data['ForecastN']=$hasForeN;
 $data['EnjoyN']= ($AllN/$All);
 $data['hasil']="sangat baik untuk olahraga";
 $data['probabilitas']= 'Yes : '.$PY.' > No : '.$PN;
 //else result is not good for sport
 }
 else{
 $data['SkyY']=$hasSkyY;
 $data['AirTempY']=$hasTempY;
 $data['HumidityY']=$hasHumY;
 $data['WindY']=$hasWindY;
 $data['WaterY']=$hasWatY;
 $data['ForecastY']=$hasForeY;
 $data['EnjoyY']= ( $AllY/$All);
 $data['SkyN']=$hasSkyN;
 $data['AirTempN']=$hasTempN;
 $data['HumidityN']=$hasHumN;
 $data['WindN']=$hasWindN;
 $data['WaterN']=$hasWatN;
 $data['ForecastN']=$hasForeN;
 $data['EnjoyN']= ($AllN/$All);
 $data['probabilitas']= 'Yes : '.$PY.' < No : '.$PN;
 $data['hasil']="tidak baik untuk olahraga";
 }
  
  $this->load->view('bayes/hasil',$data);
  
 }
}

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

Dmodel.php


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

Class Dmodel extends CI_Model{

public function countData($where=''){

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

 }


View
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 Bayes</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 Bayes</h3>
                                </div><!-- /.box-header -->
         <table id="example2" class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th>Sky</th>
                                                <th>Air Temperature</th>
                                                <th>Humidity</th>
                                                <th>Wind</th>
                                                <th>Water</th>
            <th>Forecast</th>
            <th>EnjoySport</th>
            
                                            </tr>
                                        </thead>
                                        <tbody>
          
                                            <tr>
                                                <td><?php echo $SkyY;?></td>
                                                <td><?php echo $AirTempY;?></td>
                                                <td><?php echo $HumidityY;?></td>
                                                <td><?php echo $WindY;?></td>
                                                <td><?php echo $WaterY;?></td>
            <td><?php echo $ForecastY;?></td>
            <td><?php echo $EnjoyY;?></td>
            </tr>
            <tr>
                                                <td><?php echo $SkyN;?></td>
                                                <td><?php echo $AirTempN;?></td>
                                                <td><?php echo $HumidityN;?></td>
                                                <td><?php echo $WindN;?></td>
                                                <td><?php echo $WaterN;?></td>
            <td><?php echo $ForecastN;?></td>
            <td><?php echo $EnjoyN;?></td>
            </tr>
                                       
                                        </tbody>
                                       
                                    </table>
                                <div class="box-body table-responsive">
        <h1><?php echo $hasil?><h1>
        <h2>Perbandingan probabilitas : <?php echo $probabilitas;?></h2>
        </div><!-- /.box-body -->
                            </div><!-- /.box -->
       </div>
                    </div>

                </section><!-- /.content -->
            </aside><!-- /.right-side -->                

ujicoba.php

<h4>Tentukan Fakta yang ada:</h4>
<form name="form1" method="post" action="<?php echo base_url();?>index.php/bayes/hasil">
<table border="0" align="center">
 <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 value="rainy">Rainy
  </select></th>
 
  <th colspan="3">
  <select name="airtemp">
  <option value="warm">Warm
  <option value="cold">Cold
  </select></th>
 
  <th colspan="3">
  <select name="humidity">
  <option value="normal">Normal
  <option value="high">High
  </select></th>

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

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

  <th colspan="3">
  <select name="forecast">
  <option value="same">Same
  <option value="change">Change
  </select></th>
 </tr>
 
</table>
<br><br><br>
<table align="center">
 <tr>
  <th><input type="submit" value="submit" name="submit"></th>
 </tr>
</table>
</form>

video untuk Algoritma Bayes

Posting Komentar