標籤:
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