PROGRAMMING, TIPS TRIK

Simple Bayes Denga PHP

Sipe berlanjut nie tulisan tentang machine learning. Mumpung lagi ada tugas tentang bayes jadi saya bagi-bagi coding yang sudah saya buat. Terlepas dari segalanya ini coding yang sudah saya buat semoga bermanfaat :

//Begin Algoritma Bayesian
    for($n=0;$n<$rnum;$n++)
          {
              //menghitung jumlah yes
              if($dtTraining[$n][6]=="yes"){
                       $nYes++;
              }
              //menghitung jumlah no
              if($dtTraining[$n][6]=="no"){
                       $nNO++;
              }
          }

    for($n=0;$n<$rnum;$n++)
          {
                    //menghitung jumlah variabel terhadapa yes dan no

                  if(($dataTest[0]==$dtTraining[$n][0])&&($dtTraining[$n][6]=='yes'))
                  {
                      $nvar1Y++;
                  }
                  else if(($dataTest[0]==$dtTraining[$n][0])&&($dtTraining[$n][6]=='no'))
                  {
                      $nvar1N++;
                  }
                  if(($dataTest[1]==$dtTraining[$n][1])&&($dtTraining[$n][6]=='yes'))
                  {
                      $nvar2Y++;
                  }
                  else if(($dataTest[1]==$dtTraining[$n][1])&&($dtTraining[$n][6]=='no'))
                  {
                      $nvar2N++;
                  }
                  if(($dataTest[2]==$dtTraining[$n][2])&&($dtTraining[$n][6]=='yes'))
                  {
                      $nvar3Y++;
                  }
                  else if(($dataTest[2]==$dtTraining[$n][2])&&($dtTraining[$n][6]=='no'))
                  {

                      $nvar3N++;
                  }
                  if(($dataTest[3]==$dtTraining[$n][3])&&($dtTraining[$n][6]=='yes'))
                  {
                      $nvar4Y++;
                  }
                  else if(($dataTest[3]==$dtTraining[$n][3])&&($dtTraining[$n][6]=='no'))
                  {
                      $nvar4N++;
                  }
                  if(($dataTest[4]==$dtTraining[$n][4])&&($dtTraining[$n][6]=='yes'))
                  {
                      $nvar5Y++;
                  }
                  else if(($dataTest[4]==$dtTraining[$n][4])&&($dtTraining[$n][6]=='no'))
                  {
                      $nvar5N++;
                  }
                  if(($dataTest[5]==$dtTraining[$n][5])&&($dtTraining[$n][6]=='yes'))
                  {
                      $nvar6Y++;
                  }
                  else if(($dataTest[5]==$dtTraining[$n][5])&&($dtTraining[$n][6]=='no'))
                  {
                      $nvar6N++;
                  }

          }

          $jumYes[0]=($nvar1Y/$rnum);
          $jumYes[1]=($nvar2Y/$rnum);
          $jumYes[2]=($nvar3Y/$rnum);
          $jumYes[3]=($nvar4Y/$rnum);
          $jumYes[4]=($nvar5Y/$rnum);
          $jumYes[5]=($nvar6Y/$rnum);

          $jumNo[0]=($nvar1N/$rnum);
          $jumNo[1]=($nvar2N/$rnum);
          $jumNo[2]=($nvar3N/$rnum);
          $jumNo[3]=($nvar4N/$rnum);
          $jumNo[4]=($nvar5N/$rnum);
          $jumNo[5]=($nvar6N/$rnum);

          //proses pengecekan jika data tidak ada dalam data training
          for($i=0;$i<6;$i++){
              if($jumNo[$i]==0){
                  $jumNo[$i]=0.0001;
              }
              if($jumYes[$i]==0){
                  $jumYes[$i]=0.0001;
              }
          }
          //perhitungan probabilitas
          $jumY = ($jumYes[0]/($nYes/$rnum))*($jumYes[1]/($nYes/$rnum))*
                  ( $jumYes[2]/($nYes/$rnum))*($jumYes[3]/($nYes/$rnum))
                  *($jumYes[4]/($nYes/$rnum))*( $jumYes[5]/($nYes/$rnum))*(($nYes/$rnum));
          $jumN = ($jumNo[0]/($nNO/$rnum))*($jumNo[1]/($nNO/$rnum))
                  *($jumNo[2]/($nNO/$rnum))*($jumNo[3]/($nNO/$rnum))
                  *( $jumNo[4]/($nNO/$rnum))*( $jumNo[5]/($nNO/$rnum))*(($nNO/$rnum));

          for($i=0;$i<6;$i++){
              $kesimpulan = "yes";
          }
          else if($jumN > $jumY){
              $kesimpulan = "no";
          }
          //
 //end bayesian

Selamat Mencoba dan mari sama-sama beljar..Kalo ada apa2 kirim lewat komment ya.makasih

Standar

3 thoughts on “Simple Bayes Denga PHP

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s