|
| 1 | +"""Tree learner widget""" |
| 2 | + |
| 3 | +from AnyQt.QtCore import Qt |
| 4 | +from collections import OrderedDict |
| 5 | + |
| 6 | +from Orange.data import Table |
| 7 | +from Orange.modelling.tree import TreeLearner |
| 8 | +from Orange.widgets import gui |
| 9 | +from Orange.widgets.settings import Setting |
| 10 | +from Orange.widgets.utils.owlearnerwidget import OWBaseLearner |
| 11 | + |
| 12 | + |
| 13 | +class OWTreeLearner(OWBaseLearner): |
| 14 | + """Tree algorithm with forward pruning.""" |
| 15 | + name = "Tree" |
| 16 | + description = "A tree algorithm with forward pruning." |
| 17 | + icon = "icons/Tree.svg" |
| 18 | + priority = 30 |
| 19 | + |
| 20 | + LEARNER = TreeLearner |
| 21 | + |
| 22 | + binary_trees = Setting(True) |
| 23 | + limit_min_leaf = Setting(True) |
| 24 | + min_leaf = Setting(2) |
| 25 | + limit_min_internal = Setting(True) |
| 26 | + min_internal = Setting(5) |
| 27 | + limit_depth = Setting(True) |
| 28 | + max_depth = Setting(100) |
| 29 | + |
| 30 | + # Classification only settings |
| 31 | + limit_majority = Setting(True) |
| 32 | + sufficient_majority = Setting(95) |
| 33 | + |
| 34 | + spin_boxes = ( |
| 35 | + ("Min. number of instances in leaves: ", |
| 36 | + "limit_min_leaf", "min_leaf", 1, 1000), |
| 37 | + ("Do not split subsets smaller than: ", |
| 38 | + "limit_min_internal", "min_internal", 1, 1000), |
| 39 | + ("Limit the maximal tree depth to: ", |
| 40 | + "limit_depth", "max_depth", 1, 1000)) |
| 41 | + |
| 42 | + classification_spin_boxes = ( |
| 43 | + ("Stop when majority reaches [%]: ", |
| 44 | + "limit_majority", "sufficient_majority", 51, 100),) |
| 45 | + |
| 46 | + def add_main_layout(self): |
| 47 | + box = gui.widgetBox(self.controlArea, 'Parameters') |
| 48 | + # the checkbox is put into vBox for alignemnt with other checkboxes |
| 49 | + gui.checkBox(gui.vBox(box), self, "binary_trees", "Induce binary tree", |
| 50 | + callback=self.settings_changed) |
| 51 | + for label, check, setting, fromv, tov in self.spin_boxes: |
| 52 | + gui.spin(box, self, setting, fromv, tov, label=label, |
| 53 | + checked=check, alignment=Qt.AlignRight, |
| 54 | + callback=self.settings_changed, |
| 55 | + checkCallback=self.settings_changed, controlWidth=80) |
| 56 | + |
| 57 | + def add_classification_layout(self, box): |
| 58 | + for label, check, setting, minv, maxv in self.classification_spin_boxes: |
| 59 | + gui.spin(box, self, setting, minv, maxv, |
| 60 | + label=label, checked=check, alignment=Qt.AlignRight, |
| 61 | + callback=self.settings_changed, controlWidth=80, |
| 62 | + checkCallback=self.settings_changed) |
| 63 | + |
| 64 | + def learner_kwargs(self): |
| 65 | + # Pylint doesn't get our Settings |
| 66 | + # pylint: disable=invalid-sequence-index |
| 67 | + return dict( |
| 68 | + max_depth=(None, self.max_depth)[self.limit_depth], |
| 69 | + min_samples_split=(2, self.min_internal)[self.limit_min_internal], |
| 70 | + min_samples_leaf=(1, self.min_leaf)[self.limit_min_leaf], |
| 71 | + binarize=self.binary_trees, |
| 72 | + preprocessors=self.preprocessors, |
| 73 | + sufficient_majority=(1, self.sufficient_majority / 100)[ |
| 74 | + self.limit_majority]) |
| 75 | + |
| 76 | + def create_learner(self): |
| 77 | + # pylint: disable=not-callable |
| 78 | + return self.LEARNER(**self.learner_kwargs()) |
| 79 | + |
| 80 | + def get_learner_parameters(self): |
| 81 | + from Orange.canvas.report import plural_w |
| 82 | + items = OrderedDict() |
| 83 | + items["Pruning"] = ", ".join(s for s, c in ( |
| 84 | + (plural_w("at least {number} instance{s} in leaves", |
| 85 | + self.min_leaf), self.limit_min_leaf), |
| 86 | + (plural_w("at least {number} instance{s} in internal nodes", |
| 87 | + self.min_internal), self.limit_min_internal), |
| 88 | + ("maximum depth {}".format(self.max_depth), self.limit_depth) |
| 89 | + ) if c) or "None" |
| 90 | + if self.limit_majority: |
| 91 | + items["Splitting"] = "Stop splitting when majority reaches %d%% " \ |
| 92 | + "(classification only)" % \ |
| 93 | + self.sufficient_majority |
| 94 | + items["Binary trees"] = ("No", "Yes")[self.binary_trees] |
| 95 | + return items |
| 96 | + |
| 97 | + |
| 98 | +def _test(): |
| 99 | + import sys |
| 100 | + from AnyQt.QtWidgets import QApplication |
| 101 | + |
| 102 | + a = QApplication(sys.argv) |
| 103 | + ow = OWTreeLearner() |
| 104 | + d = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris') |
| 105 | + ow.set_data(d) |
| 106 | + ow.show() |
| 107 | + a.exec_() |
| 108 | + ow.saveSettings() |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + _test() |
0 commit comments