Changeset 7664:f7178102630c in orange
 Timestamp:
 02/14/11 12:16:11 (3 years ago)
 Branch:
 default
 Convert:
 440401cd5c46ca84e6be9d7fe12513d1e1266ac3
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/ensemble/forest.py
r7660 r7664 64 64 self.randstate = self.rand.getstate() #original state 65 65 66 if not learner:67 # tree learner assembled as suggested by Brieman (2001)68 smallTreeLearner = Orange.classification.tree.TreeLearner(69 storeNodeClassifier = 0, storeContingencies=0,70 storeDistributions=1, minExamples=5).instance()71 smallTreeLearner.split.discreteSplitConstructor.measure = \72 smallTreeLearner.split.continuousSplitConstructor.measure =\73 Orange.feature.scoring.Gini()74 smallTreeLearner.split = SplitConstructor_AttributeSubset(\75 smallTreeLearner.split, attributes, self.rand)76 self.learner = smallTreeLearner77 78 66 def __call__(self, instances, weight=0): 79 67 """ … … 86 74 :rtype: :class:`Orange.ensemble.forest.RandomForestClassifier` 87 75 """ 76 77 78 # If there is no learner we create our own 79 80 if not self.learner: 81 82 # tree learner assembled as suggested by Brieman (2001) 83 smallTreeLearner = Orange.classification.tree.TreeLearner( 84 storeNodeClassifier = 0, storeContingencies=0, 85 storeDistributions=1, minExamples=5).instance() 86 87 # Use MSE on continuous class and Gini on discreete 88 if instances.domain.class_var.var_type == Orange.data.variable.Continuous.Continuous: 89 smallTreeLearner.split.discreteSplitConstructor.measure = \ 90 smallTreeLearner.split.continuousSplitConstructor.measure =\ 91 Orange.feature.scoring.MSE() 92 else: 93 smallTreeLearner.split.discreteSplitConstructor.measure = \ 94 smallTreeLearner.split.continuousSplitConstructor.measure =\ 95 Orange.feature.scoring.Gini() 96 97 smallTreeLearner.split = SplitConstructor_AttributeSubset(\ 98 smallTreeLearner.split, self.attributes, self.rand) 99 self.learner = smallTreeLearner 100 88 101 # if number of features for subset is not set, use square root 89 102 if hasattr(self.learner.split, 'attributes') and\ … … 156 169 """ 157 170 from operator import add 158 159 # voting for class probabilities 160 if resultType == orange.GetProbabilities or resultType == orange.GetBoth: 161 cprob = [0.] * len(self.domain.classVar.values) 162 for c in self.classifiers: 163 a = [x for x in c(instance, orange.GetProbabilities)] 164 cprob = map(add, cprob, a) 165 norm = sum(cprob) 166 for i in range(len(cprob)): 167 cprob[i] = cprob[i]/norm 168 169 # voting for crisp class membership, notice that 170 # this may not be the same class as one obtaining the 171 # highest probability through probability voting 172 if resultType == orange.GetValue or resultType == orange.GetBoth: 173 cfreq = [0] * len(self.domain.classVar.values) 174 for c in self.classifiers: 175 cfreq[int(c(instance))] += 1 176 index = cfreq.index(max(cfreq)) 177 cvalue = Orange.data.Value(self.domain.classVar, index) 178 179 if resultType == orange.GetValue: return cvalue 180 elif resultType == orange.GetProbabilities: return cprob 181 else: return (cvalue, cprob) 171 172 # handle discreete class 173 174 if self.class_var.var_type == Orange.data.variable.Discrete.Discrete: 175 176 # voting for class probabilities 177 if resultType == orange.GetProbabilities or resultType == orange.GetBoth: 178 prob = [0.] * len(self.domain.classVar.values) 179 for c in self.classifiers: 180 a = [x for x in c(instance, orange.GetProbabilities)] 181 prob = map(add, prob, a) 182 norm = sum(prob) 183 cprob = Orange.statistics.distributions.Discrete(self.classVar) 184 for i in range(len(prob)): 185 cprob[i] = prob[i]/norm 186 187 188 189 # voting for crisp class membership, notice that 190 # this may not be the same class as one obtaining the 191 # highest probability through probability voting 192 if resultType == orange.GetValue or resultType == orange.GetBoth: 193 cfreq = [0] * len(self.domain.classVar.values) 194 for c in self.classifiers: 195 cfreq[int(c(instance))] += 1 196 index = cfreq.index(max(cfreq)) 197 cvalue = Orange.data.Value(self.domain.classVar, index) 198 199 if resultType == orange.GetValue: return cvalue 200 elif resultType == orange.GetProbabilities: return cprob 201 else: return (cvalue, cprob) 202 203 else: 204 # Handle continuous class 205 206 # voting for class probabilities 207 if resultType == orange.GetProbabilities or resultType == orange.GetBoth: 208 probs = [c(instance, orange.GetProbabilities) for c in self.classifiers] 209 cprob = dict() 210 for prob in probs: 211 a = dict(prob.items()) 212 cprob = dict( (n, a.get(n, 0)+cprob.get(n, 0)) for n in set(a)set(cprob) ) 213 cprob = Orange.statistics.distributions.Continuous(cprob) 214 cprob.normalize() 215 216 # gather average class value 217 if resultType == orange.GetValue or resultType == orange.GetBoth: 218 values = [c(instance).value for c in self.classifiers] 219 cvalue = Orange.data.Value(self.domain.classVar, sum(values) / len(self.classifiers)) 220 221 if resultType == orange.GetValue: return cvalue 222 elif resultType == orange.GetProbabilities: return cprob 223 else: return (cvalue, cprob) 182 224 183 225 ### MeasureAttribute_randomForests
Note: See TracChangeset
for help on using the changeset viewer.