Advertisement
cahyadsn

apriori algorithm (example - not finished)

Sep 15th, 2012
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.53 KB | None | 0 0
  1. <?php
  2. //-------------------
  3. function combination($member,$num){
  4.   $n = count($member);  
  5.   //jumlah total kombinasi yang mungkin
  6.   $total = pow(2, $n);
  7.   $list =array();
  8.   //Loop untuk setiap kombinasi yang mungkin
  9.   $k=0;
  10.   for ($i = 0; $i < $total; $i++) {  
  11.     $list[$k]=array();
  12.     //Untuk setiap kombinasi, dicek untuk setiap bit
  13.     for ($j = 0; $j < $total; $j++) {  
  14.        //apakah data bit $j ada pada bit $i?  
  15.         if ((pow(2, $j) & $i)) $list[$k][]=$member[$j];      
  16.     }
  17.     if(count($list[$k])==$num){
  18.       //jika jumlah anggota sama dengan jumlah yang diinginkan increment $k
  19.       $k++;
  20.     }else{
  21.       //jika tidak sesuai, hapus array $list[$k]
  22.       unset($list[$k]);
  23.     }
  24.   }
  25.   return $list;
  26. }
  27. //-------------------
  28. function array_composition($list){
  29.   $r=$list[0];
  30.   for($i=1;$i<count($list);$i++){
  31.     $r=array_merge($r,$list[$i]);
  32.   }
  33.   $r=array_values(array_unique($r));
  34.   sort($r);
  35.   return $r;
  36. }
  37. //--------------------
  38. function build_candidate($data,$level){
  39.   $result=combination($data,$level);
  40.   sort($result);
  41.   return $result;
  42. }
  43. //-----------------------
  44. function join_step($source,$data){
  45.   $n=count($data[0]);
  46.   for($i=0;$i<count($data);$i++){
  47.     for($j=0;$j<count($source);$j++){
  48.       $freq=array_intersect($source[$j],$data[$i]);
  49.       $support[$i]=(count($freq)==count($data[$i])?(isset($support[$i])?$support[$i]+1:1):(isset($support[$i])?$support[$i]:0));
  50.     }
  51.   }
  52.   $result=array($data,$support);
  53.   return $result;
  54. }
  55. //-----------------------
  56. function prune_data($data,$min_support){
  57.   $result=array();
  58.   foreach ($data[1] as $key=>$value){
  59.     if($value<=$min_support){
  60.       unset($data[0][$key]);
  61.       unset($data[1][$key]);
  62.     }else{
  63.       $result[0][]=$data[0][$key];
  64.       $result[1][]=$data[1][$key];
  65.     }
  66.   }
  67.   return $result;
  68. }
  69. //-----------------------
  70. function print_transaction($data,$title=NULL){
  71.   echo (isset($title)?"<h2>Transaction for ".$title."</h2>\n":"");
  72.   echo "<div class=\"table\">\n"
  73.       ."<span><div class=\"header\">Trans No</div><div class=\"header\">Items</div></span>\n";
  74.   for($i=0;$i<count($data);$i++){
  75.     echo "<span><div class=\"cell\">".($i+1)."</div><div class=\"cell\">{";
  76.     for($j=0;$j<count($data[$i]);$j++){
  77.       echo ($j==0?"":",").$data[$i][$j];
  78.     }
  79.     echo "}</div></span>\n";
  80.   }    
  81.   echo "</div><br>\n";  
  82. }
  83. //-----------------------
  84. function print_itemset($data,$level,$title=NULL){
  85.   echo (isset($title)?"<h2>Candidate C-".$level."</h2>\n":"");
  86.   echo "<div class=\"table1\">\n"
  87.       ."<span><div class=\"header\">".$level."-Itemset</div></span>\n";
  88.   for($i=0;$i<count($data);$i++){
  89.     echo "<span><div class=\"cell\">{";
  90.     for($j=0;$j<count($data[$i]);$j++){
  91.       echo ($j==0?"":",").$data[$i][$j];
  92.     }
  93.     echo "}</div></span>\n";
  94.   }    
  95.   echo "</div><br>\n";
  96. }
  97. //-----------------------
  98. function print_support($data,$level,$title=NULL){
  99.   echo (isset($title)?"<h2>".$title." ".$level."</h2>\n":"");
  100.   if(!empty($data)){
  101.     echo "<div class=\"table\">\n"
  102.         ."<span><div class=\"header\">".$level."-Itemset</div><div class=\"header\">Count</div></span>\n";
  103.     for($i=0;$i<count($data[0]);$i++){
  104.       echo "<span><div class=\"cell\">{";
  105.       for($j=0;$j<count($data[0][$i]);$j++){
  106.         echo ($j==0?"":",").$data[0][$i][$j];
  107.       }
  108.       echo "}</div><div class=\"cell\">".$data[1][$i]."</div></span>\n";
  109.     }    
  110.     echo "</div><br>\n";
  111.   }
  112. }
  113. /***************************/
  114. function apriori($data,$title=NULL){
  115.   echo isset($title)?"<h1>".$title."</h1>":"";
  116.   //echo "<pre>";print_r($data);echo "</pre>";
  117.   print_transaction($data,$title);
  118.   $result[0]=$data;
  119.   $step=1;
  120.   $frequent_item=array(array(),array());
  121.   do{
  122.     $c=array_composition($result[0]);
  123.     //echo "<pre>c-->";print_r($c);echo "</pre>";
  124.     $bl=build_candidate($c,$step);
  125.     //echo "<pre>b_$step-->";print_r($bl);echo "</pre>";
  126.     print_itemset($bl,$step,1);
  127.     $jo=join_step($data,$bl);
  128.     //echo "<pre>j_$step-->";print_r($jo);echo "</pre>";
  129.     print_support($jo,$step,"Join C-");
  130.     $result=prune_data($jo,MIN_SUPPORT);
  131.     $frequent_item[0]=array_merge($frequent_item[0],$result[0]);
  132.     $frequent_item[1]=array_merge($frequent_item[1],$result[1]);
  133.     //echo "<pre>p_$step-->";print_r($result);echo "</pre>";  
  134.     print_support($result,$step,"Prune L-");
  135.     $step++;
  136.   }while(!empty($result) && count($result[0])>1);
  137.   //echo "<pre>frequent_item-->";print_r($frequent_item);echo "</pre>";
  138. }
  139. ?>
  140. <!doctype html>
  141. <head>
  142. <title>Apriori Algorithm</title>
  143. <style type="text/css">
  144. span.row {
  145.  width:100%;
  146.  display: table-row;
  147. }
  148. div.table{
  149.  border:solid 1px #999;
  150.  width:200px;
  151.  text-align:center;
  152.  padding:2px;
  153. }
  154. div.table1{
  155.  border:solid 1px #999;
  156.  width:100px;
  157.  text-align:center;
  158.  padding:2px;
  159. }
  160. div.header{
  161.  border:solid 1px #999;
  162.  color:#eee;
  163.  background-color:#333;
  164.  font-weight:bold;
  165.  text-align:center;
  166.  width:100px;
  167.  height:20px;
  168.  display: table-cell;
  169.  padding:2px;
  170. }
  171. div.cell {
  172.  border:dashed 1px #eee;
  173.  text-align:center;
  174.  width:100px;
  175.  height:20px;
  176.  display: table-cell;
  177.  padding:2px;
  178. }
  179. </style>
  180. <body>
  181. <div class="container">
  182. <?php
  183. /************************/
  184. define('MIN_SUPPORT',1);
  185. //-------- EXAMPLE -------
  186. $data1=array(
  187.        array('a','c','d'),
  188.        array('b','c','e'),
  189.        array('a','b','c','e'),
  190.        array('b','e')
  191.       );
  192. apriori($data1,'Example #1');
  193. $data2=array(
  194.        array(1,3,4),
  195.        array(2,3,5),
  196.        array(1,2,5),
  197.        array(2,5)
  198.       );
  199. apriori($data2,'Example #2');
  200. ?>
  201. </div>
  202. </body>
  203. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement