source: orange/docs/extend-widgets/rst/OWLearningCurveA.py @ 11881:99bec0d8a70d

Revision 11881:99bec0d8a70d, 7.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 4 weeks ago (diff)

More fixes to widget development manual code snippets.

Line 
1"""
2<name>Learning Curve (A)</name>
3<description>Takes a data set and a set of learners and shows a learning curve in a table</description>
4<icon>icons/LearningCurve.svg</icon>
5<priority>1000</priority>
6"""
7
8import Orange
9
10from OWWidget import *
11import OWGUI
12
13class OWLearningCurveA(OWWidget):
14    settingsList = ["folds", "steps", "scoringF", "commitOnChange"]
15
16    def __init__(self, parent=None, signalManager=None):
17        OWWidget.__init__(self, parent, signalManager, 'LearningCurveA')
18# [start-snippet-1]
19        self.inputs = [("Data", Orange.data.Table, self.set_dataset),
20                       ("Learner", Orange.classification.Learner, self.set_learner,
21                        Multiple + Default)]
22# [end-snippet-1]
23        self.folds = 5     # cross validation folds
24        self.steps = 10    # points in the learning curve
25        self.scoringF = 0  # scoring function
26        self.commitOnChange = 1 # compute curve on any change of parameters
27        self.loadSettings()
28        self.updateCurvePoints() # sets self.curvePoints, self.steps equidistant points from 1/self.steps to 1
29# [start-snippet-2]
30        self.scoring = [("Classification Accuracy", Orange.evaluation.scoring.CA),
31                        ("AUC", Orange.evaluation.scoring.AUC),
32                        ("BrierScore", Orange.evaluation.scoring.Brier_score),
33                        ("Information Score", Orange.evaluation.scoring.IS),
34                        ("Sensitivity", Orange.evaluation.scoring.Sensitivity),
35                        ("Specificity", Orange.evaluation.scoring.Specificity)]
36# [end-snippet-2]
37        self.learners = [] # list of current learners from input channel, tuples (id, learner)
38        self.data = None   # data on which to construct the learning curve
39        self.curves = []   # list of evaluation results (one per learning curve point)
40        self.scores = []   # list of current scores, learnerID:[learner scores]
41
42        # GUI
43        box = OWGUI.widgetBox(self.controlArea, "Info")
44        self.infoa = OWGUI.widgetLabel(box, 'No data on input.')
45        self.infob = OWGUI.widgetLabel(box, 'No learners.')
46
47        OWGUI.separator(self.controlArea)
48
49        box = OWGUI.widgetBox(self.controlArea, "Evaluation Scores")
50        scoringNames = [x[0] for x in self.scoring]
51        OWGUI.comboBox(box, self, "scoringF", items=scoringNames,
52                       callback=self.computeScores)
53
54        OWGUI.separator(self.controlArea)
55
56        box = OWGUI.widgetBox(self.controlArea, "Options")
57        OWGUI.spin(box, self, 'folds', 2, 100, step=1,
58                   label='Cross validation folds:  ',
59                   callback=lambda: self.computeCurve() if self.commitOnChange else None)
60        OWGUI.spin(box, self, 'steps', 2, 100, step=1,
61                   label='Learning curve points:  ',
62                   callback=[self.updateCurvePoints,
63                             lambda: self.computeCurve() if self.commitOnChange else None])
64        OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change')
65        self.commitBtn = OWGUI.button(box, self, "Apply Setting",
66                                      callback=self.computeCurve, disabled=1)
67
68        OWGUI.rubber(self.controlArea)
69
70        # table widget
71        self.table = OWGUI.table(self.mainArea,
72                                 selectionMode=QTableWidget.NoSelection)
73
74        self.resize(500,200)
75
76    ##############################################################################   
77    # slots: handle input signals
78
79    def set_dataset(self, data):
80        if data is not None:
81            self.infoa.setText('%d instances in input data set' % len(data))
82            self.data = data
83            if len(self.learners):
84                self.computeCurve()
85        else:
86            self.infoa.setText('No data on input.')
87            self.curves = []
88            self.scores = []
89        self.commitBtn.setEnabled(self.data is not None)
90
91    def set_learner(self, learner, id=None):
92        ids = [x[0] for x in self.learners]
93        if learner is None: # remove a learner and corresponding results
94            if not ids.count(id):
95                return # no such learner, removed before
96            indx = ids.index(id)
97            for i in range(self.steps):
98                self.curves[i].remove(indx)
99            del self.scores[indx]
100            del self.learners[indx]
101            self.updateTable()
102        else:
103            if ids.count(id): # update (already seen a learner from this source)
104                indx = ids.index(id)
105                self.learners[indx] = (id, learner)
106                if self.data:
107                    curve = self.getLearningCurve([learner])
108                    score = [self.scoring[self.scoringF][1](x)[0] for x in curve]
109                    self.scores[indx] = score
110                    for i in range(self.steps):
111                        self.curves[i].add(curve[i], 0, replace=indx)
112            else: # add new learner
113                self.learners.append((id, learner))
114                if self.data:
115                    curve = self.getLearningCurve([learner])
116                    score = [self.scoring[self.scoringF][1](x)[0] for x in curve]
117                    self.scores.append(score)
118                    if len(self.curves):
119                        for i in range(self.steps):
120                            self.curves[i].add(curve[i], 0)
121                    else:
122                        self.curves = curve
123        if len(self.learners):
124            self.infob.setText("%d learners on input." % len(self.learners))
125        else:
126            self.infob.setText("No learners.")
127        self.commitBtn.setEnabled(len(self.learners))
128
129        if self.data:
130            self.updateTable()
131
132    def computeCurve(self):
133        learners = [x[1] for x in self.learners]
134        self.curves = self.getLearningCurve(learners)
135        self.computeScores()
136
137    def computeScores(self):           
138        self.scores = [[] for i in range(len(self.learners))]
139        for x in self.curves:
140            for (i,s) in enumerate(self.scoring[self.scoringF][1](x)):
141                self.scores[i].append(s)
142        self.updateTable()
143
144    def getLearningCurve(self, learners):   
145        pb = OWGUI.ProgressBar(self, iterations=self.steps*self.folds)
146        curve = Orange.evaluation.testing.learning_curve_n(
147            learners, self.data, folds=self.folds,
148            proportions=self.curvePoints, callback=pb.advance)
149        pb.finish()
150        return curve
151
152    def updateCurvePoints(self):
153        self.curvePoints = [(x + 1.)/self.steps for x in range(self.steps)]
154
155    def updateTable(self):
156        self.table.setColumnCount(0)
157        self.table.setColumnCount(len(self.learners))
158        self.table.setRowCount(self.steps)
159
160        # set the headers
161        self.table.setHorizontalHeaderLabels([l.name for i,l in self.learners])
162        self.table.setVerticalHeaderLabels(["%4.2f" % p for p in self.curvePoints])
163
164        # set the table contents
165        for l in range(len(self.learners)):
166            for p in range(self.steps):
167                OWGUI.tableItem(self.table, p, l, "%7.5f" % self.scores[l][p])
168
169        for i in range(len(self.learners)):
170            self.table.setColumnWidth(i, 80)
171
172
173if __name__=="__main__":
174    appl = QApplication(sys.argv)
175    ow = OWLearningCurveA()
176    ow.show()
177   
178    l1 = Orange.classification.bayes.NaiveLearner()
179    l1.name = 'Naive Bayes'
180    ow.set_learner(l1, 1)
181
182    data = Orange.data.Table('iris.tab')
183    ow.set_dataset(data)
184
185    l2 = Orange.classification.bayes.NaiveLearner()
186    l2.name = 'Naive Bayes (m=10)'
187    l2.estimatorConstructor = Orange.statistics.estimate.M(m=10)
188    l2.conditionalEstimatorConstructor = \
189        Orange.statistics.estimate.ConditionalByRows(
190            estimatorConstructor = Orange.statistics.estimate.M(m=10))
191    ow.set_learner(l2, 2)
192
193    l4 = Orange.classification.tree.TreeLearner(minSubset=2)
194    l4.name = "Decision Tree"
195    ow.set_learner(l4, 4)
196
197#    ow.set_learner(None, 1)
198#    ow.set_learner(None, 2)
199#    ow.set_learner(None, 4)
200
201    appl.exec_()
Note: See TracBrowser for help on using the repository browser.