| Line | |
|---|
| 1 | import Orange |
|---|
| 2 | import re |
|---|
| 3 | |
|---|
| 4 | data = Orange.data.Table("iris") |
|---|
| 5 | tree = Orange.classification.tree.TreeLearner(data, max_depth=3) |
|---|
| 6 | |
|---|
| 7 | def 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 | |
|---|
| 14 | def 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 | |
|---|
| 30 | my_format = [(re.compile("%"+Orange.classification.tree.fs |
|---|
| 31 | +"B"+Orange.classification.tree.by), replaceB)] |
|---|
| 32 | |
|---|
| 33 | print tree.dump(leaf_str="%V %^B% (%^3.2BbP%)", user_formats=my_format) |
|---|
Note: See
TracBrowser
for help on using the repository browser.