Orange Forum • View topic - Bagging, Boosting, attributes and C++

Bagging, Boosting, attributes and C++

Report bugs (or imagined bugs).
(Archived/read-only, please use our ticketing system for reporting bugs and their discussion.)
Forum rules
Archived/read-only, please use our ticketing system for reporting bugs and their discussion.

Bagging, Boosting, attributes and C++

Postby Tobu » Tue Jan 06, 2009 11:36

Bagging and boosting won't work out of the box due to putting attributes of a Classifier subclass in __dict__, where C++ code can't see them. Also, classvar was used instead of ClassVar.

Here is a transcript: http://ore.mines.edu/phpBB/viewtopic.php?t=3743

And a patch:
Code: Select all
diff --git a/orange/orngEnsemble.py b/orange/orngEnsemble.py
index 0e38867..1b669d1 100644
--- a/orange/orngEnsemble.py
+++ b/orange/orngEnsemble.py
@@ -50,7 +50,7 @@ class BoostedLearnerClass(orange.Learner):
                 if epsilon >= 0.499 and len(classifiers)>1:
                     del classifiers[-1]
                 instances.removeMetaAttribute(weight)
-                return BoostedClassifier(classifiers = classifiers, name=self.name, classvar=instances.domain.classVar)
+                return BoostedClassifier(classifiers = classifiers, name=self.name, classVar=instances.domain.classVar)
             beta = epsilon/(1-epsilon)
             for e in range(n):
                 if corr[e]:
@@ -60,18 +60,19 @@ class BoostedLearnerClass(orange.Learner):
                 instances[e].setweight(weight, instances[e].getweight(weight)*f)
 
         instances.removeMetaAttribute(weight)
-        return BoostedClassifier(classifiers = classifiers, name=self.name, classvar=instances.domain.classVar)
+        return BoostedClassifier(classifiers = classifiers, name=self.name, classVar=instances.domain.classVar)
 
 class BoostedClassifier(orange.Classifier):
     def __init__(self, **kwds):
-        self.__dict__ = kwds
+        for k, v in kwds.iteritems():
+            setattr(self, k, v)
 
     def __call__(self, example, resultType = orange.GetValue):
-        votes = [0.] * len(self.classvar.values)
+        votes = [0.] * len(self.classVar.values)
         for c, e in self.classifiers:
             votes[int(c(example))] += e
         index = orngMisc.selectBestIndex(votes)
-        value = orange.Value(self.classvar, index)
+        value = orange.Value(self.classVar, index)
         if resultType == orange.GetValue:
             return value
         sv = sum(votes)
@@ -111,18 +112,19 @@ class BaggedLearnerClass(orange.Learner):
             examples = orange.ExampleTable(examples)
             data = examples.getitems(selection)
             classifiers.append(self.learner(data, weight))
-        return BaggedClassifier(classifiers = classifiers, name=self.name, classvar=examples.domain.classVar)
+        return BaggedClassifier(classifiers = classifiers, name=self.name, classVar=examples.domain.classVar)
 
 class BaggedClassifier(orange.Classifier):
     def __init__(self, **kwds):
-        self.__dict__ = kwds
+        for k, v in kwds.iteritems():
+            setattr(self, k, v)
 
     def __call__(self, example, resultType = orange.GetValue):
-        freq = [0.] * len(self.classvar.values)
+        freq = [0.] * len(self.classVar.values)
         for c in self.classifiers:
             freq[int(c(example))] += 1
         index = freq.index(max(freq))
-        value = orange.Value(self.classvar, index)
+        value = orange.Value(self.classVar, index)
         if resultType == orange.GetValue:
             return value
         for i in range(len(freq)):
@@ -209,7 +211,8 @@ class RandomForestLearner(orange.Learner):
         
 class RandomForestClassifier(orange.Classifier):
     def __init__(self, **kwds):
-        self.__dict__.update(kwds)
+        for k, v in kwds.iteritems():
+            setattr(self, k, v)
 
     def __call__(self, example, resultType = orange.GetValue):
         from operator import add


Maybe the use of getattr/setattr vs __dict__ should be documented in peculiarities.htm .

Return to Bugs