source: orange/Orange/OrangeWidgets/Classify/OWC45Tree.py @ 10770:af870e1d8181

Revision 10770:af870e1d8181, 8.4 KB checked in by Ales Erjavec <ales.erjavec@…>, 2 years ago (diff)

Raise an import error at widget import if the c4.5 library is not found.

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