PrefixSpan序列模式挖掘算法

更多数据挖掘代码:https://github.com/linyiqun/DataMiningAlgorithm

介绍

与GSP一样,PrefixSpan算法也是序列模式分析算法的一种,不过与前者不同的是PrefixSpan算法不产生任何的侯选集,在这点上可以说已经比GSP好很多了。PrefixSpan算法可以挖掘出满足阈值的所有序列模式,可以说是非常经典的算法。序列的格式就是上文中提到过的类似于<a, b, (de)>这种的。

算法原理

PrefixSpan算法的原理是采用后缀序列转前缀序列的方式来构造频繁序列的。举个例子,


比如原始序列如上图所示,4条序列,1个序列中好几个项集,项集内有1个或多个元素,首先找出前缀为a的子序列,此时序列前缀为<a>,后缀就变为了:

<wbr style="font-family:simsun; line-height:21px; background-color:rgb(245,242,239)"><span style="font-family:simsun; line-height:21px; background-color:rgb(245,242,239)"><img src="https://img-blog.csdn.net/20150213090738210?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW5kcm9pZGx1c2hhbmdkZXJlbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="border:none; max-width:100%"></span><br></wbr>

"_"下标符代表前缀为a,说明是在项集中间匹配的。这就相当于从后缀序列中提取出1项加入到前缀序列中,变化的规则就是从左往右扫描,找到第1个此元素对应的项,然后做改变。然后根据此规则继续递归直到后续序列不满足最小支持度阈值的情况。所以此算法的难点就转变为了从后缀序列变为前缀序列的过程。在这个过程要分为2种情况,第1种是单个元素项的后缀提前,比如这里的a,对单个项的提前有分为几种情况,比如:

<b a c ad>,就会变为<c ad>,如果a是嵌套在项集中的情况<b c dad r>,就会变为< _d r>,_代表的就是a.如果a在一项的最末尾,此项也会被移除<b c dda r>变为<r>。但是如果是这种情况<_da d d>a包含在下标符中,将会做处理,应该此时的a是在前缀序列所属的项集内的。

还有1个大类的分类就是对于组合项的后缀提取,可以分为2个情况,1个是从_X中寻找,一个从后面找出连续的项集,比如在这里<a>的条件下,找出前缀<(ab)>的后缀序列


第一种在_X中寻找还有没有X=a的情况,因为_已经代表1个a了,还有一个是判断_X != _a的情况,从后面的项集中找到包含有连续的aa的那个项集,然后做变换处理,与单个项集的变换规则一致。

算法的递归顺序

想要实现整个的序列挖掘,算法的递归顺序就显得非常重要了。在探索递归顺序的路上还是犯了一些错误的,刚刚开始的递归顺序是<a>---><a a>----><a a a>,假设<a a a>找不到对应的后缀模式时,然后回溯到<a (aa)>进行递归,后来发现这样会漏掉情况,为什么呢,因为如果 <a a >没法进行到<a a a>,那么就不可能会有前缀<a (aa)>,顶多会判断到<(aa)>,从<a a>处回调的。于是我发现了这个问题,就变为了下面这个样子,经测试是对的。:

加入所有的单个元素的类似为a-f,顺序为

<a>,---><a a>.同时<(aa)>,然后<ab>同时<(ab)>,就是在a添加a-f的元素的时候,检验a所属项集添加a-f元素的情况。这样就不会漏掉情况了,用了2个递归搞定了这个问题。这个算法的整体实现可以对照代码来看会理解很多。最后提醒一点,在每次做出改变之后都会判断一下是否满足最小支持度阈值的。

PrefixSpan实例

这里举1个真实一点的例子,下面是输入的初始序列:

挖掘出的所有的序列模式为,下面是一个表格的形式


在<b>的序列模式中少了1个序列模式。可以与后面程序算法测试的结果做对比。

算法的代码实现

代码实现同样以这个为例子,这样会显得更有说服性。

测试数据:

  1. bdcbac
  2. bfcebfg
  3. ahbfabf
  4. beced
  5. abdbcbade
