source: orange/orange/doc/Orange/rst/code/orngTree2.py @ 7474:6bb9b5b70491

Revision 7474:6bb9b5b70491, 1.0 KB checked in by markotoplak, 3 years ago (diff)

Examples for the Orange.classification.tree updated so that they use the
module.

Line 
1import Orange
2import re
3
4data = Orange.data.Table("iris")
5tree = Orange.classification.tree.TreeLearner(data, maxDepth=3)
6
7def getMargin(dist):
8    if dist.abs < 1e-30:
9        return 0
10    l = list(dist)
11    l.sort()
12    return (l[-1] - l[-2]) / dist.abs
13
14def replaceB(strg, mo, node, parent, tree):
15    margin = getMargin(node.distribution)
16
17    by = mo.group("by")
18    if margin and by:
19        whom = Orange.classification.tree.byWhom(by, parent, tree)
20        if whom and whom.distribution:
21            divMargin = getMargin(whom.distribution)
22            if divMargin > 1e-30:
23                margin /= divMargin
24            else:
25                Orange.classification.tree.insertDot(strg, mo)
26        else:
27            return Orange.classification.tree.insertDot(strg, mo)
28    return Orange.classification.tree.insertNum(strg, mo, margin)
29   
30myFormat = [(re.compile("%"+Orange.classification.tree.fs
31    +"B"+Orange.classification.tree.by), replaceB)]
32           
33Orange.classification.tree.printTree(tree, leafStr="%V %^B% (%^3.2BbP%)", userFormats=myFormat)
Note: See TracBrowser for help on using the repository browser.