source: orange/docs/reference/rst/code/simple_tree_random_forest.py @ 9823:7f9c3f3c6474

Revision 9823:7f9c3f3c6474, 1.5 KB checked in by lanumek, 2 years ago (diff)

Changed names of data sets (table replaced with data or name of the data set).

Line 
1import Orange
2import time
3
4class SimpleTreeLearnerSetProb():
5    """
6    Orange.classification.tree.SimpleTreeLearner which sets the skip_prob
7    so that on average a square root of the attributes will be
8    randomly choosen for each split.
9    """
10    def __init__(self, wrapped):
11        self.wrapped = wrapped
12
13    def __call__(self, examples, weight=0):
14        self.wrapped.skip_prob = 1-len(examples.domain.attributes)**0.5/len(examples.domain.attributes)
15        return self.wrapped(examples)
16
17#ordinary random forests
18tree = Orange.classification.tree.TreeLearner(min_instances=5, measure="gainRatio")
19rf_def = Orange.ensemble.forest.RandomForestLearner(trees=50, base_learner=tree, name="for_gain")
20
21#random forests with simple trees - simple trees do random attribute selection by themselves
22st = Orange.classification.tree.SimpleTreeLearner(min_instances=5)
23stp = SimpleTreeLearnerSetProb(st)
24rf_simple = Orange.ensemble.forest.RandomForestLearner(learner=stp, trees=50, name="for_simp")
25
26learners = [ rf_def, rf_simple ]
27
28iris = Orange.data.Table("iris")
29results = Orange.evaluation.testing.cross_validation(learners, iris, folds=3)
30print "Learner  CA     Brier  AUC"
31for i in range(len(learners)):
32    print "%-8s %5.3f  %5.3f  %5.3f" % (learners[i].name, \
33    Orange.evaluation.scoring.CA(results)[i], 
34    Orange.evaluation.scoring.Brier_score(results)[i],
35    Orange.evaluation.scoring.AUC(results)[i])
36
37print 
38
39print "Runtimes:"
40for l in learners:
41    t = time.time()
42    l(iris)
43    print l.name, time.time() - t
Note: See TracBrowser for help on using the repository browser.