-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleMiner.java
725 lines (678 loc) · 17.3 KB
/
RuleMiner.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
import java.util.*;
import java.io.*;
/**
* @author Tanmaya
* @param <KeyType> Type of the objects in the lists to be hashed
*/
class HashTree <KeyType>
{
Hasher<KeyType> H;
int max_leaf;
int list_len;
INode<KeyType> root;
/**
* Constructor for a hash tree
* @param H The Hasher object which hashes a KeyType object to an integer
* @param max_leaf The maximum number of entries in a leaf after which it splits
* @param list_len The length of the object array
*/
HashTree(Hasher<KeyType> H, int max_leaf, int list_len)
{
this.H=H;
this.max_leaf=max_leaf;
this.list_len=list_len;
this.root=new INode<KeyType>(0);
}
/**
* Add an array of objects to the tree
* @param l The array of objects
*/
void add(KeyType[] l)
{
if(l.length!=list_len)
{
System.err.println("Length does not match");
System.exit(1);
}
root.add(l, max_leaf, H, list_len);
}
/**
* Checks whether an array of objects is in the tree
* @param l The array of objects to be checked
* @return True or false depending on the existence of the array in the tree
*/
boolean contains(KeyType[] l)
{
if(l.length!=list_len)
{
System.err.println("Length does not match");
System.exit(1);
}
INode curr = root;
Node x = root;
for(int i=0; i<list_len; i++)
{
x = curr.getChild(H.hash(l[i]));
if(x==null)
return false;
if(x.is_leaf)
break;
else
curr=(INode)x;
}
LNode<KeyType> leaf=(LNode<KeyType>)x;
for(KeyType[] j:leaf.list)
{
int k=0;
for(; k<list_len; k++)
{
if(!j[k].equals(l[k]))
break;
}
if(k==list_len)
return true;
}
return false;
}
}
/**
* Class abstracting the node of the tree
* @author Tanmaya
* @param <ItemType> The type of items to be hashed
*/
abstract class Node<ItemType>
{
boolean is_leaf;
}
/**
* Class representing the internal nodes of the tree
* @author Tanmaya
* @param <ItemType> The type of items to be hashed
*/
class INode<ItemType> extends Node<ItemType>
{
HashMap<Integer, Node> children;
int depth;
/**
* Constructor for an internal node
* @param depth The depth of this node from root
*/
INode(int depth)
{
this.depth=depth;
this.is_leaf=false;
children=new HashMap<Integer, Node>();
}
/**
* Gets the child node of this node given a hash code
* @param hashcode The hash code of the node needed
* @return the child node or null if it doesn't exist
*/
Node getChild(int hashcode)
{
return children.get(hashcode);
}
/**
* Adds an array of objects to the subtree with this node as the root
* @param l the array of objects to be added
* @param max_leaf The maximum number of items before a split
* @param h The Hasher object used to hash the objects
* @param list_len The length of the array
*/
void add(ItemType[] l, int max_leaf, Hasher<ItemType> h, int list_len)
{
int hashcode=h.hash(l[depth]);
Node child=getChild(hashcode);
if(child==null)
{
LNode<ItemType> x= new LNode<ItemType>(depth+1);
x.add(l);
children.put(hashcode, x);
}
else if (!child.is_leaf)
{
INode x=(INode)child;
x.add(l, max_leaf, h, list_len);
}
else
{
LNode<ItemType> x=(LNode<ItemType>)child;
if(x.count<max_leaf || depth == list_len - 1)
{
x.add(l);
}
else
{
INode<ItemType> n=new INode<ItemType>(depth + 1);
List<ItemType[]> list=x.list;
children.remove(hashcode);
for(ItemType[] i:list)
{
n.add(i, max_leaf, h, list_len);
}
n.add(l, max_leaf, h, list_len);
children.put(hashcode, n);
}
}
}
}
/**
* Class representing the leaf of the tree.
* @author Tanmaya
* @param <ItemType> The type of items to be hashed
*/
class LNode<ItemType> extends Node<ItemType>
{
List<ItemType[]> list;
int depth;
int count;
/**
* Constructor for a leaf node
* @param depth The depth of this node from root
*/
LNode(int depth)
{
list=new ArrayList<ItemType[]>();
this.depth=depth;
this.count=0;
this.is_leaf=true;
}
/**
* Add an array of objects to this leaf node
* @param l The array of objects
*/
void add(ItemType[] l)
{
list.add(l);
count++;
}
/**
* Gets a list of all the arrays stored in this leaf
* @return An ArrayList of the arrays of objects
*/
List<ItemType[]> getList()
{
return list;
}
}
/**
* The interface for hashing a particular object type
* @author Tanmaya
* @param <ItemType>
*/
interface Hasher<ItemType>
{
/**
* Hashes an object of ItemType type into integer
* @param item The item to be hashed
* @return The integer hashed value
*/
public int hash(ItemType item);
}
/**
* Hashes an object using the default Java hashcode() function
* @author Tanmaya
*/
class DefaultHasher implements Hasher<Object>
{
@Override
public int hash(Object item)
{
return item.hashCode();
}
}
class ModHasher implements Hasher<Integer>
{
int k;
ModHasher(int k)
{
this.k=k;
}
@Override
public int hash(Integer item)
{
return item%k;
}
}
/**
* Class representing an itemset of Integer objects
* @author Amala
*/
class Itemset
{
ArrayList<Integer> itemset;
int count;
/**
* Constructor for an Itemset
*/
Itemset()
{
this.itemset = new ArrayList<Integer>();
this.count = 0;
}
/**
* Parameterized Constructor for an Itemset
* @param itemset An ArrayList of Integers representing the itemset
*/
Itemset(ArrayList<Integer> itemset)
{
this.itemset = itemset;
this.count = 0;
}
/**
* Add an item to the itemset
* @param item The Integer object to be added to the itemset
*/
void add(Integer item)
{
this.itemset.add(item);
}
/**
* Get the item at a particular index
* @param index The index from which the item has to be retrieved
* @return The Integer object at the specified index
*/
Integer getItem(int index)
{
return this.itemset.get(index);
}
/**
* Get the first num items from the itemset
* @param num The number of items to be retrieved
* @return An ArrayList of Integer objects
*/
ArrayList<Integer> getItems(int num)
{
ArrayList<Integer> items = new ArrayList<Integer>();
for(int i=0; i<num; i++)
items.add(this.itemset.get(i));
return items;
}
/**
* Set the support count of the itemset
* @param count The value to which the support count has to be set
*/
void setCount(int count)
{
this.count = count;
}
/**
* Returns the support count of the itemset
* @return An integer denoting the support count of the itemset
*/
int getCount()
{
return this.count;
}
/**
* Returns the size of the itemset
* @return An integer whose value is equal to the size of the itemset
*/
int size()
{
return this.itemset.size();
}
/**
* Compares an Itemset with another Itemset for the first few specified items
* @param itemset The Itemset to be compared with
* @param len The number of items from the beginning to be compared
* @return True if the items match, false otherwise
*/
boolean compare(Itemset itemset, int len)
{
for(int i=0; i<len; i++)
if(!this.itemset.get(i).equals(itemset.getItems(itemset.size()).get(i)))
return false;
return true;
}
@Override
public String toString()
{
return "("+itemset+", "+count+")";
}
@Override
public boolean equals(Object object)
{
boolean isEqual= false;
if (object != null && object instanceof Itemset)
{
isEqual = (this.itemset.equals(((Itemset) object).itemset));
}
return isEqual;
}
}
/**
* Class representing a Rule
* @author Amala
*/
class Rule
{
Itemset antecedant, consequent;
double confidence;
/**
* Parameterized constructor for a rule
* @param ant The antecedant itemset for the rule
* @param con The consequent itemset for the rule
* @param The confidence value of the rule
*/
Rule(Itemset ant, Itemset con, double conf)
{
antecedant = ant;
consequent = con;
confidence = conf;
}
@Override
public String toString()
{
return "("+antecedant+", "+consequent+","+confidence+")";
}
}
class RuleMiner{
private static void getSubsets(ArrayList<Integer> superSet, int k, int idx, Set<Integer> current,ArrayList<Set<Integer>> solution)
{
if (current.size() == k) {
solution.add(new HashSet<>(current));
return;
}
if (idx == superSet.size()) return;
Integer x = superSet.get(idx);
current.add(x);
getSubsets(superSet, k, idx+1, current, solution);
current.remove(x);
getSubsets(superSet, k, idx+1, current, solution);
}
private static ArrayList<Set<Integer>> getSubsets(ArrayList<Integer> superSet, int k)
{
ArrayList<Set<Integer>> res = new ArrayList<>();
getSubsets(superSet, k, 0, new HashSet<Integer>(), res);
return res;
}
private static ArrayList<ArrayList<Integer>> aprioriGen(ArrayList<Itemset> CFp)
{
ArrayList<ArrayList<Integer>> CF = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<CFp.size(); i++)
{
for(int j=i+1; j<CFp.size(); j++)
{
Itemset item1 = CFp.get(i);
Itemset item2 = CFp.get(j);
int flag = 1;
if(item1.compare(item2, item1.size()-1))
{
ArrayList<Integer> newitemset = item1.getItems(item1.size()-1);
newitemset.add(item1.itemset.get(item1.size()-1));
newitemset.add(item2.itemset.get(item2.size()-1));
Collections.sort(newitemset);
for(int l=0; l<newitemset.size()-2; l++)
{
ArrayList<Integer> items = new ArrayList<Integer>();
for(Integer in: newitemset)
items.add(in);
items.remove(l);
Itemset temp = new Itemset(items);
if(!CFp.contains(temp))
{
flag = 0; break;
}
}
if(flag==0)
continue;
else
CF.add(newitemset);
}
}
}
return CF;
}
public static ArrayList<ArrayList<Itemset>> getFrequentItemsets( ArrayList<ArrayList<Integer>> trn, int n_items, double minsup)
{
int k = 1;
ArrayList<ArrayList<Itemset>> frequentISL = new ArrayList<ArrayList<Itemset>>();
ArrayList<Itemset> F1 = new ArrayList<Itemset>();
int count[] = new int[n_items];
for(ArrayList<Integer> list: trn)
{
for(Integer I: list)
count[I.intValue()]++;
}
for(int i=0; i<n_items; i++)
{
if(count[i] >= trn.size()*minsup)
{
Itemset itemset = new Itemset();
itemset.add(new Integer(i));
itemset.setCount(count[i]);
F1.add(itemset);
}
}
frequentISL.add(F1);
while(true)
{
k++;
HashTree<Integer> h = new HashTree<Integer>(new ModHasher(n_items), 3, k);
ArrayList<Itemset> CFp = frequentISL.get(frequentISL.size()-1);
ArrayList<ArrayList<Integer>> Fk = aprioriGen(CFp);
for(ArrayList<Integer> newitemset: Fk)
{
Integer arr[] = new Integer[newitemset.size()];
newitemset.toArray(arr);
h.add(arr);
}
HashMap<ArrayList<Integer>, Integer> CF = new HashMap<ArrayList<Integer>, Integer>();
for(ArrayList<Integer> array: Fk)
{
CF.put(array, new Integer(0));
}
for(int i=0; i<trn.size(); i++)
{
ArrayList<Integer> transaction = trn.get(i);
ArrayList<Set<Integer>> subsets = getSubsets(transaction, k);
int l = 0;
for(Set<Integer> subset: subsets)
{
ArrayList<Integer> arrlist = new ArrayList<Integer>(subset);
Collections.sort(arrlist);
Integer arr[] = new Integer[arrlist.size()];
arrlist.toArray(arr);
if(h.contains(arr))
{
Integer in = new Integer(CF.get(arrlist).intValue() + 1);
CF.put(arrlist, in);
}
}
}
ArrayList<Itemset> F = new ArrayList<Itemset>();
Iterator<Map.Entry<ArrayList<Integer>, Integer>> iter = CF.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry<ArrayList<Integer>, Integer> entry = iter.next();
if(entry.getValue().intValue()<minsup*435)
{
iter.remove();
}
else
{
Itemset tem = new Itemset(entry.getKey());
tem.setCount(entry.getValue().intValue());
F.add(tem);
}
}
if(F.size()>=1)
{
frequentISL.add(F);
}
if(F.isEmpty()||F.size()==1)
break;
}
return frequentISL;
}
public static void apGenRules(ArrayList<ArrayList<Itemset>> F, Itemset fk, ArrayList<Itemset> Hm, double minconf, ArrayList<Rule> rules)
{
if(Hm.size()==0)
return ;
int k = fk.size(), m = Hm.get(Hm.size()-1).size();
if(k>m+1)
{
ArrayList<ArrayList<Integer>> Hm1 = aprioriGen(Hm);
for(int i=0; i<Hm1.size(); i++)
{
ArrayList<Integer> hm1 = Hm1.get(i);
ArrayList<Integer> antc = new ArrayList<Integer>();
for(Integer it: fk.itemset)
if(!hm1.contains(it))
antc.add(it);
int tempc = 1;
Itemset temp = new Itemset(antc);
for(Itemset item: F.get(fk.size()-hm1.size()-1))
if(item.equals(temp))
{
tempc = item.getCount(); break;
}
double conf = (double)(fk.getCount())/tempc;
if(conf>=minconf)
{
temp.setCount(tempc);
int consc = 1;
Itemset cons = new Itemset(hm1);
for(Itemset item: F.get(hm1.size()-1))
if(item.equals(cons))
{
consc = item.getCount(); break;
}
cons.setCount(consc);
Rule rule = new Rule(temp, cons, conf);
rules.add(rule);
}
else
{
Hm1.remove(i);
i--;
}
}
ArrayList<Itemset> HM1 = new ArrayList<Itemset>();
for(ArrayList<Integer> item: Hm1)
HM1.add(new Itemset(item));
apGenRules(F, fk, HM1, minconf, rules);
}
}
public static ArrayList<Rule> ruleGen(ArrayList<ArrayList<Itemset>> F, int n_items, double minconf)
{
ArrayList<Rule> rules = new ArrayList<Rule>();
for(int k=1; k<F.size(); k++)
{
ArrayList<Itemset> Fk = F.get(k);
for(Itemset fk: Fk)
{
ArrayList<Itemset> H1 = new ArrayList<Itemset>();
for(int i=0; i<fk.size(); i++)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(fk.getItem(i));
H1.add(new Itemset(temp));
for(Itemset itemset: F.get(0))
if(itemset.equals(H1.get(H1.size()-1)))
H1.get(H1.size()-1).setCount(itemset.getCount());
}
for(int i=0; i<H1.size(); i++)
{
Itemset h1 = H1.get(i);
ArrayList<Integer> antc = new ArrayList<Integer>();
for(Integer it: fk.itemset)
if(!h1.itemset.contains(it))
antc.add(it);
int tempc = 1;
Itemset temp = new Itemset(antc);
for(Itemset item: F.get(fk.size()-2))
if(item.equals(temp))
{
tempc = item.getCount(); break;
}
double conf = (double)(fk.getCount())/tempc;
if(conf>=minconf)
{
temp.setCount(tempc);
Rule rule = new Rule(temp, h1, conf);
rules.add(rule);
}
else
{
H1.remove(i);
i--;
}
}
apGenRules(F, fk, H1, minconf, rules);
}
}
return rules;
}
public static void main(String args[])
{
ArrayList<ArrayList<Integer>> trn = new ArrayList<ArrayList<Integer>>();
int items = 34;
String file = "opfile.txt", file1 = "Items.txt";
String item_names[] = new String[items];
try{
Scanner sc = new Scanner(new File(file));
int i = 0, temp = 0;
System.out.println("Reading file...");
while(sc.hasNextInt())
{
if(i%17 == 0)
trn.add(new ArrayList<Integer>());
temp = sc.nextInt();
if(temp == 0)
trn.get(i/17).add((i%17)*2);
else if(temp == 1)
trn.get(i/17).add((i%17)*2+1);
i++;
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
try (BufferedReader br = new BufferedReader(new FileReader(file1))) {
String line;
int i=0;
while ((line = br.readLine()) != null)
{
item_names[i++] = line;
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
//Scanner scan = new Scanner(System.in);
//System.out.print("Enter minsup value: ");
double minsup = 0.45;//scan.nextDouble();
//System.out.print("Enter minconf value: ");
double minconf = 0.95;//scan.nextDouble();
ArrayList<ArrayList<Itemset>> frequentISL = getFrequentItemsets(trn, items, minsup);
System.out.println("The frequent itemsets generated are as follows:");
for(ArrayList<Itemset> array: frequentISL)
{
for(Itemset it: array)
{
System.out.print("[ ");
for(Integer in: it.itemset)
System.out.print(item_names[in.intValue()]+" ");
System.out.println("] "+ it.getCount());
}
}
ArrayList<Rule> rules = ruleGen(frequentISL, items, minconf);
System.out.println();
System.out.println("The rules generated are as follows:");
for(Rule rule: rules)
{
for(Integer it: rule.antecedant.itemset)
System.out.print(item_names[it.intValue()]+" ");
System.out.print("("+rule.antecedant.getCount()+") => ");
for(Integer it: rule.consequent.itemset)
System.out.print(item_names[it.intValue()]+" ");
System.out.println("("+rule.consequent.getCount()+") conf("+rule.confidence+")");
}
}
}