Sequence.java:

  1. packageDataMining_PrefixSpan;
  2. importjava.util.ArrayList;
  3. /**
  4. *序列类
  5. *
  6. *@authorlyq
  7. *
  8. */
  9. publicclassSequence{
  10. //序列内的项集
  11. privateArrayList<ItemSet>itemSetList;
  12. publicSequence(){
  13. this.itemSetList=newArrayList<>();
  14. }
  15. publicArrayList<ItemSet>getItemSetList(){
  16. returnitemSetList;
  17. }
  18. publicvoidsetItemSetList(ArrayList<ItemSet>itemSetList){
  19. this.itemSetList=itemSetList;
  20. }
  21. /**
  22. *判断单一项是否包含于此序列
  23. *
  24. *@paramc
  25. *待判断项
  26. *@return
  27. */
  28. publicbooleanstrIsContained(Stringc){
  29. booleanisContained=false;
  30. for(ItemSetitemSet:itemSetList){
  31. isContained=false;
  32. for(Strings:itemSet.getItems()){
  33. if(itemSet.getItems().contains("_")){
  34. continue;
  35. }
  36. if(s.equals(c)){
  37. isContained=true;
  38. break;
  39. }
  40. }
  41. if(isContained){
  42. //如果已经检测出包含了,直接挑出循环
  43. break;
  44. }
  45. }
  46. returnisContained;
  47. }
  48. /**
  49. *判断组合项集是否包含于序列中
  50. *
  51. *@paramitemSet
  52. *组合的项集,元素超过1个
  53. *@return
  54. */
  55. publicbooleancompoentItemIsContain(ItemSetitemSet){
  56. booleanisContained=false;
  57. ArrayList<String>tempItems;
  58. StringlastItem=itemSet.getLastValue();
  59. for(inti=0;i<this.itemSetList.size();i++){
  60. tempItems=this.itemSetList.get(i).getItems();
  61. //分2种情况查找,第一种从_X中找出x等于项集最后的元素,因为_前缀已经为原本的元素
  62. if(tempItems.size()>1&&tempItems.get(0).equals("_")
  63. &&tempItems.get(1).equals(lastItem)){
  64. isContained=true;
  65. break;
  66. }elseif(!tempItems.get(0).equals("_")){
  67. //从没有_前缀的项集开始寻找,第二种为从后面的后缀中找出直接找出连续字符为ab为同一项集的项集
  68. if(strArrayContains(tempItems,itemSet.getItems())){
  69. isContained=true;
  70. break;
  71. }
  72. }
  73. if(isContained){
  74. break;
  75. }
  76. }
  77. returnisContained;
  78. }
  79. /**
  80. *删除单个项
  81. *
  82. *@params
  83. *待删除项
  84. */
  85. publicvoiddeleteSingleItem(Strings){
  86. ArrayList<String>tempItems;
  87. ArrayList<String>deleteItems=newArrayList<>();
  88. for(ItemSetitemSet:this.itemSetList){
  89. tempItems=itemSet.getItems();
  90. deleteItems=newArrayList<>();
  91. for(inti=0;i<tempItems.size();i++){
  92. if(tempItems.get(i).equals(s)){
  93. deleteItems.add(tempItems.get(i));
  94. }
  95. }
  96. tempItems.removeAll(deleteItems);
  97. }
  98. }
  99. /**
  100. *提取项s之后所得的序列
  101. *
  102. *@params
  103. *目标提取项s
  104. */
  105. publicSequenceextractItem(Strings){
  106. SequenceextractSeq=this.copySeqence();
  107. ItemSetitemSet;
  108. ArrayList<String>items;
  109. ArrayList<ItemSet>deleteItemSets=newArrayList<>();
  110. ArrayList<String>tempItems=newArrayList<>();
  111. for(intk=0;k<extractSeq.itemSetList.size();k++){
  112. itemSet=extractSeq.itemSetList.get(k);
  113. items=itemSet.getItems();
  114. if(items.size()==1&&items.get(0).equals(s)){
  115. //如果找到的是单项,则完全移除,跳出循环
  116. extractSeq.itemSetList.remove(k);
  117. break;
  118. }elseif(items.size()>1&&!items.get(0).equals("_")){
  119. //在后续的多元素项中判断是否包含此元素
  120. if(items.contains(s)){
  121. //如果包含把s后面的元素加入到临时字符数组中
  122. intindex=items.indexOf(s);
  123. for(intj=index;j<items.size();j++){
  124. tempItems.add(items.get(j));
  125. }
  126. //将第一位的s变成下标符"_"
  127. tempItems.set(0,"_");
  128. if(tempItems.size()==1){
  129. //如果此匹配为在最末端,同样移除
  130. deleteItemSets.add(itemSet);
  131. }else{
  132. //将变化后的项集替换原来的
  133. extractSeq.itemSetList.set(k,newItemSet(tempItems));
  134. }
  135. break;
  136. }else{
  137. deleteItemSets.add(itemSet);
  138. }
  139. }else{
  140. //不符合以上2项条件的统统移除
  141. deleteItemSets.add(itemSet);
  142. }
  143. }
  144. extractSeq.itemSetList.removeAll(deleteItemSets);
  145. returnextractSeq;
  146. }
  147. /**
  148. *提取组合项之后的序列
  149. *
  150. *@paramarray
  151. *组合数组
  152. *@return
  153. */
  154. publicSequenceextractCompoentItem(ArrayList<String>array){
  155. //找到目标项,是否立刻停止
  156. booleanstopExtract=false;
  157. Sequenceseq=this.copySeqence();
  158. StringlastItem=array.get(array.size()-1);
  159. ArrayList<String>tempItems;
  160. ArrayList<ItemSet>deleteItems=newArrayList<>();
  161. for(inti=0;i<seq.itemSetList.size();i++){
  162. if(stopExtract){
  163. break;
  164. }
  165. tempItems=seq.itemSetList.get(i).getItems();
  166. //分2种情况查找,第一种从_X中找出x等于项集最后的元素,因为_前缀已经为原本的元素
  167. if(tempItems.size()>1&&tempItems.get(0).equals("_")
  168. &&tempItems.get(1).equals(lastItem)){
  169. if(tempItems.size()==2){
  170. seq.itemSetList.remove(i);
  171. }else{
  172. //把1号位置变为下标符"_",往后移1个字符的位置
  173. tempItems.set(1,"_");
  174. //移除第一个的"_"下划符
  175. tempItems.remove(0);
  176. }
  177. stopExtract=true;
  178. break;
  179. }elseif(!tempItems.get(0).equals("_")){
  180. //从没有_前缀的项集开始寻找,第二种为从后面的后缀中找出直接找出连续字符为ab为同一项集的项集
  181. if(strArrayContains(tempItems,array)){
  182. //从左往右找出第一个给定字符的位置,把后面的部分截取出来
  183. intindex=tempItems.indexOf(lastItem);
  184. ArrayList<String>array2=newArrayList<String>();
  185. for(intj=index;j<tempItems.size();j++){
  186. array2.add(tempItems.get(j));
  187. }
  188. array2.set(0,"_");
  189. if(array2.size()==1){
  190. //如果此项在末尾的位置,则移除该项,否则进行替换
  191. deleteItems.add(seq.itemSetList.get(i));
  192. }else{
  193. seq.itemSetList.set(i,newItemSet(array2));
  194. }
  195. stopExtract=true;
  196. break;
  197. }else{
  198. deleteItems.add(seq.itemSetList.get(i));
  199. }
  200. }else{
  201. //这种情况是处理_X中X不等于最后一个元素的情况
  202. deleteItems.add(seq.itemSetList.get(i));
  203. }
  204. }
  205. seq.itemSetList.removeAll(deleteItems);
  206. returnseq;
  207. }
  208. /**
  209. *深拷贝一个序列
  210. *
  211. *@return
  212. */
  213. publicSequencecopySeqence(){
  214. SequencecopySeq=newSequence();
  215. ItemSettempItemSet;
  216. ArrayList<String>items;
  217. for(ItemSetitemSet:this.itemSetList){
  218. items=(ArrayList<String>)itemSet.getItems().clone();
  219. tempItemSet=newItemSet(items);
  220. copySeq.getItemSetList().add(tempItemSet);
  221. }
  222. returncopySeq;
  223. }
  224. /**
  225. *获取序列中最后一个项集的最后1个元素
  226. *
  227. *@return
  228. */
  229. publicStringgetLastItemSetValue(){
  230. intsize=this.getItemSetList().size();
  231. ItemSetitemSet=this.getItemSetList().get(size-1);
  232. size=itemSet.getItems().size();
  233. returnitemSet.getItems().get(size-1);
  234. }
  235. /**
  236. *判断strList2是否是strList1的子序列
  237. *
  238. *@paramstrList1
  239. *@paramstrList2
  240. *@return
  241. */
  242. publicbooleanstrArrayContains(ArrayList<String>strList1,
  243. ArrayList<String>strList2){
  244. booleanisContained=false;
  245. for(inti=0;i<strList1.size()-strList2.size()+1;i++){
  246. isContained=true;
  247. for(intj=0,k=i;j<strList2.size();j++,k++){
  248. if(!strList1.get(k).equals(strList2.get(j))){
  249. isContained=false;
  250. break;
  251. }
  252. }
  253. if(isContained){
  254. break;
  255. }
  256. }
  257. returnisContained;
  258. }
  259. }
