package com.yang;

import java.util.*;

public class Apriori {

private double minsup = 0.2;// 最小支持度
    private double minconf = 0.2;// 最小置信度

// 注意使用IdentityHashMap,否则由于关联规则产生存在键值相同的会出现覆盖
    private IdentityHashMap ruleMap = new IdentityHashMap();

//private String[] transSet = { "abc", "abc", "acde", "bcdf", "abcd", "abcdf" };// 事务集合
                                                                                    // ,
                                                                                    // 可以根据需要从构造函数里传入
    private String[] transSet = { "abe", "bd", "bc", "abd", "ac", "bc","ac","abce","abc" };// 事务集合
    private int itemCounts = 0;// 候选1项目集大小,即字母的个数
    private TreeSet[] frequencySet = new TreeSet[40];// 频繁项集数组,[0]:代表1频繁集...,TreeSet()使用元素的自然顺序对元素进行排序
    private TreeSet maxFrequency = new TreeSet();// 最大频繁集[所有频繁的]
    private TreeSet candidate = new TreeSet();
    private TreeSet candidateSet[] = new TreeSet[40];// 候选集数组[0]:代表1候选集
    private int frequencyIndex;

public Apriori() {

maxFrequency = new TreeSet();
        itemCounts = counts();// 初始化1候选集的大小6个
        System.out.printf("1项集的大小"+itemCounts);
        // 初始化其他两个
        for (int i = 0; i < itemCounts; i++) {
            frequencySet[i] = new TreeSet();//初始化频繁项集数组
            candidateSet[i] = new TreeSet();//初始化候选集数组
        }
        candidateSet[0] = candidate;// 1候选集
    }

//主函数入口
    public static void main(String[] args) {
        Apriori ap = new Apriori();
        ap.run();
    }
    
    //方法运行
    public void run() {
        int k = 1;
        item1_gen();

do {
            k++;
            canditate_gen(k);
            frequent_gen(k);
        } while (!is_frequent_empty(k));
        frequencyIndex = k - 1;
        print_canditate();
        maxfrequent_gen();
        print_maxfrequent();
        ruleGen();
        rulePrint();
    }
    //记录每个事务中的元素出现次数,x在事务中出现的总次数。
    public double count_sup(String x) {
        int temp = 0;
        for (int i = 0; i < transSet.length; i++) {
            for (int j = 0; j < x.length(); j++) {
                if (transSet[i].indexOf(x.charAt(j)) == -1)//返回指定字符在此字符串中第一次出现处的索引,如果不作为一个字符串,返回-1
                    break;
                else if (j == (x.length() - 1))
                    temp++;
            }
        }
        return temp;
    }
    
