实验结果

在这里插入图片描述
最小支持度0.001条件下可以得到准确结果,仅用1.6s
最小可以支持最小支持度为0.0003的计算

相关阅读

数据挖掘Apriori算法JAVA实现

完整代码

package com.company;import java.io.*;import java.util.*;classFPNode{
    String name;int count=0;
    FPNode brother= null, parent= null;
    ArrayList<FPNode> children=newArrayList<>();FPNode(String name){this.name= name;}FPNode(String name, FPNode parent,int count){this.name= name;this.parent= parent;this.count= count;}}classItem{
    String name;int support;Item(String name,int support){this.name= name;this.support= support;}}classTrade{
    ArrayList<Item> items=newArrayList<>();publicTrade(List<String> items, Map<String, Integer> frequentItems){
        items.forEach(item->{if(frequentItems.containsKey(item))this.items.add(newItem(item, frequentItems.get(item)));});this.items.sort((l, r)-> Integer.compare(r.support, l.support));//按支持度从大到小排序}}classFPTree{int minSupport;
    FPNode root=newFPNode("root");
    Map<String, Integer> frequentItems=newHashMap<>();
    Map<String, FPNode> headTable=newHashMap<>();//该项对应的最后一个节点
    Map<String, FPNode> currentPosition=newHashMap<>();
    List<List<String>> database;publicFPTree(List<List<String>> database,int minSupport){this.minSupport= minSupport;this.database= database;getFrequentItems();buildHeadTable();buildTree();}//计算支持度publicvoidgetFrequentItems(){
        Map<String, Integer> supportCount=newHashMap<>();for(List<String> line:
                database)for(String item:
                    line)
                supportCount.merge(item,1, Integer::sum);
        supportCount.forEach((item, support)->{if(support>= minSupport)
                frequentItems.put(item, support);});}//建立头表publicvoidbuildHeadTable(){
        frequentItems.keySet().forEach(frequentItem->{
            headTable.put(frequentItem,newFPNode(frequentItem));
            currentPosition.put(frequentItem, headTable.get(frequentItem));});}//建立FP-treepublicvoidbuildTree(){
        database.forEach(items->{
            Trade trade=newTrade(items, frequentItems);insertTree(trade, root);});}publicintinsertTree(Trade trade, FPNode fpNode){if(trade.items.size()==0)return0;
        Item item= trade.items.remove(0);
        FPNode nextFPNode;// 查找该项是否存在for(FPNode children:
                fpNode.children)if(children.name.equals(item.name)){
                children.count++;
                nextFPNode= children;returninsertTree(trade, nextFPNode);}
        nextFPNode=newFPNode(item.name, fpNode,1);
        fpNode.children.add(nextFPNode);
        currentPosition.get(item.name).brother= nextFPNode;
        currentPosition.put(item.name, nextFPNode);returninsertTree(trade, nextFPNode);}}classFrequentItem{
    List<String> items;int support;publicFrequentItem(List<String> items,int support){this.items= items;this.support= support;}}classFPGrowth{int minSupport;
    List<FrequentItem> patternList=newArrayList<>();publicFPGrowth(FPTree fpTree,int minSupport){this.minSupport= minSupport;fpGrowth(fpTree, null);}privatevoidfpGrowth(FPTree fpTree, List<String> suffix){if(fpTree.root.children.size()==0)return;
        fpTree.frequentItems.keySet().forEach(frequentItem->{
            List<String> newSuffix=newArrayList<>();
            newSuffix.add(frequentItem);if(suffix!= null&&!suffix.isEmpty())
                newSuffix.addAll(suffix);
            patternList.add(newFrequentItem(newSuffix, fpTree.frequentItems.get(frequentItem)));//生成条件模式库
            List<List<String>> conditionalPatternDatabase=generateConditionalPatternDatabase(fpTree, frequentItem);//生成条件FPTree
            FPTree conditionalFPTree=newFPTree(conditionalPatternDatabase, minSupport);fpGrowth(conditionalFPTree, newSuffix);});}private List<List<String>>generateConditionalPatternDatabase(FPTree fpTree, String frequentItem){
        List<List<String>> conditionalPatternDatabase=newArrayList<>();
        FPNode headNode= fpTree.headTable.get(frequentItem);for(FPNode fpNode= headNode.brother; fpNode!= null; fpNode= fpNode.brother){//生成前缀路径
            List<String> prefixPath=newArrayList<>();for(FPNode fpNode1= fpNode.parent; fpNode1.parent!= null; fpNode1= fpNode1.parent)
                prefixPath.add(fpNode1.name);for(int i=0; i< fpNode.count; i++)
                conditionalPatternDatabase.add(prefixPath);}return conditionalPatternDatabase;}public List<FrequentItem>getPatternList(){
        patternList.sort((l, r)-> Integer.compare(r.support, l.support));return patternList;}}publicclassMain{privatestaticfinal List<List<String>> database=newArrayList<>();publicstaticvoidmain(String[] args)throws IOException{double minSupport;int count=0;
        Scanner scanner=newScanner(System.in);
        minSupport= scanner.nextDouble();long startTime= System.currentTimeMillis();loadData();
        FPTree fpTree=newFPTree(database,(int) Math.ceil(minSupport* database.size()));
        FPGrowth fpGrowth=newFPGrowth(fpTree,(int) Math.ceil(minSupport* database.size()));for(FrequentItem frequentItem:
                fpGrowth.getPatternList()){
            System.out.println(frequentItem.items+": "+ frequentItem.support);
            count++;}
        System.out.println("总数: "+ count);long endTime= System.currentTimeMillis();
        System.out.println("程序运行时间:"+(endTime- startTime)+"ms");}privatestaticvoidloadData()throws IOException{try(BufferedReader bufferedReader=newBufferedReader(newFileReader("retail.dat"))){
            String line;while((line= bufferedReader.readLine())!= null){
                String[] temp= line.split(" ");
                database.add(Arrays.asList(temp));}}}}