source: orange/docs/reference/rst/code/orngTree2.py @ 9800:6d3897211a6c

Revision 9800:6d3897211a6c, 1.0 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Renamed format to to_string.

Line 
1import Orange
2import re
3
4data = Orange.data.Table("iris")
5tree = Orange.classification.tree.TreeLearner(data, max_depth=3)
6
7def get_margin(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 = get_margin(node.distribution)
16
17    by = mo.group("by")
18    if margin and by:
19        whom = Orange.classification.tree.by_whom(by, parent, tree)
20        if whom and whom.distribution:
21            div_margin = get_margin(whom.distribution)
22            if div_margin > 1e-30:
23                margin /= div_margin
24            else:
25                Orange.classification.tree.insert_dot(strg, mo)
26        else:
27            return Orange.classification.tree.insert_dot(strg, mo)
28    return Orange.classification.tree.insert_num(strg, mo, margin)
29
30my_format = [(re.compile("%" + Orange.classification.tree.fs
31    + "B" + Orange.classification.tree.by), replaceB)]
32
33print tree.to_string(leaf_str="%V %^B% (%^3.2BbP%)", user_formats=my_format)
Note: See TracBrowser for help on using the repository browser.