ItemSet.java:

  1. packageDataMining_PrefixSpan;
  2. importjava.util.ArrayList;
  3. /**
  4. *字符项集类
  5. *
  6. *@authorlyq
  7. *
  8. */
  9. publicclassItemSet{
  10. //项集内的字符
  11. privateArrayList<String>items;
  12. publicItemSet(String[]str){
  13. items=newArrayList<>();
  14. for(Strings:str){
  15. items.add(s);
  16. }
  17. }
  18. publicItemSet(ArrayList<String>itemsList){
  19. this.items=itemsList;
  20. }
  21. publicItemSet(Strings){
  22. items=newArrayList<>();
  23. for(inti=0;i<s.length();i++){
  24. items.add(s.charAt(i)+"");
  25. }
  26. }
  27. publicArrayList<String>getItems(){
  28. returnitems;
  29. }
  30. publicvoidsetItems(ArrayList<String>items){
  31. this.items=items;
  32. }
  33. /**
  34. *获取项集最后1个元素
  35. *
  36. *@return
  37. */
  38. publicStringgetLastValue(){
  39. intsize=this.items.size();
  40. returnthis.items.get(size-1);
  41. }
  42. }
PrefixSpanTool.java:

  1. packageDataMining_PrefixSpan;
  2. importjava.io.BufferedReader;
  3. importjava.io.File;
  4. importjava.io.FileReader;
  5. importjava.io.IOException;
  6. importjava.util.ArrayList;
  7. importjava.util.Collections;
  8. importjava.util.HashMap;
  9. importjava.util.Map;
  10. /**
  11. *PrefixSpanTool序列模式分析算法工具类
  12. *
  13. *@authorlyq
  14. *
  15. */
  16. publicclassPrefixSpanTool{
  17. //测试数据文件地址
  18. privateStringfilePath;
  19. //最小支持度阈值比例
  20. privatedoubleminSupportRate;
  21. //最小支持度,通过序列总数乘以阈值比例计算
  22. privateintminSupport;
  23. //原始序列组
  24. privateArrayList<Sequence>totalSeqs;
  25. //挖掘出的所有序列频繁模式
  26. privateArrayList<Sequence>totalFrequentSeqs;
  27. //所有的单一项,用于递归枚举
  28. privateArrayList<String>singleItems;
  29. publicPrefixSpanTool(StringfilePath,doubleminSupportRate){
  30. this.filePath=filePath;
  31. this.minSupportRate=minSupportRate;
  32. readDataFile();
  33. }
  34. /**
  35. *从文件中读取数据
  36. */
  37. privatevoidreadDataFile(){
  38. Filefile=newFile(filePath);
  39. ArrayList<String[]>dataArray=newArrayList<String[]>();
  40. try{
  41. BufferedReaderin=newBufferedReader(newFileReader(file));
  42. Stringstr;
  43. String[]tempArray;
  44. while((str=in.readLine())!=null){
  45. tempArray=str.split("");
  46. dataArray.add(tempArray);
  47. }
  48. in.close();
  49. }catch(IOExceptione){
  50. e.getStackTrace();
  51. }
  52. minSupport=(int)(dataArray.size()*minSupportRate);
  53. totalSeqs=newArrayList<>();
  54. totalFrequentSeqs=newArrayList<>();
  55. SequencetempSeq;
  56. ItemSettempItemSet;
  57. for(String[]str:dataArray){
  58. tempSeq=newSequence();
  59. for(Strings:str){
  60. tempItemSet=newItemSet(s);
  61. tempSeq.getItemSetList().add(tempItemSet);
  62. }
  63. totalSeqs.add(tempSeq);
  64. }
  65. System.out.println("原始序列数据:");
  66. outputSeqence(totalSeqs);
  67. }
  68. /**
  69. *输出序列列表内容
  70. *
  71. *@paramseqList
  72. *待输出序列列表
  73. */
  74. privatevoidoutputSeqence(ArrayList<Sequence>seqList){
  75. for(Sequenceseq:seqList){
  76. System.out.print("<");
  77. for(ItemSetitemSet:seq.getItemSetList()){
  78. if(itemSet.getItems().size()>1){
  79. System.out.print("(");
  80. }
  81. for(Strings:itemSet.getItems()){
  82. System.out.print(s+"");
  83. }
  84. if(itemSet.getItems().size()>1){
  85. System.out.print(")");
  86. }
  87. }
  88. System.out.println(">");
  89. }
  90. }
  91. /**
  92. *移除初始序列中不满足最小支持度阈值的单项
  93. */
  94. privatevoidremoveInitSeqsItem(){
  95. intcount=0;
  96. HashMap<String,Integer>itemMap=newHashMap<>();
  97. singleItems=newArrayList<>();
  98. for(Sequenceseq:totalSeqs){
  99. for(ItemSetitemSet:seq.getItemSetList()){
  100. for(Strings:itemSet.getItems()){
  101. if(!itemMap.containsKey(s)){
  102. itemMap.put(s,1);
  103. }
  104. }
  105. }
  106. }
  107. Stringkey;
  108. for(Map.Entryentry:itemMap.entrySet()){
  109. count=0;
  110. key=(String)entry.getKey();
  111. for(Sequenceseq:totalSeqs){
  112. if(seq.strIsContained(key)){
  113. count++;
  114. }
  115. }
  116. itemMap.put(key,count);
  117. }
  118. for(Map.Entryentry:itemMap.entrySet()){
  119. key=(String)entry.getKey();
  120. count=(int)entry.getValue();
  121. if(count<minSupport){
  122. //如果支持度阈值小于所得的最小支持度阈值,则删除该项
  123. for(Sequenceseq:totalSeqs){
  124. seq.deleteSingleItem(key);
  125. }
  126. }else{
  127. singleItems.add(key);
  128. }
  129. }
  130. Collections.sort(singleItems);
  131. }
  132. /**
  133. *递归搜索满足条件的序列模式
  134. *
  135. *@parambeforeSeq
  136. *前缀序列
  137. *@paramafterSeqList
  138. *后缀序列列表
  139. */
  140. privatevoidrecursiveSearchSeqs(SequencebeforeSeq,
  141. ArrayList<Sequence>afterSeqList){
  142. ItemSettempItemSet;
  143. SequencetempSeq2;
  144. SequencetempSeq;
  145. ArrayList<Sequence>tempSeqList=newArrayList<>();
  146. for(Strings:singleItems){
  147. //分成2种形式递归,以<a>为起始项,第一种直接加入独立项集遍历<a,a>,<a,b><a,c>..
  148. if(isLargerThanMinSupport(s,afterSeqList)){
  149. tempSeq=beforeSeq.copySeqence();
  150. tempItemSet=newItemSet(s);
  151. tempSeq.getItemSetList().add(tempItemSet);
  152. totalFrequentSeqs.add(tempSeq);
  153. tempSeqList=newArrayList<>();
  154. for(Sequenceseq:afterSeqList){
  155. if(seq.strIsContained(s)){
  156. tempSeq2=seq.extractItem(s);
  157. tempSeqList.add(tempSeq2);
  158. }
  159. }
  160. recursiveSearchSeqs(tempSeq,tempSeqList);
  161. }
  162. //第二种递归为以元素的身份加入最后的项集内以a为例<(aa)>,<(ab)>,<(ac)>...
  163. //a在这里可以理解为一个前缀序列,里面可能是单个元素或者已经是多元素的项集
  164. tempSeq=beforeSeq.copySeqence();
  165. intsize=tempSeq.getItemSetList().size();
  166. tempItemSet=tempSeq.getItemSetList().get(size-1);
  167. tempItemSet.getItems().add(s);
  168. if(isLargerThanMinSupport(tempItemSet,afterSeqList)){
  169. tempSeqList=newArrayList<>();
  170. for(Sequenceseq:afterSeqList){
  171. if(seq.compoentItemIsContain(tempItemSet)){
  172. tempSeq2=seq.extractCompoentItem(tempItemSet
  173. .getItems());
  174. tempSeqList.add(tempSeq2);
  175. }
  176. }
  177. totalFrequentSeqs.add(tempSeq);
  178. recursiveSearchSeqs(tempSeq,tempSeqList);
  179. }
  180. }
  181. }
  182. /**
  183. *所传入的项组合在所给定序列中的支持度是否超过阈值
  184. *
  185. *@params
  186. *所需匹配的项
  187. *@paramseqList
  188. *比较序列数据
  189. *@return
  190. */
  191. privatebooleanisLargerThanMinSupport(Strings,ArrayList<Sequence>seqList){
  192. booleanisLarge=false;
  193. intcount=0;
  194. for(Sequenceseq:seqList){
  195. if(seq.strIsContained(s)){
  196. count++;
  197. }
  198. }
  199. if(count>=minSupport){
  200. isLarge=true;
  201. }
  202. returnisLarge;
  203. }
  204. /**
  205. *所传入的组合项集在序列中的支持度是否大于阈值
  206. *
  207. *@paramitemSet
  208. *组合元素项集
  209. *@paramseqList
  210. *比较的序列列表
  211. *@return
  212. */
  213. privatebooleanisLargerThanMinSupport(ItemSetitemSet,
  214. ArrayList<Sequence>seqList){
  215. booleanisLarge=false;
  216. intcount=0;
  217. if(seqList==null){
  218. returnfalse;
  219. }
  220. for(Sequenceseq:seqList){
  221. if(seq.compoentItemIsContain(itemSet)){
  222. count++;
  223. }
  224. }
  225. if(count>=minSupport){
  226. isLarge=true;
  227. }
  228. returnisLarge;
  229. }
  230. /**
  231. *序列模式分析计算
  232. */
  233. publicvoidprefixSpanCalculate(){
  234. Sequenceseq;
  235. SequencetempSeq;
  236. ArrayList<Sequence>tempSeqList=newArrayList<>();
  237. ItemSetitemSet;
  238. removeInitSeqsItem();
  239. for(Strings:singleItems){
  240. //从最开始的a,b,d开始递归往下寻找频繁序列模式
  241. seq=newSequence();
  242. itemSet=newItemSet(s);
  243. seq.getItemSetList().add(itemSet);
  244. if(isLargerThanMinSupport(s,totalSeqs)){
  245. tempSeqList=newArrayList<>();
  246. for(Sequences2:totalSeqs){
  247. //判断单一项是否包含于在序列中,包含才进行提取操作
  248. if(s2.strIsContained(s)){
  249. tempSeq=s2.extractItem(s);
  250. tempSeqList.add(tempSeq);
  251. }
  252. }
  253. totalFrequentSeqs.add(seq);
  254. recursiveSearchSeqs(seq,tempSeqList);
  255. }
  256. }
  257. printTotalFreSeqs();
  258. }
  259. /**
  260. *按模式类别输出频繁序列模式
  261. */
  262. privatevoidprintTotalFreSeqs(){
  263. System.out.println("序列模式挖掘结果:");
  264. ArrayList<Sequence>seqList;
  265. HashMap<String,ArrayList<Sequence>>seqMap=newHashMap<>();
  266. for(Strings:singleItems){
  267. seqList=newArrayList<>();
  268. for(Sequenceseq:totalFrequentSeqs){
  269. if(seq.getItemSetList().get(0).getItems().get(0).equals(s)){
  270. seqList.add(seq);
  271. }
  272. }
  273. seqMap.put(s,seqList);
  274. }
  275. intcount=0;
  276. for(Strings:singleItems){
  277. count=0;
  278. System.out.println();
  279. System.out.println();
  280. seqList=(ArrayList<Sequence>)seqMap.get(s);
  281. for(SequencetempSeq:seqList){
  282. count++;
  283. System.out.print("<");
  284. for(ItemSetitemSet:tempSeq.getItemSetList()){
  285. if(itemSet.getItems().size()>1){
  286. System.out.print("(");
  287. }
  288. for(Stringstr:itemSet.getItems()){
  289. System.out.print(str+"");
  290. }
  291. if(itemSet.getItems().size()>1){
  292. System.out.print(")");
  293. }
  294. }
  295. System.out.print(">,");
  296. //每5个序列换一行
  297. if(count==5){
  298. count=0;
  299. System.out.println();
  300. }
  301. }
  302. }
  303. }
  304. }
调用类Client.java:

  1. packageDataMining_PrefixSpan;
  2. /**
  3. *PrefixSpan序列模式挖掘算法
  4. *@authorlyq
  5. *
  6. */
  7. publicclassClient{
  8. publicstaticvoidmain(String[]agrs){
  9. StringfilePath="C:\\Users\\lyq\\Desktop\\icon\\input.txt";
  10. //最小支持度阈值率
  11. doubleminSupportRate=0.4;
  12. PrefixSpanTooltool=newPrefixSpanTool(filePath,minSupportRate);
  13. tool.prefixSpanCalculate();
  14. }
  15. }
输出的结果:

  1. 原始序列数据:
  2. <(bd)cb(ac)>
  3. <(bf)(ce)b(fg)>
  4. <(ah)(bf)abf>
  5. <(be)(ce)d>
  6. <a(bd)bcb(ade)>
  7. 序列模式挖掘结果:
  8. <a>,<aa>,<ab>,<aba>,<abb>,
  9. <b>,<ba>,<bb>,<bba>,<bbc>,
  10. <bbf>,<bc>,<bca>,<bcb>,<bcba>,
  11. <bcd>,<b(ce)>,<bd>,<(bd)>,<(bd)a>,
  12. <(bd)b>,<(bd)ba>,<(bd)bc>,<(bd)c>,<(bd)ca>,
  13. <(bd)cb>,<(bd)cba>,<be>,<bf>,<(bf)>,
  14. <(bf)b>,<(bf)bf>,<(bf)f>,
  15. <c>,<ca>,<cb>,<cba>,<cd>,
  16. <(ce)>,
  17. <d>,<da>,<db>,<dba>,<dbc>,
  18. <dc>,<dca>,<dcb>,<dcba>,
  19. <e>,
  20. <f>,<fb>,<fbf>,<ff>,
