Aprori算法[关联规则算法]

Aprori算法[关联规则算法]Aprori算法[关联规则算法]

 <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<?php
/*
*Apriori算法(关联规则算法的实现)
*/ /*
*项目集X支持G个数小于等于1去除
*/
//--------------------------------------------------------------------
function first($train)
{
$new_array = $train;
$array_count = array(NULL);
array_splice($array_count,0,1);
for($i=1;$i<count($train[0]);$i++)
{
$count = 0;
for($j=1;$j<count($train);$j++)
{
if($train[$j][$i]==1)
{
$count++;
}
} if($count==1){//非频繁项目集
array_push($array_count,$i);
}
} for($i=count($array_count)-1;$i>=0;$i--)
{
for($j=0;$j<count($train);$j++)
{
array_splice($new_array[$j],$array_count[$i],1);
} } return $new_array;
}
//-------------------------------------------------------------------- /*
*总事务D中包含事务X的数量
*/
//--------------------------------------------------------------------
function search($train,$array)
{
$array_num = array();
array_splice($array_num,0,1);
for($i=0;$i<count($array);$i++)
{
for($j=1;$j<count($train[0]);$j++)
{ if($train[0][$j]==$array[$i])
{
array_push($array_num,$j);
break;
}
}
}
$count = 0;//用于统计满足数据项集X的事务个数
for($i=1;$i<count($train);$i++)
{
$flags = true;
for($j=0;$j<count($array_num);$j++)
{
if($train[$i][$array_num[$j]]==0)
{
$flags = false;
break;
}
}
if($flags)$count++; }
return $count;
}
//-------------------------------------------------------------------- /*
*合并两数组,除去其中相同的元素
*/
//--------------------------------------------------------------------
function merge($arrayA,$arrayB)
{
$array = array(NULL);
array_splice($array,0,1); //将两数组中的元素合并
for($i=0;$i<count($arrayA)+count($arrayB);$i++)
{
if($i<count($arrayA))
{
array_push($array,$arrayA[$i]);
}
else {
array_push($array,$arrayB[$i-count($arrayA)]);
}
} $array = array_unique($array);//删除两数组中重复的元素 //对新的数组下标重新进行排序
foreach($array as $value)
{
$newarray[]=$value;
}
return $newarray;
}
//-------------------------------------------------------------------- /*
*判断两个一维数组是否相等(不考虑键的位置,也不考虑数组中有重复的元素)
*/
//--------------------------------------------------------------------
function judgeequal($arrayA,$arrayB)
{ if(count($arrayA)!=count($arrayB))
{
$flags = false;
}else{
$flags2 = true;
for($i=0;$i<count($arrayA);$i++)
{
if(!in_array($arrayA[$i],$arrayB,true))
{
$flags2 = false;
break;
}
}
if($flags2)
{
$flags = true;
}else {
$flags = false;
}
}
return $flags;
}
//-------------------------------------------------------------------- /*
*求支持度和置信度
*/
//--------------------------------------------------------------------
function support_confidence($arrayA,$arrayB,$train,&$support,&$confidence)
{
$newarray = merge($arrayA,$arrayB);
$countxy = search($train,$newarray);
$support = $countxy / (count($train)-1);//项集X的支持度
$countx = search($train,$arrayA);
$confidence = $countxy / $countx;
return 0; }
//-------------------------------------------------------------------- /*
*Apriori算法
*/
//--------------------------------------------------------------------
function Apriori($train,$sup,$con)
{
$aprioriAll = array();//存储所有的关联
array_splice($aprioriAll,0,1);
$apriori = array();
array_splice($apriori,0,1);
$train = first($train);
for($i=1;$i<count($train[0])-1;$i++)
{
$arrayA[0] =$train[0][$i];
for($j=$i+1;$j<count($train[0]);$j++)
{
$arrayB[0] =$train[0][$j];
support_confidence($arrayA,$arrayB,$train,$support,$confidence);
if(($support>=$sup)&&($confidence>=$con))
{
$Meg = merge($arrayA,$arrayB);
array_push($apriori,$Meg);
}
}
}
array_push($aprioriAll,$apriori);
while(count($apriori)>1)
{
$array = array();
array_splice($array,0,1);
for($i=0;$i<count($apriori)-1;$i++)
{
for($j=$i+1;$j<count($apriori);$j++)
{
$arrayB = merge($apriori[$i],$apriori[$j]);
support_confidence($apriori[$i],$arrayB,$train,$support,$confidence);
if(($support>=$sup)&&($confidence>=$con))
{
/* echo "<pre>";
print_r($arrayA);
echo "<pre>";
print_r($arrayB);
*/
array_push($array,$arrayB);
} }
}
$apriori = $array;
for($i=0;$i<count($apriori)-1;$i++)
{
for($j=count($apriori)-1;$j>=$i+1;$j--)
{
if(judgeequal($apriori[$i],$apriori[$j]))
{
array_splice($apriori,$j,1);
}
}
}
foreach($apriori as $value)
{
$newarray[]=$value;
}
$apriori = $newarray;
array_push($aprioriAll,$apriori);
}
return $aprioriAll;
}
//-------------------------------------------------------------------- /*
*把.txt中的内容读到数组中保存
*$filename:文件名称
*/
//--------------------------------------------------------------------
function getFileContent($filename)
{
$array = array(null);
$content = file_get_contents($filename);
$result = explode("\r\n",$content);
//print_r(count($result));
for($j=0;$j<count($result);$j++)
{
//print_r($result[$j]."<br>");
$con = explode(" ",$result[$j]);
array_push($array,$con);
}
array_splice($array,0,1);
return $array;
}
//-------------------------------------------------------------------- /*
*把数组中内容写到.txt中保存
*$result:要存储的数组内容
*$filename:文件名称
*/
//--------------------------------------------------------------------
function Array_Totxt($result,$filename)
{
$fp= fopen($filename,'wb');
for($i=0;$i<count($result);$i++)
{ for($j=0;$j<count($result[$i]);$j++)
{ $temp = NULL;
for($k=0;$k<count($result[$i][$j]);$k++){
$temp = $result[$i][$j][$k]."\t";
fwrite($fp,$temp);
}
fwrite($fp,"\r\n");
} }
fclose($fp);
}
//--------------------------------------------------------------------
$train = getFileContent("train.txt");
// $train = getFileContent("er.txt");
$apriori = Apriori($train,0.5,0.6);
echo "<pre>";
print_r($apriori);
Array_Totxt($apriori,"result.txt")
// Array_Totxt($apriori,"erresult.txt")
?>

Aprori算法[关联规则算法]Aprori算法[关联规则算法]Aprori算法[关联规则算法]Aprori算法[关联规则算法]Aprori算法[关联规则算法]

上一篇:PHP 底层的运行机制与原理


下一篇:【Python3之socket编程】