数据挖掘实验三应用 Apriori 算法挖掘频繁项集 联系客服

发布时间 : 星期五 文章数据挖掘实验三应用 Apriori 算法挖掘频繁项集更新完毕开始阅读

}

map::const_iterator map_it=items_count.begin(); cout<<\候选项集1:\

while(map_it!=items_count.end()) //输出候选1项集 {

cout<<\ map_it++; }

cout<<\ getchar();

map_it=items_count.begin();

cout<<\频繁1项集(minsup=2):\

while(map_it!=items_count.end()) //频繁1项集 {

if(map_it->second>minsup) //支持度大于2 {

cout.setf(ios::fixed); cout<<\支持度:\>second<

item.push_back(map_it->first);

frequentvec.push_back(item); //插入候选项集的vector中 item.clear(); }

map_it++; }

if(!frequentvec.empty()) //判断频繁项集是否为空,为空则退出 {

cout<<\ getchar();

int round=1; //生成候选项集轮次

int found; //是否包含有非频繁的子集,为表示含有,有的话进行剪枝 string tempstr;

vector tempvec; do {

//生成下一轮的候选项集

vector >::size_type st=frequentvec.size(); candidatevec.clear(); //清除上一轮的候选项集

for(vector >::size_type st1=0;st1

for(vector >::size_type st2=st1+1;st2

found=0;

item=mergeItem(frequentvec[st1],frequentvec[st2],round); //调用函数合

并生成下一轮的候选项集

if(!item.empty()&&!isExist(item,candidatevec)) //若经过判断处理后返回的vector不为空且还不存在该项集,则作为候选项集加入候选vector中

{

////////实现剪枝////////////////////////// string teststr; int testint; tempvec=item;

sort(tempvec.begin(),tempvec.end());

while(next_permutation(tempvec.begin(),tempvec.end())) //遍历所有的组合 {

for(vector::size_type

tempst=0;tempst!=tempvec.size();tempst++) //拼接出该字符串组合

{

tempstr+=tempvec[tempst]; }

for(map::const_iterator

tempit=items_count.begin();tempit!=items_count.end();tempit++)

{

if(tempit->second

if(tempstr.find(tempit->first)!=string::npos) //表示包含有非频繁子项集

{

found=1;

teststr=tempit->first; testint=tempit->second; break; } } }

tempstr.erase();

if(found) //包含非频繁子项集 {

break; } }

if(!found) //只有不包含有非频繁子项集才加入候选项集中,否则剪枝掉

candidatevec.push_back(item); found=0; //重置 }

} }

frequentvec.clear(); //清除上一轮的频繁项集 round++;

cout<<\候选\项集:\

for(vector >::size_type ix=0;ix!=candidatevec.size();++ix) //输出候选项集

{

cout<<\

for(vector::size_type iy=0;iy!=candidatevec[ix].size();++iy) {

cout<

cout<<\ }

if(candidatevec.empty()) //候选项集为空 {

cout<<\候选\项集为空!\ }

int flag; //标记某个项集在某条事务中是否出现,出现为1,不出现为0 int count; //统计某个想集在整个交易的事务集中出现的次数 string tempstr; //临时string,用于串接各个项成一个字符串 int mark; //为避免执行多余的字符串串接工作

for(vector >::size_type sx=0;sx!=candidatevec.size();++sx) //构造下一轮的频繁项集

{

mark=1; count=0;

for(vector >::size_type sy=0;sy!=bitmap.size();++sy) {

flag=1; //初始化为1,表出现

for(vector::size_type sz=0;sz!=candidatevec[sx].size();++sz) {

if(bitmap[sy][candidatevec[sx].at(sz)]==0) //存在某一个子项不存在,则没出现项集

{

flag=0; }

if(mark==1) //只串接一次 {

tempstr+=candidatevec[sx].at(sz); //串接字符串 } }

if(flag) //flag仍然为,表示该项集在该条事务中出现了,计数加 {

count++; }

mark++; }

if(count>minsup) //支持度大于2 {

frequentvec.push_back(candidatevec[sx]); //插入频繁项集 }

items_count[tempstr]=count; //对应该项集的计数值

sort(candidatevec[sx].begin(),candidatevec[sx].end()); //排序 string tempstr2;

while(next_permutation(candidatevec[sx].begin(),candidatevec[sx].end())) //取下一排列组合

{

for(vector::size_type

tempst=0;tempst!=candidatevec[sx].size();tempst++) //拼接出该字符串组合

{

tempstr2+=candidatevec[sx][tempst]; }

items_count[tempstr2]=count; //对应该项集的计数值 tempstr2.erase(); }

tempstr.erase(); }

cout<<\ getchar();

if(!frequentvec.empty()) //频繁项集不为空 {

cout<<\频繁\项集(minsup=2):\

for(int sx=0;sx!=frequentvec.size();++sx) //输出频繁项集 {

cout.setf(ios::fixed); cout<<\

for(vector::size_type sz=0;sz!=frequentvec[sx].size();++sz) {

cout<

tempstr+=frequentvec[sx].at(sz); //串接字符串 }

cout<<\ cout<