    //统计1候选集的个数a,b,c,d,e,f,return值为6
    public int counts() {

String temp1 = null;
        char temp2 = 'a';
        // 遍历所有事务集String 加入集合,set自动去重了
        for (int i = 0; i < transSet.length; i++) {
            temp1 = transSet[i];
            for (int j = 0; j < temp1.length(); j++) {
                temp2 = temp1.charAt(j);//返回位置为j的temp1的值a
                candidate.add(String.valueOf(temp2));//treeSet添加会去掉重复的值
            }
        }
        return candidate.size();//中元素个数不重复,且递增排序
    }

//求1频繁集
    public void item1_gen() {
        String temp1 = "";
        double m = 0;

Iterator temp = candidateSet[0].iterator();//使用方法iterator()要求容器返回一个Iterator。
        while (temp.hasNext()) {//遍历temp(1候选集)
            temp1 = (String) temp.next();
            m = count_sup(temp1);//调用下面的方法,统计1候选集中每个元素个数,计算支持度时,用此m/transSet.length

// 符合条件的加入 1候选集
            if (m >= minsup * transSet.length) {//minsup * transSet.length的值为记录每个事务中的元素出现次数,判断是否1频繁集
                frequencySet[0].add(temp1);//1频繁集加入频繁项集数组,自动出去重复的集合
            }
        }
    }
    //求K候选集
    public void canditate_gen(int k) {
        String y = "", z = "", m = "";
        char c1 ,c2 ;

Iterator temp1 = frequencySet[k - 2].iterator();//iterator迭代器,用于数组遍历
        Iterator temp2 = frequencySet[0].iterator();//遍历频繁项集数组,[0]:代表1频繁集
        TreeSet h = new TreeSet();

while (temp1.hasNext()) {
            y = (String) temp1.next();//
            c1 = y.charAt(y.length() - 1);//返回指定y.length() - 1(数组的最后一个)的char值

while (temp2.hasNext()) {
                z = (String) temp2.next();

c2 = z.charAt(0);//c2=a,b,c,d,e,f
                if (c1 >= c2)
                    continue;//大于最后一个字符才拼上。abd,而无adb
                else {
                    m = y + z;//m为字符串组合yz
                    h.add(m);//m加入TreeSet
                }
            }
            temp2 = frequencySet[0].iterator();
        }
        candidateSet[k - 1] = h;
    }

// k候选集=>k频繁集
    public void frequent_gen(int k) {
        String s1 = "";

Iterator ix = candidateSet[k - 1].iterator();//遍历K候选集ix
        while (ix.hasNext()) {
            s1 = (String) ix.next();//ix中的值s1
            if (count_sup(s1) >= (minsup * transSet.length)) {//s1项集支持度大于最小支持度
                frequencySet[k - 1].add(s1);//s1加入K频繁集中
            }
        }
    }
    //判断频繁集为空
    public boolean is_frequent_empty(int k) {
        if (frequencySet[k - 1].isEmpty())
            return true;
        else
            return false;
    }
    //打印候选集 频繁集
    public void print_canditate() {

for (int i = 0; i < frequencySet[0].size(); i++) {
            Iterator ix = candidateSet[i].iterator();
            Iterator iy = frequencySet[i].iterator();
            System.out.print("候选集" + (i + 1) + ":");
            while (ix.hasNext()) {
                System.out.print((String) ix.next() + "\t");
            }
            System.out.print("\n" + "频繁集" + (i + 1) + ":");
            while (iy.hasNext()) {
                System.out.print((String) iy.next() + "\t");
            }
            System.out.println();
        }
    }

//求关联项集合
    public void maxfrequent_gen() {
        int i;
        for (i = 1; i < frequencyIndex; i++) {
            maxFrequency.addAll(frequencySet[i]);
        }
    }
    //打印频繁项集
    public void print_maxfrequent() {
        Iterator iterator = maxFrequency.iterator();
        System.out.print("频繁项集:");
        while (iterator.hasNext()) {
            System.out.print(((String) iterator.next()) + "\t");
        }
        System.out.println();
        System.out.println();
    }
    //关联规则项集
    public void ruleGen() {
        String s;
        Iterator iterator = maxFrequency.iterator();
        while (iterator.hasNext()) {
            s = (String) iterator.next();
            subGen(s);
        }
    }

//求关联规则
    //将1左移多少位,将s分成不重叠的两部分。生成所有关联规则。再判断支持度
    public void subGen(String s) {
        String x = "", y = "";
        for (int i = 1; i < (1 << s.length()) - 1; i++) {
            for (int j = 0; j < s.length(); j++) {
                if (((1 << j) & i) != 0) {
                    x += s.charAt(j);
                }
            }

for (int j = 0; j < s.length(); j++) {
                if (((1 << j) & (~i)) != 0) {

y += s.charAt(j);

}
            }
            if (count_sup(x + y) / count_sup(x) >= minconf) {
                ruleMap.put(x, y);
            }
            x = "";
            y = "";

}
    }

//打印关联规则
    public void rulePrint() {
        String x, y;
        float temp = 0;

Set hs = ruleMap.keySet();//迭代后只能用get取key,Set不包含重复元素的collection
        Iterator iterator = hs.iterator();
        System.out.println("关联规则:");
        while (iterator.hasNext()) {
            x = (String) iterator.next();

y = (String) ruleMap.get(x);

temp = (float) (count_sup(x + y) / count_sup(x));

System.out.println(x + (x.length() < 5 ? "\t" : "") + "-->" + y+ "\t" + "置信度: " + temp);
            
        }
    }
}
学习点:1.TreeSet.add自动去重

2.TreeSet[] frequencySet; TreeSet frequencySet[];两种方法定义的都是数组,似乎是相同的。

3.canditate_gen中,大于最后一个字符的时候才拼上,adb即abd,所以不存在adb这种。

4.生成关联规则时,是将1左移多少位。能够将所有的组合都生成。x与y不会重叠。

效果图:

