/
DoubleBeam_weights.py
737 lines (630 loc) · 35 KB
/
DoubleBeam_weights.py
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
726
727
728
729
730
731
732
733
734
735
736
737
import Orange
import orange
import sys
import operator
from datetime import datetime
from SDRule_updated_weights import *
true = 1
false = 0
class DoubleBeam_weights:
def __init__(self, type = "harmonic",weight_factor = 0.9, minSupport = 0.2, beam_width=10, g=1, refinement_heuristics = "Inverted Laplace", selection_heuristics = "Laplace", **kwds):
self.minSupport = minSupport
self.g = g
self.CandidatesList = set()
self.selectionBeamWidth = beam_width
self.refinementBeamWidth = 500
self.refinementCandidates = []
self.selectionCandidates = []
self.alredyRefinedRules = dict()
self.refinement_heuristics = refinement_heuristics
self.selection_heuristics = selection_heuristics
self.alreadySelectedRules = set()
#self.weights_type = "geometric"
self.weights_type = type
#self.weight_factor = 0.9
self.weight_factor = weight_factor
def __call__(self, data, targetClass, num_of_rules ):
self.alredyRefinedRules[str(targetClass)] = set()
if self.dataOK(data): # Checks weather targetClass is discrete
data_discretized = False
# If any of the attributes are continuous, discretize them
if data.domain.hasContinuousAttributes():
original_data = data
data_discretized = True
new_domain = []
discretize = orange.EntropyDiscretization(forceAttribute=True)
for attribute in data.domain.attributes:
if attribute.varType == orange.VarTypes.Continuous:
d_attribute = discretize(attribute, data)
# An attribute is irrelevant, if it is discretized into a single interval
# if len(d_attribute.getValueFrom.transformer.points) > 0:
new_domain.append(d_attribute)
else:
new_domain.append(attribute)
data = original_data.select(new_domain + [original_data.domain.classVar])
self.data = data
self.weigted_data = data
self.c = orange.newmetaid()
self.count = orange.newmetaid()
self.weigted_data.addMetaAttribute(self.c)
self.weigted_data.addMetaAttribute(self.count)
#print self.c
#print self.weigted_data.domain.attributes
self.targetClass = targetClass
#Initialize CanditatesList (all features)
self.fillCandidatesList(data,targetClass)
"""
print "Candidates for refinement:\n"
for rule in self.refinementCandidates:
print "N: %d\t\tTP: %d\t\t\tFP: %d\t\tRule:\t%s" %(len(rule.TP)+len(rule.FP),len(rule.TP), len(rule.FP), rule.ruleToString())
print "\nCandidates for selection:\n"
for rule in self.selectionCandidates:
print "N: %d\t\tTP: %d\t\t\tFP: %d\t\tRule:\t%s" %(len(rule.TP)+len(rule.FP),len(rule.TP), len(rule.FP), rule.ruleToString())
"""
"""
print self.refinementCandidates[0].ruleToString()
print "Best refinement: P %d\tN %d\tp %d\tn %d\tRQ %.3f" %(self.refinementCandidates[0].P,self.refinementCandidates[0].N,len(self.refinementCandidates[0].TP),len(self.refinementCandidates[0].FP), self.refinementCandidates[0].refinement_quality)
print "\n\n"
"""
#Initialize RefinementBeam, consisting of refinementBeamWidth empty rules
self.initializeRefinementBeam()
#Initialize SelectionBeam, consisting of selectionBeamWidth empty rules
self.initializeSelectionBeam()
#update RefinementBeam
self.updateRefinementBeam(self.refinementCandidates)
#update SelectionBeam
#self.chooseSelectionCandidates(self.RefinementBeam)
"""
print self.selectionCandidates[0].ruleToString()
print "Best selection: P %d\tN %d\tp %d\tn %d\tSQ %.3f" %(self.selectionCandidates[0].P,self.selectionCandidates[0].N,len(self.selectionCandidates[0].TP),len(self.selectionCandidates[0].FP), self.selectionCandidates[0].selection_quality)
print "\n\n"
"""
#print "Before updatation"
self.updateSelectionBeam(self.selectionCandidates)
#print "After update"
#self.printBeam(self.refinementCandidates, name="Refinement candidates")
#self.printBeam(self.RefinementBeam, name="Refinement beam")
#self.printBeam(self.refinementCandidates, name="Refinement candidates")
#self.printBeam(self.SelectionBeam, name="Selection beam")
improvements = True
refinement_improvements = True
ms=2
max_steps=5
# and i<max_steps and refinement_improvements
# improvements and i<max_steps and refinement_improvements:
#while i<max_steps:
while ms <= max_steps:
#print "pocnuva rafiniranjeto, dolzina %d" %i
self.refinedRefinementBeam(targetClass)
#self.printBeam(self.refinementCandidates,"Refinement candidates")
refinement_improvements = self.updateRefinementBeam(self.refinementCandidates)
#self.printBeam(self.RefinementBeam, name="Refinement beam")
#unionOfBeams = []; unionOfBeams.extend(self.RefinementBeam); unionOfBeams.extend(self.SelectionBeam)
#self.chooseSelectionCandidates(unionOfBeams)
#self.printBeam(self.selectionCandidates, name="Selection candidates")
#print "Pred update"
improvements = self.updateSelectionBeam(self.selectionCandidates)
#print "Posle update"
#m(self.SelectionBeam, "Selection beam")
ms=ms+1
beam = self.SelectionBeam
#self.printBeam(beam, "Final selection beam.")
if num_of_rules != 0:
beam = self.ruleSubsetSelection(beam, num_of_rules, data)
#self.printBeam(beam, "Posle SS")
self.SelectionBeam = beam
if data_discretized:
targetClassRule = SDRule(original_data, targetClass, conditions=[], g=self.g)
#targetClassRule = SDRule(original_data, targetClass, conditions=[], g=1, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)
# change beam so the rules apply to original data
#self.printBeam(self.SelectionBeam, "Pred diskretizacija")
self.SelectionBeam = [rule.getUndiscretized(original_data) for rule in self.SelectionBeam]
#self.printBeam(self.SelectionBeam, "Posle diskretizacija")
else:
targetClassRule = SDRule(data, targetClass, conditions=[], g =self.g)
#targetClassRule = SDRule(data, targetClass, conditions=[], g =1, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)
#print "Ready to return"
#self.printBeam(self.SelectionBeam, "Ova se vrakja")
rules = SDRules(self.SelectionBeam, targetClassRule, "SD-inverted")
#rules.printRules()
#print "*"*100
return rules
#return SDRules(self.SelectionBeam, targetClassRule, "SD-inverted")
def fillCandidatesList(self, data, targetClass):
#first initialize empty rule
rule = SDRule(data=data, targetClass=targetClass, g=self.g, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)
newRefinementBeam = {}
newSelectionBeam = {}
self.alredyRefinedRules[str(targetClass)].add(rule.orderedRuleToString())
for attr in data.domain.attributes:
value = attr.firstvalue()
while(value):
#print i
#i+=1
newRule = rule.cloneAndAddCondition(attr,value,rule,refinement_heuristics=self.refinement_heuristics,selection_heuristics=self.selection_heuristics)
newRule.filterAndStore(rule)
self.CandidatesList.add(newRule)
newRefinementBeam[newRule] = newRule.refinement_quality
newSelectionBeam[newRule] = newRule.selection_quality
value = attr.nextvalue(value)
no_candidates = len(self.CandidatesList)
#sort the rules according to their refinement qualities
sorted_newRefinementBeam = sorted(newRefinementBeam.items(), key=operator.itemgetter(1), reverse=True)
self.refinementCandidates = [i[0] for i in sorted_newRefinementBeam]
#sort the rules according to their selection quality
sorted_newSelectionBeam = sorted(newSelectionBeam.items(), key=operator.itemgetter(1), reverse=True)
l_sortedNewSelectionBeam = [i[0] for i in sorted_newSelectionBeam]
self.sortSelectionCandidates(l_sortedNewSelectionBeam)
#self.selectionCandidates = [i[0] for i in sorted_newSelectionBeam]
#self.refinementBeamWidth = min(no_candidates*10,500)
#self.refinementBeamWidth = 1
def chooseSelectionCandidates(self,beam):
newSelectionBeam = {}
for rule in beam:
newSelectionBeam[rule]=rule.selection_quality
sorted_newSelectionBeam = sorted(newSelectionBeam.items(), key=operator.itemgetter(1), reverse=True)
self.selectionCandidates = [i[0] for i in sorted_newSelectionBeam]
def resetDataWeights(self):
for d in self.weigted_data:
d.setweight(self.c,1)
d.setweight(self.count,0)
def updateWeights(self,rule,type=""):
#print rule
#print rule.ruleToString()
for d in self.weigted_data:
if type=="geometric":
if rule.covers(d):
weight = d.getweight(self.c)
weight = weight*self.weight_factor
d.setweight(self.c, weight)
#print "set weight: ", weight
#print "weight is set to: ", d.getweight(self.c)
#print "geometric: ", weight
continue
elif type=="harmonic":
if rule.covers(d):
count = d.getweight(self.count)
count = count + 1
d.setweight(self.c, 1.0/count)
d.setweight(self.count, count)
#print "weight ", d.getweight(self.c)
#print "count ", d.getweight(self.count)
#print "set weight: ", weight
#print "weight is set to: ", d.getweight(self.c)
#print "harmonic: ", weight
continue
else:
d.setweight(self.c, 0)
#pass
def replaceRefinementBeam(self, refinementCandidates):
empty_rule = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]
newRefinementBeam = {}
alreadyRefined = []
refinement_improvement = False
#for ref in self.RefinementBeam:
# if ref.complexity != 0:
#newRefinementBeam[ref] = ref.refinement_quality
# alreadyRefined.append(ref.orderedRuleToString())
for refinement in refinementCandidates:
#if the refinemet quality is smaller than the worst rule for refinement in RefinementBeam, ignore this rule and others that follow
#if refinement.refinement_quality < self.RefinementBeam[-1].refinement_quality:
# print "we are breaking"
# break
if (refinement.orderedRuleToString() not in alreadyRefined):
#otherwise insert the refinement into the right position in newRefinementBeam
newRefinementBeam[refinement] = refinement.refinement_quality
alreadyRefined.append(refinement.orderedRuleToString())
sorted_newRefinementBeam = sorted(newRefinementBeam.items(), key=operator.itemgetter(1), reverse=True)
self.refinementCandidates = [i[0] for i in sorted_newRefinementBeam]
if len(self.refinementCandidates) > self.refinementBeamWidth:
#the updated RefinementBeam should consist only of refinementBeamWidth elements
self.RefinementBeam = self.refinementCandidates[:self.refinementBeamWidth]
else:
self.RefinementBeam = self.refinementCandidates + empty_rule*(self.refinementBeamWidth-len(self.refinementCandidates))
for i in range(min(len(self.refinementCandidates), self.refinementBeamWidth)):
if self.refinementCandidates[i].orderedRuleToString() not in alreadyRefined:
refinement_improvement = True
return refinement_improvement
def initializeRefinementBeam(self):
self.RefinementBeam = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]*self.refinementBeamWidth
def initializeSelectionBeam(self):
self.SelectionBeam = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g,refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]*self.selectionBeamWidth
def updateRefinementBeam(self, refinementCandidates):
empty_rule = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]
newRefinementBeam = {}
alreadyRefined = []
refinement_improvement = False
for ref in self.RefinementBeam:
if ref.complexity != 0:
#newRefinementBeam[ref] = ref.refinement_quality
alreadyRefined.append(ref.orderedRuleToString())
for refinement in refinementCandidates:
#if the refinemet quality is smaller than the worst rule for refinement in RefinementBeam, ignore this rule and others that follow
#if refinement.refinement_quality < self.RefinementBeam[-1].refinement_quality:
# print "we are breaking"
# break
if (refinement.orderedRuleToString() not in alreadyRefined):
#otherwise insert the refinement into the right position in newRefinementBeam
newRefinementBeam[refinement] = refinement.refinement_quality
alreadyRefined.append(ref.orderedRuleToString())
sorted_newRefinementBeam = sorted(newRefinementBeam.items(), key=operator.itemgetter(1), reverse=True)
self.refinementCandidates = [i[0] for i in sorted_newRefinementBeam]
if len(self.refinementCandidates) > self.refinementBeamWidth:
#the updated RefinementBeam should consist only of refinementBeamWidth elements
self.RefinementBeam = self.refinementCandidates[:self.refinementBeamWidth]
else:
self.RefinementBeam = self.refinementCandidates + empty_rule*(self.refinementBeamWidth-len(self.refinementCandidates))
"""
for i in range(min(len(self.refinementCandidates), self.refinementBeamWidth)):
if self.refinementCandidates[i].orderedRuleToString() not in alreadyRefined:
refinement_improvement = True
"""
return refinement_improvement
"""
print "*-"*50
print "\n"
print "Rule to be refined: ", self.RefinementBeam[0].ruleToString()
print "\n"
print "*-"*50
"""
def printBeam(self, beam, name):
print "#"*100
print "\n %s \n" %(name)
for rule in beam:
print "SQ: %.3f\tRQ: %.3f\tTP: %d\tFP: %d\t%s" %(rule.selection_quality,rule.refinement_quality,len(rule.TP),len(rule.FP),rule.ruleToString())
print "*"*100
def updateSelectionBeam(self, selectionCandidates):
empty_rule = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g, \
refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]
newSelectionBeam = list()
#newSelectionBeam = {}
changes = False
alreadySelected = list()
self.resetDataWeights()
candidates = list()
for c in self.SelectionBeam:
if c.complexity != 0:
if c.orderedRuleToString() not in alreadySelected:
candidates.append(c)
alreadySelected.append(c.orderedRuleToString())
for c in selectionCandidates:
if c.complexity != 0:
if c.orderedRuleToString() not in alreadySelected:
candidates.append(c)
alreadySelected.append(c.orderedRuleToString())
for i in range(self.selectionBeamWidth):
if candidates==[]:
break
bestRule = self.selectBestRule(candidates)
#print "best rule: ", bestRule.ruleToString()
#print "TP: ", bestRule.TPlen
#print "FP: ", bestRule.FPlen
#print "pred updateni"
self.updateWeights(rule=bestRule, type=self.weights_type)
#print "posle updatani"
#print "Weights are updated"
candidates.remove(bestRule)
newSelectionBeam.append(bestRule)
if len(newSelectionBeam) < self.selectionBeamWidth:
self.SelectionBeam = newSelectionBeam + empty_rule*(self.selectionBeamWidth-len(newSelectionBeam))
else:
self.SelectionBeam = newSelectionBeam
"""
#print "New selection beam."
for sel in self.SelectionBeam:
if sel.complexity != 0:
newSelectionBeam[sel] = sel.selection_quality
alreadySelected.append(sel.orderedRuleToString())
self.alreadySelectedRules.add(sel.orderedRuleToString())
for selection in selectionCandidates:
#print selection.orderedRuleToString()
#if the selection quality is smaller than the worst rule for selection in SelectionBeam, ignore this rule and others that follow
if selection.selection_quality < self.SelectionBeam[-1].selection_quality:
break
if (selection.orderedRuleToString() not in alreadySelected) and (selection.orderedRuleToString() not in self.alreadySelectedRules):
#otherwise insert the refinement into the right position in newRefinementBeam
newSelectionBeam[selection] = selection.selection_quality
alreadySelected.append(selection.orderedRuleToString())
#self.alreadySelectedRules.add(selection.orderedRuleToString())
changes = True
#else:
# print selection.orderedRuleToString(), ' is in SelectionBeam.'
#print "*"*39
sorted_newSelectionBeam = sorted(newSelectionBeam.items(), key=operator.itemgetter(1), reverse=True)
self.selectionCandidates = [i[0] for i in sorted_newSelectionBeam]
if len(self.selectionCandidates) > self.selectionBeamWidth:
#the updated SelectionBeam should consist only of selectionBeamWidth elements
self.sortSelectionCandidates(self.selectionCandidates)
#self.selectRelevantCandidates()
self.SelectionBeam = self.selectionCandidates[:self.selectionBeamWidth]
#self.SelectionBeam = []
#for sel in self.selectionCandidates:
# if
else:
self.SelectionBeam = self.selectionCandidates + empty_rule*(self.selectionBeamWidth-len(self.selectionCandidates))
#print len(self.SelectionBeam)
return changes
empty_rule = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g, refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]
newSelectionBeam = {}
changes = False
alreadySelected = []
alreadyAdded = []
#print "New selection beam."
for sel in self.SelectionBeam:
if sel.complexity != 0:
#newSelectionBeam[sel] = sel.selection_quality
alreadySelected.append(sel.orderedRuleToString())
alreadyAdded.append(sel)
for selection in selectionCandidates:
#print selection.orderedRuleToString()
#if the selection quality is smaller than the worst rule for selection in SelectionBeam, ignore this rule and others that follow
if selection.selection_quality < self.SelectionBeam[-1].selection_quality:
break
if (selection.orderedRuleToString() not in alreadySelected):
#otherwise insert the refinement into the right position in newRefinementBeam
if len(alreadyAdded)==0:
#newSelectionBeam[selection] = selection.selection_quality
alreadySelected.append(selection.orderedRuleToString())
alreadyAdded.append(selection)
changes = True
else:
if selection.selection_quality == alreadyAdded[-1].selection_quality:
#print "two rules with same quality"
if len(selection.filter.conditions) < len(alreadyAdded[-1].filter.conditions):
#print selection.conditions
#print "we change now"
#self.printBeam(alreadyAdded,name="already added before removing")
alreadyAdded.remove(alreadyAdded[-1])
#self.printBeam(alreadyAdded,name="already added after removing")
alreadySelected.append(selection.orderedRuleToString())
alreadyAdded.append(selection)
#self.printBeam(alreadyAdded,name="already added after adding")
changes = True
else:
alreadySelected.append(selection.orderedRuleToString())
alreadyAdded.append(selection)
changes = True
for s in alreadyAdded:
newSelectionBeam[s] = s.selection_quality
#else:
# print selection.orderedRuleToString(), ' is in SelectionBeam.'
#print "*"*39
sorted_newSelectionBeam = sorted(newSelectionBeam.items(), key=operator.itemgetter(1), reverse=True)
self.selectionCandidates = [i[0] for i in sorted_newSelectionBeam]
if len(self.selectionCandidates) > self.selectionBeamWidth:
#the updated SelectionBeam should consist only of selectionBeamWidth elements
self.SelectionBeam = self.selectionCandidates[:self.selectionBeamWidth]
else:
self.SelectionBeam = self.selectionCandidates + empty_rule*(self.selectionBeamWidth-len(self.selectionCandidates))
#print len(self.SelectionBeam)
return changes
"""
"""
tempSelectionBeam = []
for i in range(len(self.selectionCandidates)-1):
temp_i = i
if sorted_newSelectionBeam[self.selectionCandidates[i]]==sorted_newSelectionBeam[self.selectionCandidates[i+1]]:
temp = {}
temp[self.selectionCandidates[i]]=len(self.selectionCandidates[i].conditions)
for j in range(i+1,len(self.selectionCandidates)-1):
if sorted_newSelectionBeam[self.selectionCandidates[i]]==sorted_newSelectionBeam[self.selectionCandidates[j]]:
temp[self.selectionCandidates[j]]=len(self.selectionCandidates[i].conditions)
else:
temp_i = j
break
sorted_temp = sorted(temp.items(), key=operator.itemgetter(1), reverse=False)
tempSelectionBeam.append(sorted_temp[0])
else:
tempSelectionBeam.append(self.selectionCandidates[i])
if len(tempSelectionBeam) > self.selectionBeamWidth:
#the updated SelectionBeam should consist only of selectionBeamWidth elements
self.SelectionBeam = tempSelectionBeam[:self.selectionBeamWidth]
else:
self.SelectionBeam = tempSelectionBeam + empty_rule*(self.selectionBeamWidth-len(self.selectionCandidates))
print "*-"*50
print "\n"
print "Rule to be selected: ", self.SelectionBeam[0].ruleToString()
print "\n"
print "*-"*50
"""
#return changes
def selectBestRule(self,candidates):
bestRule = []
alreadyChecked = list()
if len(candidates)>0:
bestRule = candidates[0]
bestRule.setWeightedData(self.weigted_data); bestRule.calculateWeightedTP(self.c); #bestRule.calculateWeightedFP(self.c);
bestRule.calculateWeightedSelectionQuality()
bestQuality=bestRule.weighted_selection_quality
alreadyChecked.append(bestRule.orderedRuleToString())
for i in range(1,len(candidates)):
rule = candidates[i]
rule.setWeightedData(self.weigted_data); rule.calculateWeightedTP(self.c); #rule.calculateWeightedFP(self.c);
rule.calculateWeightedSelectionQuality()
ruleQuality=rule.weighted_selection_quality
if rule.orderedRuleToString() not in alreadyChecked:
if ruleQuality > bestQuality:
bestQuality = ruleQuality
bestRule = rule
elif ruleQuality == bestQuality:
if rule.TPlen > bestRule.TPlen:
bestQuality = ruleQuality
bestRule = rule
elif rule.TPlen == bestRule.TPlen:
if rule.complexity < bestRule.complexity:
bestQuality = ruleQuality
bestRule = rule
alreadyChecked.append(rule.orderedRuleToString())
return bestRule
def selectRelevantCandidates(self):
empty_rule = [SDRule(data=self.data, targetClass=self.targetClass, g=self.g, \
refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)]
i=1
newSelectionBeam = [self.selectionCandidates[0]]
for j in range(1,len(self.selectionCandidates)):
candidate = self.selectionCandidates[j]
if self.isRelevant(candidate, newSelectionBeam):
#print "Relevant rule ", candidate.ruleToString()
i = i+1
newSelectionBeam.append(candidate)
#else:
#print "Irrelevant rule ", candidate.ruleToString()
#if i == self.selectionBeamWidth:
# break
if len(newSelectionBeam) > self.selectionBeamWidth:
self.SelectionBeam = newSelectionBeam[:self.selectionBeamWidth]
else:
self.SelectionBeam = newSelectionBeam + empty_rule*(self.selectionBeamWidth-len(newSelectionBeam))
#self.printBeam(self.SelectionBeam, "Updated selection beam")
def sortSelectionCandidates(self, selectionCandidates):
#print "selection candidates", len(selectionCandidates)
sortedSelectionCandidates = []
for i in range(len(selectionCandidates)-1):
#candidate = selectionCandidates[i]
#ref_candidate = selectionCandidates[i+1]
#if candidate.selection_quality == ref_candidate.selection_quality:
temps = {}
for j in range(i+1,len(selectionCandidates)):
if selectionCandidates[j]==selectionCandidates[j-1]:
temps[j-1] = selectionCandidates[j-1].TP
temps[j] = selectionCandidates[j-1].TP
else:
temps[j-1] = selectionCandidates[j-1].TP
i = j
break
sorted_temps = sorted(temps.items(), key=operator.itemgetter(1), reverse=True)
l_sorted_temps = [k[0] for k in sorted_temps]
for st in l_sorted_temps:
sortedSelectionCandidates.append(selectionCandidates[st])
if len(sortedSelectionCandidates)<len(selectionCandidates):
sortedSelectionCandidates.append(selectionCandidates[-1])
"""
else:
sortedSelectionCandidates.append(selectionCandidates[i])
if selectionCandidates
"""
#print "Sorted selection candidates ", len(sortedSelectionCandidates)
self.selectionCandidates = sortedSelectionCandidates
def isRelevant(self, newRule, beam):
for rule in beam:
if newRule.isIrrelevant(rule):
return false
return true
def refinedRefinementBeam(self, targetClass):
min_sup = self.minSupport
newRefinementCandidates = {}
newSelectionCandidates = {}
for refinement in self.RefinementBeam:
if refinement.orderedRuleToString() not in self.alredyRefinedRules[str(targetClass)] and refinement.complexity !=0:
attributes = refinement.conditions()
for attr in self.data.domain.attributes:
if attr.name not in attributes:
value = value = attr.firstvalue()
while value:
newRule = refinement.cloneAndAddCondition(attr,value,refinement,refinement_heuristics=self.refinement_heuristics, selection_heuristics=self.selection_heuristics)
newRule.filterAndStore(refinement)
#print newRule.ruleToString()
#print newRule.support
if newRule.support > min_sup:
#if (len(newRule.TP) != len(refinement.TP)) and (len(newRule.FP) != len(refinement.FP)):
if len(newRule.FP) < len(refinement.FP):
newRefinementCandidates[newRule]=newRule.refinement_quality
newSelectionCandidates[newRule]=newRule.selection_quality
self.alredyRefinedRules[str(targetClass)].add(refinement.orderedRuleToString())
value = attr.nextvalue(value)
sorted_newRefinementCandidates = sorted(newRefinementCandidates.items(), key=operator.itemgetter(1), reverse=True)
sorted_newSelectionCandidates = sorted(newSelectionCandidates.items(), key=operator.itemgetter(1), reverse=True)
self.refinementCandidates = [i[0] for i in sorted_newRefinementCandidates]
l_sortedSelectionCandidates = [i[0] for i in sorted_newSelectionCandidates]
self.sortSelectionCandidates(l_sortedSelectionCandidates)
#self.selectionCandidates = [i[0] for i in sorted_newSelectionCandidates]
"""
if len(self.refinementCandidates)!=0:
print "#"*100
print self.refinementCandidates[0].ruleToString()
print "Best refinement: P %d\tN %d\tp %d\tn %d\tRQ %.3f" %(self.refinementCandidates[0].P,self.refinementCandidates[0].N,len(self.refinementCandidates[0].TP),len(self.refinementCandidates[0].FP), self.refinementCandidates[0].refinement_quality)
print "\n\n"
"""
"""
print self.selectionCandidates[0].ruleToString()
print "Best selection: P %d\tN %d\tp %d\tn %d\tSQ %.3f" %(self.selectionCandidates[0].P,self.selectionCandidates[0].N,len(self.selectionCandidates[0].TP),len(self.selectionCandidates[0].FP), self.selectionCandidates[0].selection_quality)
print "\n\n"
"""
"""
print "Candidates for refinement: \n"
for rule in self.refinementCandidates:
print "N: %d\t\tTP: %d\t\t\tFP: %d\t\tRule:\t%s" %(len(rule.TP)+len(rule.FP),len(rule.TP), len(rule.FP), rule.ruleToString())
print "#"*80
print "\n\n"
print "Candidates for selection: \n"
for rule in self.selectionCandidates:
print "N: %d\t\tTP: %d\t\t\tFP: %d\t\tRule:\t%s" %(len(rule.TP)+len(rule.FP),len(rule.TP), len(rule.FP), rule.ruleToString())
print "#"*80
"""
def betterThanWorstRule(self, newRule, beam, worstRuleIndex):
if newRule.quality2 > beam[worstRuleIndex].quality2: # better quality
return true
elif newRule.quality2 == beam[worstRuleIndex].quality2 and newRule.complexity < beam[worstRuleIndex].complexity: # same quality and smaller complexity
return true
else:
return false
def replaceWorstRule(self, rule, beam, worstRuleIndex):
beam[worstRuleIndex] = rule
wri = 0
for i in range(len(beam)):
if beam[i].quality2 < beam[wri].quality2:
wri = i
return wri
def dataOK(self, data):
if data.domain.classVar.varType != orange.VarTypes.Discrete:
print "Target Variable must be discrete"%(attr.name)
return false
return true
def ruleSubsetSelection(self, beam, num_of_rules, data):
#print "RSS"
#self.printBeam(beam, "RSS")
SS = []
c = orange.newmetaid()
data.addMetaAttribute(c) #initialize to 1
if num_of_rules <= len(beam):
for i in range(num_of_rules):
best_score = 0
best_rule_index = 0
for i in range(len(beam)):
score = 0
for d in data: # calculate sum of weights of examples
if beam[i].filter(d):
score += 1.0/d.getweight(c)
if score>best_score:
best_score = score
best_rule_index = i
for d in data: # increase exampe counter
if beam[best_rule_index].filter(d):
d.setweight(c, d.getweight(c)+1)
SS.append(beam[best_rule_index])
del beam[best_rule_index]
data.removeMetaAttribute(c)
else:
return beam
return SS
def writeResults(self,file_name):
current_directory = os.path.dirname(os.path.realpath(__file__)) + r"/results"
print current_directory
#___________________________________________________________________________________
if __name__=="__main__":
dataset_directory = current_directory = os.path.dirname(os.path.realpath(__file__))+r"/20_DATASETS_TAB/"
dataset = "contact-lenses.tab"
filename = os.path.join(dataset_directory,dataset)
data = orange.ExampleTable(filename)
print
learner = DoubleBeam_weights(weight_factor = 0.9,minSupport=0.01, beam_width=5, refinement_heuristics = "Inverted precision", selection_heuristics="Precision", type="harmonic")
"""
for targetClass in data.domain.classVar.values:
#targetClass= orange.Value(data.domain.classVar, "hard")
print targetClass
rules = learner(data, targetClass=targetClass, num_of_rules=5)
rules.printRules()
"""
rules = learner(data, targetClass="hard", num_of_rules=0)
learner.printBeam(learner.SelectionBeam, "SB")
rules.printRules()