source: orange/Orange/OrangeWidgets/Classify/OWC45Tree.py @ 10844:8d8d78127c20

Revision 10844:8d8d78127c20, 8.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 2 years ago (diff)

Fixed c45 test on windows.

Line 
1"""
2<name>C4.5</name>
3<description>C45 (classification tree) learner/classifier.</description>
4<icon>icons/C45.png</icon>
5<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact>
6<priority>35</priority>
7"""
8import os
9
10from distutils import sysconfig
11
12from OWWidget import *
13import OWGUI
14from exceptions import Exception
15
16from orngWrap import PreprocessedLearner
17
18import Orange
19
20_Orange_dir = os.path.dirname(Orange.__file__)
21
22if sys.platform == "win32":
23    _SO = "c45.dll"
24else:
25    _SO = "c45.so"
26
27if os.path.exists(os.path.join(_Orange_dir, _SO)):
28    # Test if the c45.(so|pyd) can be loaded by orange.C45Learner
29    try:
30        Orange.core.C45Learner()
31    except orange.KernelException, ex:
32        # I guess not
33        raise ImportError(ex.message)
34else:
35    raise ImportError("c45 is not found")
36
37
38class OWC45Tree(OWWidget):
39    settingsList = ["name",
40                    "infoGain", "subset", "probThresh",
41                    "minObjs", "prune", "cf",
42                    "iterative", "manualWindow", "window", "manualIncrement", "increment", "trials",
43                    "convertToOrange"]
44
45    def __init__(self, parent=None, signalManager = None, name='C4.5'):
46        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
47
48        self.callbackDeposit = []
49
50        self.inputs = [("Data", ExampleTable, self.setData),
51                       ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
52       
53        self.outputs = [("Learner", orange.Learner),
54                        ("Classification Tree", Orange.classification.tree.TreeClassifier)]#, ("C45 Tree", orange.C45Classifier)]
55
56        # Settings
57        self.name = 'C4.5'
58        self.infoGain = 0;  self.subset = 0;       self.probThresh = 0;
59        self.useMinObjs = 1; self.minObjs = 2;   self.prune = 1;       self.cf = 25
60        self.iterative = 0; self.manualWindow = 0; self.window = 50;     self.manualIncrement = 0;  self.increment = 10;   self.trials = 10
61
62        self.convertToOrange = 1
63
64        self.loadSettings()
65
66        self.data = None                    # input data set
67        self.preprocessor = None            # no preprocessing as default
68
69        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name',
70                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
71        OWGUI.separator(self.controlArea)
72
73        self.wbSplit = OWGUI.widgetBox(self.controlArea, "Splitting")
74        OWGUI.checkBox(self.wbSplit, self, 'infoGain', 'Use information gain instead of ratio (-g)')
75        OWGUI.checkBox(self.wbSplit, self, 'subset', 'Subsetting (-s)')
76        OWGUI.checkBox(self.wbSplit, self, 'probThresh', 'Probabilistic threshold for continuous attributes (-p)')
77
78        OWGUI.separator(self.controlArea)
79
80        self.wbPruning = OWGUI.widgetBox(self.controlArea, "Pruning")
81        OWGUI.checkWithSpin(self.wbPruning, self, 'Minimal examples in leaves (-m)', 1, 1000, 'useMinObjs', 'minObjs', '', 1, labelWidth = 225)
82        OWGUI.checkWithSpin(self.wbPruning, self, 'Post pruning with confidence level (-cf) of ', 0, 100, 'prune', 'cf', '', 5, labelWidth = 225)
83
84        OWGUI.separator(self.controlArea)
85
86        self.wbIterative = OWGUI.widgetBox(self.controlArea, "Iterative generation")
87        self.cbIterative = OWGUI.checkBox(self.wbIterative, self, 'iterative', 'Generate the tree iteratively (-i, -t, -w)')
88        self.spTrial = OWGUI.spin(self.wbIterative, self, 'trials', 1, 30, 1, '', "       Number of trials (-t)", orientation = "horizontal", labelWidth = 225)
89        self.csWindow = OWGUI.checkWithSpin(self.wbIterative, self, "Manually set initial window size (-w) to ", 10, 1000, 'manualWindow', 'window', '', 10, labelWidth = 225)
90        self.csIncrement = OWGUI.checkWithSpin(self.wbIterative, self, "Manually set window increment (-i) to ", 10, 1000, 'manualIncrement', 'increment', '', 10, labelWidth = 225)
91
92        self.cbIterative.disables = [self.spTrial, self.csWindow, self.csIncrement]
93        self.cbIterative.makeConsistent()
94
95#        OWGUI.separator(self.controlArea)
96
97#        OWGUI.checkBox(self.controlArea, self, 'convertToOrange', 'Convert to orange tree structure', box = 1)
98
99        OWGUI.separator(self.controlArea)
100
101        OWGUI.button(self.controlArea, self, "&Apply", callback = self.setLearner, disabled=0, default=True)
102
103        OWGUI.rubber(self.controlArea)
104        self.setLearner()
105
106
107    def sendReport(self):
108        self.reportSettings("Learning parameters",
109                            [("Attribute quality measure", ["Information gain", "Gain ratio"][self.infoGain]),
110                             ("Subsetting", OWGUI.YesNo[self.subset]),
111                             ("Probabilistic threshold for continuous attributes", OWGUI.YesNo[self.probThresh]),
112                             self.useMinObjs and ("Minimal number of examples in leaves", self.minObjs),
113                             self.prune and ("Post pruning confidence level", self.cf),
114                             ("Iterative generation", OWGUI.YesNo[self.iterative]),
115                             self.iterative and ("Number of trials", self.trials),
116                             self.iterative and self.manualWindow and ("Initial window size manually set to", self.window),
117                             self.iterative and self.manualIncrement and ("Window increment manually set to", self.increment)])
118        self.reportData(self.data)
119
120
121
122    def setData(self,data):
123        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
124        self.setLearner()
125       
126    def setPreprocessor(self, pp):
127        self.preprocessor = pp
128        self.setLearner()
129
130
131    def setLearner(self):
132        self.error(0)
133        try:
134            self.learner = orange.C45Learner(gainRatio=not self.infoGain, subset=self.subset, probThresh=self.probThresh,
135                                             minObjs=self.useMinObjs and self.minObjs or 0, prune=self.prune, cf=self.cf/100.,
136                                             batch = not self.iterative, window=self.manualWindow and self.window or 0, increment=self.manualIncrement and self.increment or 0, trials=self.trials,
137                                             convertToOrange = 1, #self.convertToOrange,
138                                             storeExamples = 1)
139        except orange.KernelException, ex:
140            self.error(0, "C45Loader: cannot load \c45.dll")
141            import orngDebugging
142            # Dont show the message box when running debugging scripts
143            if not orngDebugging.orngDebuggingEnabled and getattr(self, "__showMessageBox", True): 
144                # The message string must not have newlines otherwise it
145                # confuses Qt to show it in plain text not as rtf.
146                QMessageBox.warning( None, "C4.5 plug-in", '''File c45.dll not found.
147See <a href="http://orange.biolab.si/doc/reference/Orange.classification.tree/#c4-5-classifier-and-learner">this link</a>
148for the instructions on how to build the library.'''.replace("\n", " "), QMessageBox.Ok)
149                setattr(self, "__showMessageBox", False)
150            return
151       
152        if self.preprocessor:
153            self.learner = self.preprocessor.wrapLearner(self.learner)
154
155        self.learner.name = self.name
156        self.send("Learner", self.learner)
157
158        self.learn()
159
160
161    def learn(self):
162        self.error()
163        if self.data and self.learner:
164            if not self.data.domain.classVar:
165                self.error("This data set has no class.")
166                self.classifier = None
167            elif self.data.domain.classVar.varType != orange.VarTypes.Discrete:
168                self.error("This algorithm only works with discrete classes.")
169                self.classifier = None
170            else:
171                try:
172                    self.classifier = self.learner(self.data)
173                    self.classifier.name = self.name
174                except Exception, (errValue):
175                    self.error(str(errValue))
176                    self.classifier = None
177        else:
178            self.classifier = None
179
180#        self.send("Classifier", self.classifier)
181#        if self.convertToOrange:
182        self.send("Classification Tree", self.classifier)
183#        else:
184#            self.send("C45 Tree", self.classifier)
185
186       
187##############################################################################
188# Test the widget, run from DOS prompt
189# > python OWDataTable.py)
190# Make sure that a sample data set (adult_sample.tab) is in the directory
191
192if __name__=="__main__":
193    a=QApplication(sys.argv)
194    ow=OWC45Tree()
195##    dataset = orange.ExampleTable('adult_sample')
196##    ow.setData(dataset)
197
198    ow.show()
199    a.exec_()
200    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.