Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-------------------
- function combination($member,$num){
- $n = count($member);
- //jumlah total kombinasi yang mungkin
- $total = pow(2, $n);
- $list =array();
- //Loop untuk setiap kombinasi yang mungkin
- $k=0;
- for ($i = 0; $i < $total; $i++) {
- $list[$k]=array();
- //Untuk setiap kombinasi, dicek untuk setiap bit
- for ($j = 0; $j < $total; $j++) {
- //apakah data bit $j ada pada bit $i?
- if ((pow(2, $j) & $i)) $list[$k][]=$member[$j];
- }
- if(count($list[$k])==$num){
- //jika jumlah anggota sama dengan jumlah yang diinginkan increment $k
- $k++;
- }else{
- //jika tidak sesuai, hapus array $list[$k]
- unset($list[$k]);
- }
- }
- return $list;
- }
- //-------------------
- function array_composition($list){
- $r=$list[0];
- for($i=1;$i<count($list);$i++){
- $r=array_merge($r,$list[$i]);
- }
- $r=array_values(array_unique($r));
- sort($r);
- return $r;
- }
- //--------------------
- function build_candidate($data,$level){
- $result=combination($data,$level);
- sort($result);
- return $result;
- }
- //-----------------------
- function join_step($source,$data){
- $n=count($data[0]);
- for($i=0;$i<count($data);$i++){
- for($j=0;$j<count($source);$j++){
- $freq=array_intersect($source[$j],$data[$i]);
- $support[$i]=(count($freq)==count($data[$i])?(isset($support[$i])?$support[$i]+1:1):(isset($support[$i])?$support[$i]:0));
- }
- }
- $result=array($data,$support);
- return $result;
- }
- //-----------------------
- function prune_data($data,$min_support){
- $result=array();
- foreach ($data[1] as $key=>$value){
- if($value<=$min_support){
- unset($data[0][$key]);
- unset($data[1][$key]);
- }else{
- $result[0][]=$data[0][$key];
- $result[1][]=$data[1][$key];
- }
- }
- return $result;
- }
- //-----------------------
- function print_transaction($data,$title=NULL){
- echo (isset($title)?"<h2>Transaction for ".$title."</h2>\n":"");
- echo "<div class=\"table\">\n"
- ."<span><div class=\"header\">Trans No</div><div class=\"header\">Items</div></span>\n";
- for($i=0;$i<count($data);$i++){
- echo "<span><div class=\"cell\">".($i+1)."</div><div class=\"cell\">{";
- for($j=0;$j<count($data[$i]);$j++){
- echo ($j==0?"":",").$data[$i][$j];
- }
- echo "}</div></span>\n";
- }
- echo "</div><br>\n";
- }
- //-----------------------
- function print_itemset($data,$level,$title=NULL){
- echo (isset($title)?"<h2>Candidate C-".$level."</h2>\n":"");
- echo "<div class=\"table1\">\n"
- ."<span><div class=\"header\">".$level."-Itemset</div></span>\n";
- for($i=0;$i<count($data);$i++){
- echo "<span><div class=\"cell\">{";
- for($j=0;$j<count($data[$i]);$j++){
- echo ($j==0?"":",").$data[$i][$j];
- }
- echo "}</div></span>\n";
- }
- echo "</div><br>\n";
- }
- //-----------------------
- function print_support($data,$level,$title=NULL){
- echo (isset($title)?"<h2>".$title." ".$level."</h2>\n":"");
- if(!empty($data)){
- echo "<div class=\"table\">\n"
- ."<span><div class=\"header\">".$level."-Itemset</div><div class=\"header\">Count</div></span>\n";
- for($i=0;$i<count($data[0]);$i++){
- echo "<span><div class=\"cell\">{";
- for($j=0;$j<count($data[0][$i]);$j++){
- echo ($j==0?"":",").$data[0][$i][$j];
- }
- echo "}</div><div class=\"cell\">".$data[1][$i]."</div></span>\n";
- }
- echo "</div><br>\n";
- }
- }
- /***************************/
- function apriori($data,$title=NULL){
- echo isset($title)?"<h1>".$title."</h1>":"";
- //echo "<pre>";print_r($data);echo "</pre>";
- print_transaction($data,$title);
- $result[0]=$data;
- $step=1;
- $frequent_item=array(array(),array());
- do{
- $c=array_composition($result[0]);
- //echo "<pre>c-->";print_r($c);echo "</pre>";
- $bl=build_candidate($c,$step);
- //echo "<pre>b_$step-->";print_r($bl);echo "</pre>";
- print_itemset($bl,$step,1);
- $jo=join_step($data,$bl);
- //echo "<pre>j_$step-->";print_r($jo);echo "</pre>";
- print_support($jo,$step,"Join C-");
- $result=prune_data($jo,MIN_SUPPORT);
- $frequent_item[0]=array_merge($frequent_item[0],$result[0]);
- $frequent_item[1]=array_merge($frequent_item[1],$result[1]);
- //echo "<pre>p_$step-->";print_r($result);echo "</pre>";
- print_support($result,$step,"Prune L-");
- $step++;
- }while(!empty($result) && count($result[0])>1);
- //echo "<pre>frequent_item-->";print_r($frequent_item);echo "</pre>";
- }
- ?>
- <!doctype html>
- <head>
- <title>Apriori Algorithm</title>
- <style type="text/css">
- span.row {
- width:100%;
- display: table-row;
- }
- div.table{
- border:solid 1px #999;
- width:200px;
- text-align:center;
- padding:2px;
- }
- div.table1{
- border:solid 1px #999;
- width:100px;
- text-align:center;
- padding:2px;
- }
- div.header{
- border:solid 1px #999;
- color:#eee;
- background-color:#333;
- font-weight:bold;
- text-align:center;
- width:100px;
- height:20px;
- display: table-cell;
- padding:2px;
- }
- div.cell {
- border:dashed 1px #eee;
- text-align:center;
- width:100px;
- height:20px;
- display: table-cell;
- padding:2px;
- }
- </style>
- <body>
- <div class="container">
- <?php
- /************************/
- define('MIN_SUPPORT',1);
- //-------- EXAMPLE -------
- $data1=array(
- array('a','c','d'),
- array('b','c','e'),
- array('a','b','c','e'),
- array('b','e')
- );
- apriori($data1,'Example #1');
- $data2=array(
- array(1,3,4),
- array(2,3,5),
- array(1,2,5),
- array(2,5)
- );
- apriori($data2,'Example #2');
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement