PrefixSpan序列模式挖掘算法

介绍

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

算法原理

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


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

  

"_"下标符代表前缀为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个序列模式。可以与后面程序算法测试的结果做对比。

算法的代码实现

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

测试数据:

[java]  view plain  copy
 print ?
  1. bd c b ac  
  2. bf ce b fg  
  3. ah bf a b f  
  4. be ce d  
  5. a bd b c b ade  
Sequence.java:

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

[java]  view plain  copy
 print ?
  1. package DataMining_PrefixSpan;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. /** 
  6.  * 字符项集类 
  7.  *  
  8.  * @author lyq 
  9.  *  
  10.  */  
  11. public class ItemSet {  
  12.     // 项集内的字符  
  13.     private ArrayList<String> items;  
  14.   
  15.     public ItemSet(String[] str) {  
  16.         items = new ArrayList<>();  
  17.         for (String s : str) {  
  18.             items.add(s);  
  19.         }  
  20.     }  
  21.   
  22.     public ItemSet(ArrayList<String> itemsList) {  
  23.         this.items = itemsList;  
  24.     }  
  25.   
  26.     public ItemSet(String s) {  
  27.         items = new ArrayList<>();  
  28.         for (int i = 0; i < s.length(); i++) {  
  29.             items.add(s.charAt(i) + "");  
  30.         }  
  31.     }  
  32.   
  33.     public ArrayList<String> getItems() {  
  34.         return items;  
  35.     }  
  36.   
  37.     public void setItems(ArrayList<String> items) {  
  38.         this.items = items;  
  39.     }  
  40.   
  41.     /** 
  42.      * 获取项集最后1个元素 
  43.      *  
  44.      * @return 
  45.      */  
  46.     public String getLastValue() {  
  47.         int size = this.items.size();  
  48.   
  49.         return this.items.get(size - 1);  
  50.     }  
  51. }  
PrefixSpanTool.java:

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

[java]  view plain  copy
 print ?
  1. package DataMining_PrefixSpan;  
  2.   
  3. /** 
  4.  * PrefixSpan序列模式挖掘算法 
  5.  * @author lyq 
  6.  * 
  7.  */  
  8. public class Client {  
  9.     public static void main(String[] agrs){  
  10.         String filePath = "C:\\Users\\lyq\\Desktop\\icon\\input.txt";  
  11.         //最小支持度阈值率  
  12.         double minSupportRate = 0.4;  
  13.           
  14.         PrefixSpanTool tool = new PrefixSpanTool(filePath, minSupportRate);  
  15.         tool.prefixSpanCalculate();  
  16.     }  
  17. }  
输出的结果:

[java]  view plain  copy
 print ?
  1. 原始序列数据:  
  2. <(b d )c b (a c )>  
  3. <(b f )(c e )b (f g )>  
  4. <(a h )(b f )a b f >  
  5. <(b e )(c e )d >  
  6. <a (b d )b c b (a d e )>  
  7. 序列模式挖掘结果:  
  8.   
  9.   
  10. <a >, <a a >, <a b >, <a b a >, <a b b >,   
  11.   
  12.   
  13. <b >, <b a >, <b b >, <b b a >, <b b c >,   
  14. <b b f >, <b c >, <b c a >, <b c b >, <b c b a >,   
  15. <b c d >, <b (c e )>, <b d >, <(b d )>, <(b d )a >,   
  16. <(b d )b >, <(b d )b a >, <(b d )b c >, <(b d )c >, <(b d )c a >,   
  17. <(b d )c b >, <(b d )c b a >, <b e >, <b f >, <(b f )>,   
  18. <(b f )b >, <(b f )b f >, <(b f )f >,   
  19.   
  20. <c >, <c a >, <c b >, <c b a >, <c d >,   
  21. <(c e )>,   
  22.   
  23. <d >, <d a >, <d b >, <d b a >, <d b c >,   
  24. <d c >, <d c a >, <d c b >, <d c b a >,   
  25.   
  26. <e >,   
  27.   
  28. <f >, <f b >, <f b f >, <f f >,   
经过比对,与上述表格中的结果完全一致,从结果中可以看出他的递归顺序正是刚刚我所想要的那种。

算法实现时的难点

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

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

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

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

我对PrefixSpan算法的理解

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

PrefixSpan算法整体的特点

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值