数据挖掘Apriori算法论文_第1页
数据挖掘Apriori算法论文_第2页
数据挖掘Apriori算法论文_第3页
数据挖掘Apriori算法论文_第4页
数据挖掘Apriori算法论文_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

1、 数据挖掘课程作业题目 基于关联规则apriori算法的事务数据挖掘 班级 学号 姓名 日期 2010.06.13 目 录一、引言2二、正文21.背景22.算法思想23.数据集34.源代码35.算法流程166运行结果16三、结论17四、参考文献18一、 引言随着网络、数据库技术的迅速发展以及数据库管理系统的广泛应用,人们积累的数据越来越多。由此,数据挖掘技术应运而生。数据挖掘(data mining)就是从大量的、不完全的、有噪声的、模糊的、随机的实际应用数据中,提取隐含在其中的、人们事先不知道的、但又是潜在有用的信息和知识的过程。它是一种新的商业信息处理技术,其主要特点是对商业数据库中的大量

2、业务数据进行抽取、转换、分析和其他模型化处理,从中提取辅助商业决策的关键性数据。简而言之,数据挖掘其实是一类深层次的数据分析方法。从这个角度数据挖掘也可以描述为:按企业制定的业务目标,对大量的企业数据进行探索和分析,揭示隐藏的、未知的或验证已知的规律性,并进一步将其模型化的先进有效的方法。其中关联规则的算法的研究在数据挖掘算法的研究中占有相当重要的地位,关联规则算法的核心是apriori算法,但随着对关联规则研究的深入,它的缺点也暴露出来了。本文将对数据挖掘的一种经典算法apriori算法对于事务项目数据的挖掘应用。二、正文1、背景自1994年由agrawal等人提出的关联规则挖掘aprior

3、i的算法从其产生到现在,对关联规则挖掘方面的研究有着很大的影响。apriori 算法是一种最有影响的挖掘布尔关联规则频繁项集的算法。算法基于这样的事实:算法使用频繁项集性质的先验知识。apriori 使用一种称作逐层搜索的迭代方法,k-项集用于探索(k+1)-项集。首先,找出频繁1-项集的集合。该集合记作l1。l1 用于找频繁2-项集的集合l2,而l2 用于找l3,如此下去,直到不能找到频繁k-项集。找每个lk 需要一次数据库扫描。为提高频繁项集逐层产生的效率,一种称作apriori 性质的重要性质用于压缩搜索空间。为了提高频繁项目集逐层产生的效率,apriori算法利用了两个重要的

4、性质用于压缩搜索空间:    (l)若x是频繁项集,则x的所有子集都是频繁项集。    (2)若x是非频繁项集,则x的所有超集都是非频繁项集。2、算法思想    算法: apriori算法,使用逐层迭代找出频繁项集。    输入:事务数据库d;最小支持度阈值min_sup。    输出:d 中的频繁项集l。    1) l1 = find_frequent_1_itemsets(d);  

5、0; 2) for (k = 2; lk-1  ; k+)     3) ck = aproiri_gen(lk-1,min_sup);    4) for each transaction t d /scan d for count    5) ct = subset(ck,t); /get subsets of t that are candidates    6) for each candidate c ct  

6、60; 7) c.count+;    8)     9) lk=c ck | c.count min_sup    10)     11) return l = klk;3、数据集 我用的的是一个事务数据集,数据集存在一个transaction数据库中,数据库中有三个表:customer、trandb、transactiondb,存有事务和项目的数据transactiondb:trandb:customer:4、源代码/ aprioriview.cpp : imp

7、lementation of the caprioriview class/#include "stdafx.h"#include "apriori.h"#include "time.h"#include "aprioriset.h"#include "aprioridoc.h"#include "aprioriview.h"#include "setpara.h"#ifdef _debug#define new debug_new#undef this_

8、filestatic char this_file = _file_;#endif/ caprioriviewimplement_dyncreate(caprioriview, crecordview)begin_message_map(caprioriview, crecordview)/afx_msg_map(caprioriview)on_bn_clicked(idc_bn_freqitem, onbnfreqitem)on_command(id_parameter, onparameter)/afx_msg_map/ standard printing commandson_comma

