数据挖掘实验三应用 Apriori 算法挖掘频繁项集_第1页
数据挖掘实验三应用 Apriori 算法挖掘频繁项集_第2页
数据挖掘实验三应用 Apriori 算法挖掘频繁项集_第3页
数据挖掘实验三应用 Apriori 算法挖掘频繁项集_第4页
数据挖掘实验三应用 Apriori 算法挖掘频繁项集_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验三、应用 Apriori 算法挖掘频繁项集学院 计算机科学与软件学院 实验目的:(1)熟悉 VC+编程工具和 Apriori 频繁项集挖掘算法。(2)根据管理层的需求,确定数据挖掘的任务,明确数据挖掘的功能,也就是明确要挖掘什么。(3)由确定的数据挖掘任务,从实验一处理后的结果中,采用切块或切片等联机分析处理技术,选择出挖掘任务相关数据。(4)用 VC+编程工具编写 Apriori 算法的程序,对任务相关数据运行 Apriori算法,挖掘出所有的频繁项集。1. 写出实验报告。 实验原理:1 、Apriori 算法 Apriori 使用一种称作逐层搜索的迭代方法,k 项集用于探索(k+1)项

2、集。首先,通过扫描数据库,累计每个项的计数,并收集满足最小支持度的项,找出频繁 1 项集的集合。该集合记作 L 1 。然后,L 1 用于找频繁 2 项集的集合L 2 ,L 2 用于找 L 3 ,如此下去,直到不能再找到频繁 k 项集。找每个 L k 需要一次数据库全扫描。2、提高频繁项集逐层产生的效率 Apriori 性质:频繁项集的所有非空子集也必须是频繁的。 三、 实验内容:1、实验内容 在给定的数据中提取统一购物篮购买的商品信息,由这些数据构成事务数据库 D,挖掘其中的频繁项集 L。挖掘频繁项集的算法描述如下:Apriori 算法:使用逐层迭代找出频繁项集输入:事务数据库 D;最小支持度

3、阈值。输出:D 中的频繁项集 L。(1) L 1 = find_frequent_1-itemsets(D); / 挖掘频繁 1-项集,比较容易(2) for (k=2;L k-1 ;k+) (3) C k = apriori_gen(L k-1 ,min_sup); / 调用 apriori_gen 方法生成候选频繁k-项集分为两步:合并、减枝(4) for each transaction t D / 扫描事务数据库 D(5) Ct = subset(C k ,t);(6) for each candidate c Ct(7) c.count+; / 统计候选频繁 k-项集的计数(8) (

4、9) L k =c Ck|c.countmin_sup / 满足最小支持度的 k-项集即为频繁 k-项集(10) (11) return L= k L k ; / 合并频繁 k-项集(k>0)算法在根据频繁 k-1 项集生成频繁 K 项集过程中要计算频繁 K 项集中每个元素的支持度,并计算 K 项集中每个 k-1 项子集是否在 F k-1 中,上述两条任何一条不满足,则删去这个 K 项集中的元素。2 、实验过程 1、打开试验用数据,读取出同一流水号的商品 ID 并取前 5 位,生成以行为单位生成事务数据集 transitions; 2、ind_frequent_1-itemsets 生成

5、频繁一项集for(each transaction in transitions)for(each item in transaction)oneItemSet;oneItemSet.count+;/对 1 项集进行计数 3、apriori-gen (L k-1 ) 候选集产生算法For all itemset pLk-1 doFor all itemset qLk-1 doIf p.item1=q.item1, p.item2=q.item2, ,p.itemk-2=q.itemk-2,p.item k-1 !=q.item k-1thenbegin c=pq/p、q 合并后任意的 L k-

6、1 子集if has_infrequent_subset(c, L k-1 )then delete c /存在 c 不属于 L k-1 剪枝else add c to CkEndReturn Ck 4、has_infrequent_subset(c, L k-1 )判断候选集的元素For all (k-1)-subsets of c doIf Not(SLk-1) THEN return TRUE;Return FALSE;1. 流程图4、主要程序代码1、/产生事务数据库代码(加注释)#include<iostream>#include<string>#include

7、<fstream>#include<algorithm>using namespace std;class Sales_npublic:string serial;int market;char date10;int sn; int id;float num;float price;int main() /打开并创建txt文件/char name150,name250; ifstream infile;cout<<"选择要打开的文件:1019n.txt 1020n.txt 1021n.txt"<<endl; cin>&g

