-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApriori.java
225 lines (192 loc) · 7.13 KB
/
Apriori.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package apriori;
import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
public class Apriori {
int Tlim,Ilim,minsupport;
boolean DatasetTable [][];
File f;
HashMap<HashSet<Integer>,Integer>k_frequentItemSet=null;
Apriori(int Tlim,int Ilim,int minsupport,File f){
this.Tlim=Tlim;
this.Ilim=Ilim;
this.minsupport=minsupport;
this.f=f;
DatasetTable =new boolean[Tlim][Ilim];
k_frequentItemSet=new HashMap<HashSet<Integer>,Integer>();
}
void CreateRandomDataset() throws IOException{
int n;
Scanner s=new Scanner(System.in);
Vector<Integer> v=new Vector();
FileWriter fw=null;
try{
fw=new FileWriter(f);
}
catch(IOException e){
System.out.println("Problem opening file");
System.exit(-1);
}
fw.write("TID\tList Of ItemIDs\n");
for(int i=0;i<Tlim;i++){
fw.write("T"+i+"\t");
v.clear();
System.out.println("Enter no of items for T "+ i);
n=s.nextInt();
int j=0;
while(j<n){
int t=(int)(Math.random()*Ilim);
if(!v.contains(t)){
v.add(t);
j++;
}
}
Collections.sort(v);
System.out.println("Size"+v.size());
for(int iter=0;iter<v.size();iter++)
fw.write("I"+v.get(iter)+" ");
fw.write("\n");
//fw.flush();
}
fw.close();
}
void ReadDatasetFile() throws FileNotFoundException,IOException{
FileReader fr=new FileReader(f);
for(int i=0;i<Tlim;i++)
for(int j=0;j<Ilim;j++)
DatasetTable[i][j]=false;
while(fr.read()!='\n');
int ch=fr.read();
int transno=0,itemno=0;
while(ch!=-1){
if(ch=='\n')
transno++;
if((char)ch=='I'){
itemno=0;
while((char)ch!=' '){
int c=fr.read();
itemno=itemno*10+c-48;
ch=fr.read();
}
DatasetTable[transno][itemno]=true;
}
ch=fr.read();
}
System.out.println("Table created");
for(int i=0;i<Tlim;i++)
{for(int j=0;j<Ilim;j++)
System.out.print(DatasetTable[i][j]+" ");
System.out.println("");
}
}
void onefrequentItemset(){
for(int i=0;i<Ilim;i++){
//=======itemset generation=======
HashSet<Integer> itemset=new HashSet<Integer>();
itemset.add(i);
//=======put itemset as key, count not calculated yet=======
k_frequentItemSet.put(itemset, 0);
int count=0;
//count value
for(int transno=0;transno<Tlim;transno++)
if(DatasetTable[transno][i])count++;
k_frequentItemSet.put(itemset, count);
}
System.out.println("1-Itemset generated\n"+k_frequentItemSet+"\n");
}
HashSet<Integer> canJoin(HashSet<Integer> anItemSet,HashSet<Integer> anotherItemSet,int k){
HashSet<Integer> newItemSet=new HashSet<Integer>();
Iterator<Integer> i1=anItemSet.iterator();
Iterator<Integer>i2=anotherItemSet.iterator();
int count=0;int n1=0,n2=0;
while(i1.hasNext()&&i2.hasNext())
{
n1=i1.next();n2=i2.next();
if(n1==n2)
{
newItemSet.add(n1);
count++;
}
else break;
}
if(count<(k-2)||n1>n2)
return null;
newItemSet.add(n1);
newItemSet.add(n2);
return newItemSet;
}
HashMap<HashSet<Integer>,Integer> generateCandidateItemSet(int k){
Set<HashSet<Integer>> prevItemSets=k_frequentItemSet.keySet();
HashMap<HashSet<Integer>,Integer>CandidateItemSet=new HashMap<HashSet<Integer>,Integer>();
HashSet<Integer> newItemSet;
for(HashSet<Integer> anItemSet:prevItemSets){
for(HashSet<Integer> anotherItemSet:prevItemSets){
if(!anItemSet.equals(anotherItemSet))
{
newItemSet=canJoin(anItemSet,anotherItemSet,k);
if(newItemSet!=null)
CandidateItemSet.put(newItemSet, 0);
}
}
}
return CandidateItemSet;
}
void setCounts(HashMap<HashSet<Integer>,Integer>CandidateItemSet){
Set<HashSet<Integer>> currItemSets=CandidateItemSet.keySet();
boolean flag=true;int count=0;
for(HashSet<Integer> anItemSet:currItemSets){
count=0;
for(int transno=0;transno<Tlim;transno++){
flag=true;
Iterator<Integer> items=anItemSet.iterator();
while(items.hasNext()){
int itemno=items.next();
if(!DatasetTable[transno][itemno])
flag=false;
}
if(flag)count++;
}
CandidateItemSet.put(anItemSet, count);
}
}
HashMap<HashSet<Integer>,Integer> prune(HashMap<HashSet<Integer>,Integer>CandidateItemSet){
Set<Entry<HashSet<Integer>,Integer>> EntrySet= CandidateItemSet.entrySet();
HashMap<HashSet<Integer>,Integer> finalset=new HashMap<HashSet<Integer>,Integer>();
for(Map.Entry<HashSet<Integer>,Integer> entry:EntrySet){
if(entry.getValue()>=minsupport)
finalset.put(entry.getKey(), entry.getValue());
}
return finalset;
}
void generateFrequentItemSet(int k){
HashMap<HashSet<Integer>,Integer>CandidateItemSet=generateCandidateItemSet(k);
System.out.println(k+ " -Itemsets\n"+CandidateItemSet);
setCounts(CandidateItemSet);
System.out.println(k+ " -Itemsets , After setting counts\n"+CandidateItemSet);
k_frequentItemSet=prune(CandidateItemSet);
System.out.println(k+" -Frequent Itemset, After Pruning\n"+k_frequentItemSet+"\n\n");
}
public static void main(String[] args) throws IOException {
int Tlim,Ilim,minsupport=2;
File f=new File("dataset.txt");
Scanner s=new Scanner(System.in);
System.out.println("Enter no of transactions");
Tlim=s.nextInt();
System.out.println("Enter no of items");
Ilim=s.nextInt();
//Initialisation Completed
Apriori a=new Apriori(Tlim,Ilim,minsupport,f);
a.CreateRandomDataset();//write a random dataset into file
a.ReadDatasetFile();//store transaction details into 2-D array "DatasetTable "
a.onefrequentItemset();
for(int i=2;i<Ilim;i++)
a.generateFrequentItemSet(i);
}
}