经过比对,与上述表格中的结果完全一致,从结果中可以看出他的递归顺序正是刚刚我所想要的那种。

算法实现时的难点

我在实现这个算法时确实碰到了不少的问题,下面一一列举。

1、Sequence序列在判断或者提取单项和组合项的时候,情况少考虑了,还有考虑到了处理的方式又可能错了。

2、递归的顺序在最早的时候考虑错了,后来对递归的顺序进行了调整。

3、在算法的调试时遇到了,当发现某一项出现问题时,不能够立即调试,因为里面陷入的递归层次实在太深,只能自己先手算此情况下的前缀,后缀序列,然后自己模拟出1个Seq调试,在纠正extract方法时用的比较多。

我对PrefixSpan算法的理解

实现了这个算法之后,再回味这个算法,还是很奇妙的,一个序列,通过从左往右的扫描,通过各个项集的子集,能够组合出许许多多的的序列模式,然后进行挖掘,PrefixSpan通过递归的形式全部找出,而且效率非常高,的确是个很强大的算法。

PrefixSpan算法整体的特点

首先一点,他不会产生候选序列,在产生投影数据库的时候(也就是产生后缀子序列),他的规模是不断减小的。PrefixSpan采用分治法进行序列的挖掘,十分的高效。唯一比较会有影响的开销就是在构造后缀子序列的过程,专业上的名称叫做构造投影数据库的时候。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值