8、t;name1; infile.open(name1,ios:in); /*string contents;*/if(infile.fail()cout << "error open!" << endl;cout<<"要保存的文件名:"<<endl; cin>>name2; ofstream outfile(name2,ios:out); if(!outfile) cout<<"open eror!"<<endl; exit(1); /访问预处理文件/

9、Sales_n sal10000;int sal_size=0;int ser_size=0;int m = 0,n = 1; int new1340020=0; /暂时储存商品IDwhile(!infile.eof()infile >> salsal_size.serial >> salsal_size.market >> salsal_size.date>> salsal_size.sn>> salsal_size.id>> salsal_size.num>> salsal_size.price;sal_s

10、ize+;/取统一流水的商品ID前三位按升序无重复的保存起来/new100=sal0.id/10000;for (int i =1;i<sal_size;i+) if (sali.serial=sali-1.serial) new1mn=sali.id/10000; /流水号相同n+;/outfile<<sali.id/100<<'t'else /排序/for(int k = 0;k<n;k+)for(int j = 0;j < n-k-1;j+) if(new1mj > new1mj+1) int t = new1mj; new

11、1mj = new1mj+1; new1mj+1 = t; for(int l= 0;l< n;l+)if(new1ml-1!=new1ml) outfile<<new1ml<<'t'outfile<<endl;m+;n = 0;new1mn=sali.id/10000;n+; infile.close();/关闭文件 outfile.close();/关闭文件system( "PAUSE ");2、/Apriori算法挖掘频繁项集support = 2(加注释)#include <iostream>#i

12、nclude <fstream>#include <string>#include <vector>#include <map>#include <cmath>#include <bitset>#include <algorithm>#include <iomanip>using namespace std;const int minsup=2; /设置最小支持度map<string,int> items_count; /统计各个项集的数目vector<string> mer

13、geItem(vector<string> vect1,vector<string> vect2,int round); /合并生成新的候选项集int isExist(vector<string> item,vector<vector<string> >items); /判断项集item是否已经存在候选项集集合items中,存在则返回vector<string> mergeItem(vector<string> vect1,vector<string> vect2,int round) /判断两个项

14、集是否可以合并(要求只有一项不同)成一个新的项集(做为候选集) /剪枝工作/ int count=0; /统计两个vector中相同的项的数目 vector<string> vect; map<string,int> tempMap; /辅助判断两个vector中重复的项 for(vector<string>:size_type st=0;st<vect1.size();st+) tempMapvect1st+; vect.push_back(vect1st); for(int st=0;st<vect2.size();st+) tempMapv

15、ect2st+; if(tempMapvect2st=2) /表示这两项相同 count+; else vect.push_back(vect2st); if(count+1)!=round) /要求两个项目集只有一个项目不相同,其他都相同 vect.clear(); return vect;int isExist(vector<string> item,vector<vector<string> >items) /判断项集item是否已经存在候选项集集合items中,存在则返回 int count; /统计相同的项的数目 if(!items.empty()

16、 for(vector<vector<string> >:size_type ix=0;ix!=items.size();ix+) count=0; for(vector<string>:size_type iy=0;iy!=itemsix.size();iy+) for(vector<string>:size_type iz=0;iz!=item.size();iz+) if(itemiz=itemsix.at(iy) count+; if(count=item.size() /表示存在 return 1; return 0;int main(

17、) vector<vector<string> > datavec; /原始数据项集 vector<vector<string> > candidatevec; /候选项集 vector<vector<string> > frequentvec; /频繁项集 vector<map<string,int> > bitmap; /判断某个项目在某一个事务中是否存在,存在则值为1,反之为0 long trancount=0; /原始事务总数 char name150; ifstream file; cou

18、t<<"选择要打开的文件:new1.txt new2.txt new3.txt"<<endl; cin>>name1; file.open(name1,ios:in); /打开数据文件 if(!file) /检查文件是否打开成功 cout<<"Fail to open data file!"<<endl; return 1; else string temp; vector<string> item; /项集的临时vector int begin,end; while(getline

19、(file,temp) /一行一行读入数据 trancount+; begin=0; temp.erase(0,temp.find_first_not_of("rtn "); /去除字符串首部的空格 temp.erase(temp.find_last_not_of("rtn")+1); while(end=temp.find('t',begin)!=string:npos) /每一个事务中的项是以't'为分隔符的 item.push_back(temp.substr(begin,end-begin); /将每一个项插入it

20、em中 begin=end+1; item.push_back(temp.substr(begin); /一个事务中的最后一项 datavec.push_back(item); /将一个事务中的所有项当成一个整体插入另一个大的vector中 item.clear(); /清空item cout<<"Press Enter to continue the processing" /pause getchar(); map<string,int> item_map; for(vector<vector<string> >:size

21、_type ix=0;ix!=datavec.size();+ix) for(vector<string>:size_type iy=0;iy!=datavecix.size();+iy) items_countdatavecix.at(iy)+; /该项集的计数加 item_mapdatavecix.at(iy)=1; /表示该项目在该事务中存在,值为1,否则默认为0 bitmap.push_back(item_map); item_map.clear(); /这里一定要清空一下 map<string,int>:const_iterator map_it=items_

22、count.begin(); cout<<"候选项集1:"<<endl; while(map_it!=items_count.end() /输出候选1项集 cout<<""<<map_it->first<<""<<endl; map_it+; cout<<"Press Enter to continue the processing" /pause getchar(); map_it=items_count.begin();

23、cout<<"频繁1项集(minsup=2):"<<endl; while(map_it!=items_count.end() /频繁1项集 if(map_it->second>minsup) /支持度大于2 cout.setf(ios:fixed); cout<<""<<map_it->first<<""<<" 支持度:"<<setprecision(6)<<map_it->second<

24、<endl; item.push_back(map_it->first); frequentvec.push_back(item); /插入候选项集的vector中 item.clear(); map_it+; if(!frequentvec.empty() /判断频繁项集是否为空,为空则退出 cout<<"Press Enter to continue the processing" /pause getchar(); int round=1; /生成候选项集轮次 int found; /是否包含有非频繁的子集,为表示含有,有的话进行剪枝 stri

25、ng tempstr; vector<string> tempvec; do /生成下一轮的候选项集 vector<vector<string> >:size_type st=frequentvec.size(); candidatevec.clear(); /清除上一轮的候选项集 for(vector<vector<string> >:size_type st1=0;st1<st;st1+) for(vector<vector<string> >:size_type st2=st1+1;st2<s

26、t;st2+) found=0; item=mergeItem(frequentvecst1,frequentvecst2,round); /调用函数合并生成下一轮的候选项集 if(!item.empty()&&!isExist(item,candidatevec) /若经过判断处理后返回的vector不为空且还不存在该项集,则作为候选项集加入候选vector中 /实现剪枝/ string teststr; int testint; tempvec=item; sort(tempvec.begin(),tempvec.end(); while(next_permutation(

27、tempvec.begin(),tempvec.end() /遍历所有的组合 for(vector<string>:size_type tempst=0;tempst!=tempvec.size();tempst+) /拼接出该字符串组合 tempstr+=tempvectempst; for(map<string,int>:const_iterator tempit=items_count.begin();tempit!=items_count.end();tempit+) if(tempit->second<minsup) /非频繁项集 if(temps

28、tr.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<<

29、;"候选"<<round<<"项集:"<<endl; for(vector<vector<string> >:size_type ix=0;ix!=candidatevec.size();+ix) /输出候选项集 cout<<"" for(vector<string>:size_type iy=0;iy!=candidatevecix.size();+iy) cout<<candidatevecix.at(iy)<<'

30、' cout<<""<<endl; if(candidatevec.empty() /候选项集为空 cout<<"候选"<<round<<"项集为空!"<<endl; int flag; /标记某个项集在某条事务中是否出现,出现为1,不出现为0 int count; /统计某个想集在整个交易的事务集中出现的次数 string tempstr; /临时string,用于串接各个项成一个字符串 int mark; /为避免执行多余的字符串串接工作 for(ve

31、ctor<vector<string> >:size_type sx=0;sx!=candidatevec.size();+sx) /构造下一轮的频繁项集 mark=1; count=0; for(vector<map<string,int> >:size_type sy=0;sy!=bitmap.size();+sy) flag=1; /初始化为1,表出现 for(vector<string>:size_type sz=0;sz!=candidatevecsx.size();+sz) if(bitmapsycandidatevecs

32、x.at(sz)=0) /存在某一个子项不存在,则没出现项集 flag=0; if(mark=1) /只串接一次 tempstr+=candidatevecsx.at(sz); /串接字符串 if(flag) /flag仍然为,表示该项集在该条事务中出现了,计数加 count+; mark+; if(count>minsup) /支持度大于2 frequentvec.push_back(candidatevecsx); /插入频繁项集 items_counttempstr=count; /对应该项集的计数值 sort(candidatevecsx.begin(),candidatevecsx.end(); /排序 string tempstr2; while(next_permutation(candidate

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论