source: orange/orange/OrangeWidgets/Classify/OWClassificationTree.py @ 9546:2b6cc6f397fe

Revision 9546:2b6cc6f397fe, 7.8 KB checked in by ales_erjavec <ales.erjavec@…>, 2 years ago (diff)

Renamed widget channel names in line with the new naming rules/convention.
Added backwards compatibility in orngDoc loadDocument to enable loading of schemas saved before the change.

Line 
1"""
2<name>Classification Tree</name>
3<description>Classification tree learner/classifier.</description>
4<icon>icons/ClassificationTree.png</icon>
5<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact>
6<priority>30</priority>
7"""
8from OWWidget import *
9import orngTree, OWGUI
10from orngWrap import PreprocessedLearner
11from exceptions import Exception
12
13import warnings
14warnings.filterwarnings("ignore", ".*this class is not optimized for 'candidates' list and can be very slow.*", orange.KernelWarning, ".*orngTree", 34)
15
16
17
18class OWClassificationTree(OWWidget):
19    settingsList = ["name",
20                    "estim", "relK", "relM",
21                    "bin", "subset",
22                    "preLeafInst", "preNodeInst", "preNodeMaj",
23                    "preLeafInstP", "preNodeInstP", "preNodeMajP",
24                    "postMaj", "postMPruning", "postM", 
25                    "limitDepth", "maxDepth"]
26
27    measures = (("Information Gain", "infoGain"), ("Gain Ratio", "gainRatio"), ("Gini Index", "gini"), ("ReliefF", "relief"))
28    binarizationOpts = ["No binarization", "Exhaustive search for optimal split", "One value against others"]
29
30    def __init__(self, parent=None, signalManager = None, name='Classification Tree'):
31        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
32
33        self.inputs = [("Data", ExampleTable, self.setData), ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
34        self.outputs = [("Learner", orange.TreeLearner),("Classification Tree", orange.TreeClassifier)]
35
36        self.name = 'Classification Tree'
37        self.estim = 0; self.relK = 5; self.relM = 100; self.limitRef = True
38        self.bin = 0; self.subset = 0
39        self.preLeafInstP = 2; self.preNodeInstP = 5; self.preNodeMajP = 95
40        self.preLeafInst = 1; self.preNodeInst = 0; self.preNodeMaj = 0
41        self.postMaj = 1; self.postMPruning = 1; self.postM = 2.0
42        self.limitDepth = False; self.maxDepth = 100
43        self.loadSettings()
44
45        self.data = None
46        self.preprocessor = None
47        self.setLearner()
48
49        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', tooltip='Name to be used by other widgets to identify your learner/classifier.')
50        OWGUI.separator(self.controlArea)
51
52        qBox = OWGUI.widgetBox(self.controlArea, 'Attribute selection criterion')
53
54        self.qMea = OWGUI.comboBox(qBox, self, "estim", items = [m[0] for m in self.measures], callback = self.measureChanged)
55
56        b1 = OWGUI.widgetBox(qBox, orientation = "horizontal")
57        OWGUI.separator(b1, 16, 0)
58        b2 = OWGUI.widgetBox(b1)
59        self.cbLimitRef, self.hbxRel1 = OWGUI.checkWithSpin(b2, self, "Limit the number of reference examples to ", 1, 1000, "limitRef", "relM")
60        OWGUI.separator(b2)
61        self.hbxRel2 = OWGUI.spin(b2, self, "relK", 1, 50, orientation="horizontal", label="Number of neighbours in ReliefF  ")
62 
63        OWGUI.separator(self.controlArea)
64
65        OWGUI.radioButtonsInBox(self.controlArea, self, 'bin', self.binarizationOpts, "Binarization")
66        OWGUI.separator(self.controlArea)
67
68        self.measureChanged()
69
70        self.pBox = OWGUI.widgetBox(self.controlArea, 'Pre-Pruning')
71
72        self.preLeafInstBox, self.preLeafInstPBox = OWGUI.checkWithSpin(self.pBox, self, "Min. instances in leaves ", 1, 1000, "preLeafInst", "preLeafInstP")
73        self.preNodeInstBox, self.preNodeInstPBox = OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes with less instances than ", 1, 1000, "preNodeInst", "preNodeInstP")
74        self.preNodeMajBox, self.preNodeMajPBox = OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes with a majority class of (%)", 1, 100, "preNodeMaj", "preNodeMajP")
75        self.cbLimitDepth, self.maxDepthBox = OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes at depth", 0, 1000, "limitDepth", "maxDepth")
76        OWGUI.separator(self.controlArea)
77        self.mBox = OWGUI.widgetBox(self.controlArea, 'Post-Pruning')
78
79        OWGUI.checkBox(self.mBox, self, 'postMaj', 'Recursively merge leaves with same majority class')
80        self.postMPruningBox, self.postMPruningPBox = OWGUI.checkWithSpin(self.mBox, self, "Pruning with m-estimate, m=", 0, 1000, 'postMPruning', 'postM')
81
82        OWGUI.separator(self.controlArea)
83        self.btnApply = OWGUI.button(self.controlArea, self, "&Apply", callback = self.setLearner, disabled=0, default=True)
84       
85        OWGUI.rubber(self.controlArea)
86        self.resize(200,200)
87
88   
89    def sendReport(self):
90        self.reportSettings("Learning parameters",
91                            [("Attribute selection", self.measures[self.estim][0]),
92                             self.estim == 3 and ("ReliefF settings", "%i reference examples, %i neighbours" % (self.relM, self.relK)),
93                             ("Binarization", self.binarizationOpts[self.bin]),
94                             ("Pruning", ", ".join(s for s, c in (
95                                                 ("%i instances in leaves" % self.preLeafInstP, self.preLeafInst),
96                                                 ("%i instance in node" % self.preNodeInstP, self.preNodeInst),
97                                                 ("stop on %i%% purity" % self.preNodeMajP, self.preNodeMaj),
98                                                 ("maximum depth %i" % self.maxDepth, self.limitDepth)) if c)
99                                          or "None"),
100                             ("Recursively merge leaves with same majority class", OWGUI.YesNo[self.postMaj]),
101                             ("Pruning with m-estimate", ["No", "m=%i" % self.postM][self.postMPruning])])
102        self.reportData(self.data)
103
104    def setPreprocessor(self, preprocessor):
105        self.preprocessor = preprocessor
106        self.setLearner()
107       
108    def setLearner(self):
109        if hasattr(self, "btnApply"):
110            self.btnApply.setFocus()
111        if not self.limitDepth:
112            mDepth = {}
113        else:
114            mDepth = {'maxDepth': self.maxDepth}
115        self.learner = orngTree.TreeLearner(measure = self.measures[self.estim][1],
116            reliefK = self.relK, reliefM = self.limitRef and self.relM or -1,
117            binarization = self.bin,
118            minExamples = self.preNodeInst and self.preNodeInstP,
119            minSubset = self.preLeafInst and self.preLeafInstP,
120            maxMajority = self.preNodeMaj and self.preNodeMajP/100.0 or 1.0,
121            sameMajorityPruning = self.postMaj,
122            mForPruning = self.postMPruning and self.postM,
123            storeExamples = 1, **mDepth)
124
125        self.learner.name = self.name
126        if self.preprocessor:
127            self.learner = self.preprocessor.wrapLearner(self.learner)
128       
129        self.send("Learner", self.learner)
130
131        self.error()
132        if self.data:
133            try:
134                self.classifier = self.learner(self.data)
135                self.classifier.name = self.name
136            except Exception, (errValue):
137                self.error(str(errValue))
138                self.classifier = None
139        else:
140            self.classifier = None
141
142        self.send("Classification Tree", self.classifier)
143
144
145    def measureChanged(self):
146        relief = self.estim == 3
147        self.hbxRel1.setEnabled(relief and self.limitRef)
148        self.hbxRel2.setEnabled(relief)
149        self.cbLimitRef.setEnabled(relief)
150
151    def setData(self,data):
152        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
153        self.setLearner()
154
155
156##############################################################################
157# Test the widget, run from DOS prompt
158# > python OWDataTable.py)
159# Make sure that a sample data set (adult_sample.tab) is in the directory
160
161if __name__=="__main__":
162    a=QApplication(sys.argv)
163    ow=OWClassificationTree()
164
165    #d = orange.ExampleTable('adult_sample')
166    #ow.setData(d)
167
168    ow.show()
169    a.exec_()
170    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.