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

Revision 9823:7f9c3f3c6474, 1.0 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 re
3
4iris = Orange.data.Table("iris")
5tree = Orange.classification.tree.TreeLearner(iris, 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.