网站首页
休闲人生
科学探索
研究项目
闲散文字
诗词歌赋
职业经历
大千光影
移动浏览
访客留言
个人相册
用于作业辅助分析的程序源代码
大
|
中
|
小
|
October 4, 2006 @ 12:52,
Robin Hoo
data.php
<?php $support=0.2; $confidence=0.7; $data_file="data.csv"; //For question 1.b) //$data_file="data2006.csv"; $cmd_array=read_data($data_file); foreach($cmd_array as $cmd) eval($cmd); function read_data($file) { $handle = @fopen($file, "rb"); $result_array=array(); $item=array(); if (!$handle) die("Can not open data file!\n"); $buffer=fgets($handle,4096); $result_array[]="\$item=explode(',','".str_replace(" ","",trim($buffer))."');"; $item=explode(",",str_replace(" ","",trim($buffer))); while (!feof($handle)) { $buffer = fgets($handle, 4096); if (trim($buffer)=="") continue; $temp_array=explode(",",trim($buffer)); if (count($temp_array)!=count($item)) continue; for($i=0;$i<count($item);$i++) { $result_array[]="\$".$item[$i]."[]='".trim($temp_array[$i])."';"; } } fclose($handle); return $result_array; } ?>
assignment.php
<?php require_once("data.php"); $total=count($$item[0]); $result_array=array(); $step=count($item); print "Aprori\n"; print "=======================================================\n"; for($i=0;$i<$step;$i++) { $temp_array=array(); $cmd_array=apriori($item,count($item),$i+1); foreach($cmd_array as $cmd) eval($cmd); if (count($temp_array)==0) { if (count($result_array)==0) die("No interesting item found!\n"); break; } $temp_array=output_array($temp_array,$i+1,"C",$support,$total); if (count($temp_array)==0) { if (count($result_array)==0) die("No interesting item found!\n"); break; } $result_array=array(); $result_array=output_array($temp_array,$i+1,"L",$support,$total); $item=array(); $keys=array_keys($result_array); foreach($keys as $item[]) $item=array_unique($item); sort($item); } print "=======================================================\n\n"; print "Seek Rules\n"; $temp_array=array(); $temp_array=$result_array; $cmd_array=seek_rules($result_array,$i); $result_array=array(); foreach($cmd_array as $cmd) eval($cmd); output_rules($result_array,$confidence); $temp_array=array(); for($i=0;$i<count($result_array);$i++) if ($result_array[$i][3]>=$confidence*100) $temp_array[]=$result_array[$i]; output_rules($temp_array,$confidence); function filter($var) { return ($var=='T'); } function cmp($a, $b) { if ($a[2] == $b[2]) { return cmpa($a,$b); } return ($a[2] < $b[2]) ? 1 : -1; } function cmpa($a, $b) { if ($a[3] == $b[3]) { return 0; } return ($a[3] < $b[3]) ? 1 : -1; } function apriori($array,$number,$step) { $keys=array_values($array); for($i=0;$i<count($keys);$i++) $keys[$i]=explode(",",trim($keys[$i])); if ($step!=1) $keys=join_array($keys); $result_array=array(); for($i=0;$i<count($keys);$i++) { $temp_array=$keys[$i]; $cmd="\$temp_array['".$temp_array[0]; for($j=1;$j<count($temp_array);$j++) $cmd.=",".$temp_array[$j]; $cmd.="']=count(array_filter("; if (count($keys[$i])>1) { $cmd.="array_intersect_assoc(\$".$temp_array[0]; for($j=1;$j<count($temp_array);$j++) $cmd.=",\$".$temp_array[$j]; $cmd.=")"; } else $cmd.="\$".$temp_array[0]; $cmd.=",\"filter\"));"; $result_array[]=$cmd; } return $result_array; } function join_array($array) { $result_array=array(); for($i=0;$i<count($array)-1;$i++) { for($j=$i+1;$j<count($array);$j++) { $sw=true; $temp_array=$array[$i]; $temp_array[]=$array[$j][count($array[$j])-1]; for($k=0;$k<count($array[$i])-1;$k++) if ($array[$i][$k]!=$array[$j][$k]) $sw=false; if ($sw and $array[$i][count($array[$i])-1]!=$array[$j][count($array[$j])-1]) { $result_array[]=$temp_array; } } } $r=count($array[0]); $n=$r+1; if ($r<3) return $result_array; $temp_array=combination($n,$r); $myresult=array(); foreach($result_array as $myarray) { $insert=true; foreach($temp_array as $combine) { $temp=array(); $temp=explode(" ",trim($combine)); $tmp=array(); foreach($temp as $item) { $tmp[]=$myarray[$item]; } if (array_search($tmp,$array)===false) { $insert=false; break; } } if ($insert) $myresult[]=$myarray; } return $myresult; } function combination($n,$r) { for($i=0;$i<$r;$i++) $stack[$i]=$i; $array=array(); if ($n<$r) return $array; while($stack[0]!=$n-$r) { $string=""; $insert=true; for($i=0;$i<$r;$i++) if ($stack[$i]>$n-$r+$i) $insert=false; if($insert) { for($i=0;$i<$r;$i++) $string.=$stack[$i]." "; $array[]=trim($string); } $pointer=$r-1; $stack[$pointer]++; while($stack[$pointer]>$n-$r+$pointer+1) { $pointer--; if ($pointer<0) break; $stack[$pointer]++; for($i=$pointer+1;$i<$r;$i++) $stack[$i]=$stack[$i-1]+1; } } for($i=0;$i<$r;$i++) if ($stack[$i]>$n-$r+$i) return $array; $string=""; for($i=0;$i<$r;$i++) $string.=$stack[$i]." "; $array[]=trim($string); return $array; } function output_array($array,$step,$type,$support,$number) { $maxkeylen=0; $maxvaluelen=0; $maxdeclen=0; foreach($array as $key=>$value) { if (strlen($key)>$maxkeylen) $maxkeylen=strlen($key); if (strlen($value)>$maxvaluelen) $maxvaluelen=strlen($value); if (strlen($value)-strlen(floor($value))-1>$maxdeclen) $maxdeclen=strlen($value)-strlen(floor($value))-1; } print $type.$step.":ITEM SET".str_repeat(" ",$maxkeylen+7-strlen($type.$step.":ITEM SET")).str_pad("SUP.",$maxvaluelen+8," ",STR_PAD_BOTH)."\n"; print str_repeat("=",$maxkeylen+$maxvaluelen+15)."\n"; foreach($array as $key=>$value) { print str_pad("{".$key."}",$maxkeylen+4," ",STR_PAD_RIGHT)." ".str_pad($value,$maxvaluelen," ",STR_PAD_LEFT).str_pad(number_format($value/$number*100, 2, '.', '')."%",8," ",STR_PAD_LEFT); if ($value<$number*$support) { print "*"; unset($array[$key]); } print "\n"; } print str_repeat("=",$maxkeylen+$maxvaluelen+15)."\n\n"; asort($array); return $array; } function seek_rules($array,$step) { $keys=array_keys($array); $result_array=array(); foreach($keys as $value) { $item=array(); $temp_array=array(); $temp_array=explode(",",$value); foreach($temp_array as $item[]); $temp_array=array(); sort($item); for($i=0;$i<count($item)-1;$i++) { $temp=map_keys(combination(count($item),$i+1),$item); foreach($temp as $temp_array[]); } for($i=0;$i<count($temp_array);$i++) { $cmd="\$percnt1=count(array_filter("; if (strpos($temp_array[$i],",")!==false) { $cmd.="array_intersect_assoc(\$".str_replace(",",",\$",$temp_array[$i]).")"; } else $cmd.="\$".$temp_array[$i]; $result_array[]=$cmd.",\"filter\"));"; $cmd="\$percnt2=count(array_filter("; if (strpos(map_rest($temp_array[$i],$item),",")!==false) { $cmd.="array_intersect_assoc(\$".str_replace(",",",\$",map_rest($temp_array[$i],$item)).")"; } else $cmd.="\$".map_rest($temp_array[$i],$item); $result_array[]=$cmd.",\"filter\"));"; $result_array[]="\$percnt1/=\$total;"; $result_array[]="\$percnt2/=\$total;"; $result_array[]="\$result_array[]=array('".$temp_array[$i]."','".map_rest($temp_array[$i],$item)."',round(\$percnt2*100,2),round(\$temp_array['".$value."']/\$total/\$percnt1*100,2),round(\$temp_array['".$value."']/\$total/\$percnt1/\$percnt2,2),round(\$temp_array['".$value."']/\$total*100,2));"; } } return $result_array; } function map_keys($array,$items) { $result_array=array(); foreach($array as $value) { $temp_array=array(); $temp_array=explode(" ",$value); $string=""; $string.=$items[$temp_array[0]]; for($i=1;$i<count($temp_array);$i++) { $string.=",".$items[$temp_array[$i]]; } $result_array[]=trim($string); } return $result_array; } function map_rest($value,$items) { $temp_array=array(); $temp_array=explode(",",$value); $string=""; for($i=0;$i<count($items);$i++) { if (array_search($items[$i],$temp_array)===false) $string.=$items[$i].","; } return trim(substr($string,0,strlen(trim($string))-1)); } function output_rules($array,$confidence) { $maxlen=0; for($i=0;$i<count($array);$i++) if ($maxlen<strlen($array[$i][0])) $maxlen=strlen($array[$i][0]); for($i=0;$i<count($array);$i++) if ($maxlen<strlen($array[$i][1])) $maxlen=strlen($array[$i][1]); print str_repeat(" ",strlen(count($array)))." ".str_pad("LHS",$maxlen," ",STR_PAD_BOTH)." ".str_pad("RHS",$maxlen," ",STR_PAD_BOTH)."Exp Conf(%) Conf (%) Life Supp(%)\n"; print str_repeat("=",2*$maxlen+5+strlen(count($array))+strlen("Exp Conf(%) Conf (%) Life Supp(%)"))."\n"; for($i=0;$i<count($array);$i++) { print str_pad($i+1,strlen(count($array))," ",STR_PAD_LEFT)." "; print str_pad($array[$i][0],$maxlen," ",STR_PAD_RIGHT)." -> ".str_pad($array[$i][1],$maxlen," ",STR_PAD_RIGHT)." ".str_pad(number_format($array[$i][2], 2, '.', ''),6," ",STR_PAD_LEFT)."% ".str_pad(number_format($array[$i][3], 2, '.', ''),6," ",STR_PAD_LEFT)."% ".str_pad(number_format($array[$i][4], 2, '.', ''),6," ",STR_PAD_LEFT)." ".str_pad(number_format($array[$i][5], 2, '.', ''),6," ",STR_PAD_LEFT)."%"; if ($array[$i][3]>=$confidence*100) print "*"; print "\n"; } print str_repeat("=",2*$maxlen+5+strlen(count($array))+strlen("Exp Conf(%) Conf (%) Life Supp(%)"))."\n\n"; } ?>
最后编辑: Robin Hoo 编辑于2007/08/12 06:54
文章来自: 本站原创
分类:
研究项目
»
零碎杂凑
|
评论(0)
|
引用(0)
|
阅读(4042)
引用地址:
注意:
该地址仅在今日23:59:59之前有效
收藏到 [
天天
] [
和讯
] [
我摘
] [
天极
] [
新浪
] [
博采
] [
Del.icio.us
]
上一篇:
快速生成组合序列算法[备忘]
下一篇:
辅助分析代码运行结果
0 comment(s)
发表评论
表情
打开HTML
打开UBB
打开表情
隐藏
昵称
密码
游客无需密码
网址
电邮
[注册]
验证码
不区分大小写
音乐在线
简繁转换
转换到繁體中文
文档分类
科学探索
自然科学
社会人文
研究项目
CJK2PS项目
站点镜像
天马行空
零碎杂凑
职业经历
闲散文字
集文记事
行吟纪实
五味陈杂
诗词歌赋
休闲人生
美食厨艺
职场规则
娱乐休闲
数码人生
管理运筹
结绳记事
大千光影
日志月历
<
2009
>
<
1
>
戊子年(鼠)/己丑年(牛)
日
一
二
三
四
五
六
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
访问统计
访问次数 700970
今日访问 134
日志数量 136
评论数量 23
引用数量 0
留言数量 7
注册用户 0
在线人数 4
最新日志
摄影师拍到南非松鼠表演中...
代码人生——A*算法编程...
关于OpenSUSE带g...
Ubuntu gfxme...
GFXBOOT菜单中文显...
随机日志
无题-旧日酒后戏作
安全抵达日本成田机场(2...
又是五月天
更新纪录
[转载]时间旅行
站内搜索
日志标题
日志全文
评论引用
所有留言
Tags
阿里妈妈广告
访客来源
友情链接
文字联结
Bo-Blog
BluePub
CJK2PS项目
无云天
长云的博客
Lear's Blog
图片联结
天气预报
其它杂项
用户登录
注册
申请链接
RSS:
日志
|
评论
编码:UTF-8
XHTML 1.0
谷歌广告