Apriori算法-java的更多相关文章

  1. 频繁模式挖掘apriori算法介绍及Java实现

    频繁模式是频繁地出如今数据集中的模式(如项集.子序列或者子结构).比如.频繁地同一时候出如今交易数据集中的商品(如牛奶和面包)的集合是频繁项集. 一些基本概念 支持度:support(A=>B) ...

  2. 关联规则挖掘之apriori算法

    前言: 众所周知,关联规则挖掘是数据挖掘中重要的一部分,如著名的啤酒和尿布的问题.今天要学习的是经典的关联规则挖掘算法--Apriori算法 一.算法的基本原理 由k项频繁集去导出k+1项频繁集. 二 ...

  3. Apriori算法例子

    1 Apriori介绍 Apriori算法使用频繁项集的先验知识,使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描事务(交易)记录,找出所有的频繁1项集,该集合记做L1,然 ...

  4. Apriori算法实例----Weka,R, Using Weka in my javacode

    学习数据挖掘工具中,下面使用4种工具来对同一个数据集进行研究. 数据描述:下面这些数据是15个同学选修课程情况,在课程大纲中共有10门课程供学生选择,下面给出具体的选课情况,以ARFF数据文件保存,名 ...

  5. 玩转大数据:深入浅出大数据挖掘技术(Apriori算法、Tanagra工具、决策树)

    一.本课程是怎么样的一门课程(全面介绍) 1.1.课程的背景           “大数据”作为时下最火热的IT行业的词汇,随之而来的数据仓库.数据分析.数据挖掘等等围绕大数据的商业价值的利用逐渐成为 ...

  6. Apriori算法实现

    Apriori算法原理:http://blog.csdn.net/kingzone_2008/article/details/8183768 import java.util.HashMap; imp ...

  7. Apriori算法第二篇----详细分析和代码实现

    1 Apriori介绍 Apriori算法使用频繁项集的先验知识,使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描事务(交易)记录,找出所有的频繁1项集,该集合记做L1,然 ...

  8. 基于spark实现并行化Apriori算法

    详细代码我已上传到github:click me 一. 实验要求         在 Spark2.3 平台上实现 Apriori 频繁项集挖掘的并行化算法.要求程序利用 Spark 进行并行计算. ...

  9. MapReduce实现Apriori算法

    Apiroi算法在Hadoop MapReduce上的实现 输入格式: 一行为一个Bucket 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 ...

随机推荐

  1. x264宏块及子块划分方式

    1 宏块划分方式 一副图像(帧,非场图像,x264支持宏块级场编码,这里以帧图像为例说明)按从左到右.从上到下16x16的方式划分宏块,对于图像宽度不是16的倍数的情况,会扩展至16的倍数,然后通过s ...

  2. 第一次使用cnblogs

    第一次使用!!!!!留名纪念下!!!!!!!!!!!

  3. Retrofit,Rxjava,OkHttp3的配置

    这几个库的版本都更新了,和以前的使用略有不同,这是两篇介绍的博客:http://www.jianshu.com/p/91ac13ed076d ,https://drakeet.me/retrofit- ...

  4. 我的linux云服务器配置记录

    配置vps的时候,随手记录一下~~ 添加一些源: vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ xenial main ...

  5. Node.js:模块

    概要:本篇博客主要介绍node.js的模块 1.创建模块 在node.js中创建一个模块非常简单,因为一个文件就是一个模块.我们只需要明白如何从其他文件中获取这个模块.Node.js提供了 expor ...

  6. EasyuiAPI:布局

    一.Panel(面板) 1.通过标签创建: <div id="p" class="easyui-panel" title="My Panel&q ...

  7. hdu_5873_Football Games(xjb搞)

    题目链接:hdu_5873_Football Games 题意: 有n个队,每个队都会给其他队打一场,赢一场得2分,平局得一分,输了不得分,然后给你全部比赛结束后的得分,问你是否有假分 题解: 可以知 ...

  8. hdu_5862_Counting Intersections(扫描线)

    题目链接:hdu_5862_Counting Intersections 题意: 给你与坐标轴平行的线段,问你交点数 题解: 实质就是扫描线,这里我用树状数组来记录,所有线段按X坐标排序,遇到横线段的 ...

  9. sqlplus登录Oracle时ORA-01017: invalid username/password; logon denied的错误

    今天用scott用户登录Oracle数 据库时,竟然出现了ORA-01017: invalid username/password; logon denied错误,原以为是因为我的scott用户没有解 ...

  10. 使用UGUI实现拖拽功能(拼图小游戏)

    实现方式 1.引入UGUI自带的事件系统 UnityEngine.EventSystems 2.为我们的类添加接口 IBeginDragHandler, IDragHandler, IEndDragH ...