9、nd(id_file_print, crecordview:onfileprint)on_command(id_file_print_direct, crecordview:onfileprint)on_command(id_file_print_preview, crecordview:onfileprintpreview)end_message_map()/ caprioriview construction/destructioncaprioriview:caprioriview(): crecordview(caprioriview:idd)/afx_data_init(caprior

10、iview)m_pset = null;nallfreqitem=0;/afx_data_init/ todo: add construction code herecaprioriview:caprioriview()void caprioriview:dodataexchange(cdataexchange* pdx)crecordview:dodataexchange(pdx);/afx_data_map(caprioriview)ddx_control(pdx, idc_list_freqitem, m_list_freqitem);/afx_data_mapbool capriori

11、view:precreatewindow(createstruct& cs)/ todo: modify the window class or styles here by modifying/ the createstruct csreturn crecordview:precreatewindow(cs);void caprioriview:oninitialupdate()m_pset = &getdocument()->m_aprioriset;crecordview:oninitialupdate();getparentframe()->recalcla

12、yout();resizeparenttofit();/ caprioriview printingbool caprioriview:onprepareprinting(cprintinfo* pinfo)/ default preparationreturn doprepareprinting(pinfo);void caprioriview:onbeginprinting(cdc* /*pdc*/, cprintinfo* /*pinfo*/)/ todo: add extra initialization before printingvoid caprioriview:onendpr

13、inting(cdc* /*pdc*/, cprintinfo* /*pinfo*/)/ todo: add cleanup after printing/ caprioriview diagnostics#ifdef _debugvoid caprioriview:assertvalid() constcrecordview:assertvalid();void caprioriview:dump(cdumpcontext& dc) constcrecordview:dump(dc);caprioridoc* caprioriview:getdocument() / non-debu

14、g version is inlineassert(m_pdocument->iskindof(runtime_class(caprioridoc);return (caprioridoc*)m_pdocument;#endif /_debug/ caprioriview database supportcrecordset* caprioriview:ongetrecordset()return m_pset;/ caprioriview message handlersvoid caprioriview:onbnfreqitem() / todo: add your control

15、notification handler code hereint nfieldcount=m_pset->getodbcfieldcount();int ndbcount;cstring strvalue;cstring strinttostring=""clock_t start,stop,tick;double timeused;int nlargecount=0;int nlistfreqitemcount=0; start=clock();clearitem(); m_list_freqitem.insertcolumn(0,"item"

16、,lvcfmt_left,100);m_list_freqitem.insertcolumn(1,"count",lvcfmt_left,100); if (nitemcount<=0)messagebox("请先进行参数设置",null,mb_ok); return;findlargeitem(); for(int k=1;largeitemcountk-1!=0;k+) apriorigen(k,1);/初始化数组for(int mm=0;mm<candlargeitemcountk;mm+)ncountcandmm=0; m_pset-

17、>movefirst();ndbcount=0;while(!m_pset->iseof() transgencand(k,ndbcount); /统计计数for(int jj=0;jj<ntranscandcount;jj+) for(int jj1=0;jj1<candlargeitemcountk;jj1+) if(transgencandfreqjj.find(candlargeitemkjj1)!=-1) ncountcandjj1+; break; ndbcount+; m_pset->movenext(); showfreqitem(k);stop=

18、clock();tick=stop - start; timeused=(double)tick/clk_tck;strinttostring=""strinttostring.format("%s%f",strinttostring,timeused);messagebox(strinttostring,null,mb_ok); void caprioriview:clearitem() /清除列表显示的内容 m_list_freqitem.deleteallitems ();while(m_list_freqitem.deletecolumn (0)

19、;updatewindow();for(int kk=0;kk<nmaxsize;kk+) largeitemcountkk=0;for(int kk1=0;kk1<nmaxsize;kk1+)for(int kk2=0;kk2<nmaxsize;kk2+)largeitemkk1kk2=""void caprioriview:apriorigen(int ncandfreqitem, int nminsupp)/由l(k-1)生成c(k)cstring strtemp1,strtemp2,strrighttemp1,strrighttemp2;cstri

20、ng strtemp,strlefttemp1,strlefttemp2;int nstrtemp1,nstrtemp2; int ncandfreqitemcount=0;strtemp1=""strtemp2=""strrighttemp1=""strrighttemp2=""strtemp=""strlefttemp1="" strlefttemp2=""nstrtemp1=0;nstrtemp2=0;nallfreqitem=nallfreqite

21、m + largeitemcountncandfreqitem-1;for(int i1=0;i1<largeitemcountncandfreqitem-1;i1+)/strtemp1=strcandfreqitemi1; strtemp1=largeitemncandfreqitem -1i1; nstrtemp1=strtemp1.reversefind(',');strrighttemp1=strtemp1.right(strtemp1.getlength ()-nstrtemp1-1); strlefttemp1=strtemp1.left(nstrtemp1)

22、;for(int j1=i1+1;j1<largeitemcountncandfreqitem-1;)/strtemp2=strcandfreqitemj1;strtemp2=largeitemncandfreqitem-1j1;nstrtemp2=strtemp2.reversefind (',');strrighttemp2=strtemp2.right(strtemp2.getlength ()-nstrtemp2-1);strlefttemp2=strtemp2.left(nstrtemp2) ;if(strlefttemp1=strlefttemp2)&

23、&(strrighttemp1<strrighttemp2)strtemp=strtemp1+','+strrighttemp2;if(prune(ncandfreqitem,strtemp)candlargeitemncandfreqitemncandfreqitemcount+=strtemp; j1+;elsej1+;else break; candlargeitemcountncandfreqitem=ncandfreqitemcount; void caprioriview:subitemgen(int strsubitemcount,cstring s

24、trsubitem)/对每个事务分解成单个项目cstring strtemp1;cstring strtempsubitem10;cstring strreverse;int nsubitemcount;int nstrrighttemp1;int ntempcount;strtemp1=strsubitem;nsubitemcount=0;while(nstrrighttemp1=strtemp1.reversefind(',')!=-1)ntempcount=strtemp1.getlength() - nstrrighttemp1 - 1; strtempsubitemn

25、subitemcount+=strtemp1.right( ntempcount);strtemp1=strtemp1.left(nstrrighttemp1); strtempsubitemnsubitemcount+=strtemp1;for(int i2=0;i2<nsubitemcount/2;i2+)strreverse=strtempsubitemnsubitemcount-i2-1;strtempsubitemnsubitemcount- i2-1=strtempsubitemi2;strtempsubitemi2=strreverse; for(int i1=0;i1&l

26、t;nsubitemcount;i1+) dbitemstrsubitemcounti1=strtempsubitemi1; dbitemcountstrsubitemcount=nsubitemcount; void caprioriview:findlargeitem()/显示1-频繁项目集int nfieldcount=m_pset->getodbcfieldcount();int ncount=0;int nfreqitem100; int ndbcount=0;cstring strinit;cstring strvalue;cstring strinttostring; /

27、初始化候选项目集 for(int ninitcount=0;ninitcount<nitemcount;ninitcount+) strinit=""strinit.format("%s%d",strinit,ninitcount+1); candlargeitem0ninitcount='i'+strinit;/初始化数组for(int ii=0;ii<nitemcount;ii+)nfreqitemii=0;m_pset->movefirst ();while(!m_pset->iseof()for(int j

28、=1;j<nfieldcount;j+)m_pset->getfieldvalue(j,strvalue); subitemgen(ndbcount+ ,strvalue);for(int i=0;i<nitemcount;i+)if(strvalue.find(candlargeitem0i)!=-1)nfreqitemi+;m_pset->movenext();ndbitemcount=ndbcount; for(int i1=0;i1<nitemcount;i1+) strinttostring="" if(double(nfreqite

29、mi1)/double(ndbitemcount)>=ditemsupp) largeitem0ncount=candlargeitem0i1; m_list_freqitem.insertitem(ncount,strinttostring);m_list_freqitem.setitemtext(ncount,0,largeitem0ncount); strinttostring=""strinttostring.format("%s%d",strinttostring,nfreqitemi1);m_list_freqitem.setitemt

30、ext(ncount,1,strinttostring);ncount+;largeitemcount0=ncount;void caprioriview:onparameter() / todo: add your command handler code herecsetpara dlg;dlg.m_itemcount =10;dlg.m_item_supp=0.2; int ren=dlg.domodal(); nitemcount=dlg.m_itemcount;ditemsupp=dlg.m_item_supp ;bool caprioriview:prune(int ncandfr

31、eqitemcount,cstring strcandfreqitem)cstring strtemp1;cstring strtempsubitemnmaxsize;cstring strreverse;cstring strsubcandfreqitemnmaxsize;/分解候选项目cstring strsubtemp=""cstring strsubtemp1;int nsubfreqitemcount=0;/统计分解候选项目的个数int nsubitemcount;int nstrrighttemp1;int ntempcount;int nprunecount=

32、0;strtemp1=strcandfreqitem;nsubitemcount=0;while(nstrrighttemp1=strtemp1.reversefind(',')!=-1)ntempcount=strtemp1.getlength() - nstrrighttemp1 - 1; strtempsubitemnsubitemcount+=strtemp1.right( ntempcount);strtemp1=strtemp1.left(nstrrighttemp1); strtempsubitemnsubitemcount+=strtemp1;for(int i

33、2=0;i2<nsubitemcount/2;i2+)strreverse=strtempsubitemnsubitemcount-i2-1;strtempsubitemnsubitemcount- i2-1=strtempsubitemi2;strtempsubitemi2=strreverse;for(int i3=nsubitemcount-1;i3>=0;i3-) strsubtemp1=strtempsubitemi3;strsubtemp.empty(); for(int i4=0;i4<nsubitemcount;i4+)if(strsubtemp1!=strt

34、empsubitemi4) strsubtemp=strsubtemp+strtempsubitemi4+','strsubcandfreqitemnsubfreqitemcount+ = strsubtemp.left(strsubtemp.getlength()-1); for(int i5=0;i5<nsubfreqitemcount;i5+) for(int i6=0;i6<largeitemcountncandfreqitemcount-1;i6+) if(strsubcandfreqitemi5.find(largeitemncandfreqitemco

35、unt-1i6)>=0) nprunecount+;if(nprunecount=nsubitemcount)return true;return false;void caprioriview:transgencand(int ncandfreqitem,int ncurrentcount )cstring strtranstemp;cstring strtranstemp1; ntranscandcount=0;int ncurrenttempcount=ncurrentcount;int anmaxsize;int ncount=0;ancount=0;/初始化数组for(int

36、ntranscand=0;ntranscand<nmaxsize;ntranscand+)transgencandfreqntranscand=""doif(ancount-ncount) <= (dbitemcountncurrenttempcount- ncandfreqitem -1)if(ncount=ncandfreqitem) strtranstemp=""for(int jj=0;jj<ncandfreqitem;jj+)strtranstemp=strtranstemp+dbitemncurrenttempcountaj

37、j+','strtranstemp1=""strtranstemp1=strtranstemp+dbitemncurrenttempcountancandfreqitem; transgencandfreqntranscandcount+=strtranstemp1; / messagebox(strtranstemp1); ancount+; continue;ncount+;ancount=ancount-1+1;elseif(ncount=0) return;a-ncount+;while(1);/for(int ll=0;ll<ntransca

38、ndcount;ll+)/messagebox(transgencandfreqll,null,mb_ok);void caprioriview:showfreqitem(int nscancount)cstring strinttostring="" cstring strvalue;cstring strjj32;int nlargecount=-1;int nlargeitemcount=0; /以下为求频繁项目集int k,nlistfreqitemcount;k=nscancount; nlistfreqitemcount=largeitemcountk-1;m_

39、list_freqitem.insertitem(0,strvalue);m_list_freqitem.setitemtext(0,0,"-"); m_list_freqitem.setitemtext(0,1,"-"); for(int jj2=0;jj2<candlargeitemcountk;jj2+)if(double(ncountcandjj2)/double(ndbitemcount)>=ditemsupp)largeitemknlargeitemcount+=candlargeitemkjj2;nlargecount+;str

40、jj31=strinttostring;strjj30=candlargeitemkjj2;strinttostring=""strinttostring.format("%s%d",strinttostring,ncountcandjj2); strjj31=strinttostring;m_list_freqitem.insertitem(nlargecount,strvalue);m_list_freqitem.setitemtext(nlargecount,0,largeitemknlargeitemcount-1);m_list_freqite

41、m.setitemtext(nlargecount,1,strinttostring);updatewindow(); /复制频繁项目个数 largeitemcountk=nlargeitemcount;5、算法流程第一步, 经过算法的第一次迭代,对事务数据库进行一次扫描,计算出d中所包含的每个项目出现的次数,生成候选1-项集的集合c1。    第二步,根据设定的最小支持度,从c1中确定频繁1-项集l1。    第三步,由l1产生候选2-项集c2,然后扫描事务数据库对c2中的项集进行计数。    第四步,根据最小支持度,从候选集c2中确定频繁集l2。    第五步,由频繁2-项集l2生成候选3-项集c3,生成的候选3-项集的集合c3=1,2,3,1,3,5,2,3,5,根据apriori的性质剪枝

温馨提示

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

评论

0/150

提交评论