Ignore:
Timestamp:
06/13/13 18:12:28 (10 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Updated Widget development tutorial.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/extend-widgets/rst/OWLearningCurveB.py

    r11085 r11593  
    22<name>Learning Curve (B)</name> 
    33<description>Takes a data set and a set of learners and shows a learning curve in a table</description> 
    4 <icon>icons/LearningCurveB.png</icon> 
     4<icon>icons/LearningCurve.svg</icon> 
    55<priority>1010</priority> 
    66""" 
    77 
     8import Orange 
     9 
    810from OWWidget import * 
    9 import OWGUI, orngTest, orngStat 
     11import OWGUI 
     12 
    1013 
    1114class OWLearningCurveB(OWWidget): 
     
    1518        OWWidget.__init__(self, parent, signalManager, 'LearningCurveA') 
    1619 
    17         self.inputs = [("Train Data", ExampleTable, self.trainset, Default), 
    18                        ("Test Data", ExampleTable, self.testset), 
    19                        ("Learner", orange.Learner, self.learner, Multiple)] 
     20        self.inputs = [("Train Data", Orange.data.Table, self.trainset, Default), 
     21                       ("Test Data", Orange.data.Table, self.testset), 
     22                       ("Learner", Orange.classification.Learner, 
     23                        self.learner, Multiple)] 
    2024         
    2125        self.folds = 5     # cross validation folds 
     
    2428        self.commitOnChange = 1 # compute curve on any change of parameters 
    2529        self.loadSettings() 
    26         self.setCurvePoints() # sets self.curvePoints, self.steps equidistantpoints from 1/self.steps to 1 
    27         self.scoring = [("Classification Accuracy", orngStat.CA), ("AUC", orngStat.AUC), ("BrierScore", orngStat.BrierScore), ("Information Score", orngStat.IS), ("Sensitivity", orngStat.sens), ("Specificity", orngStat.spec)] 
     30        self.setCurvePoints() # sets self.curvePoints, self.steps equidistan points from 1/self.steps to 1 
     31        self.scoring = [("Classification Accuracy", Orange.evaluation.scoring.CA), 
     32                        ("AUC", Orange.evaluation.scoring.AUC), 
     33                        ("BrierScore", Orange.evaluation.scoring.Brier_score), 
     34                        ("Information Score", Orange.evaluation.scoring.IS), 
     35                        ("Sensitivity", Orange.evaluation.scoring.Sensitivity), 
     36                        ("Specificity", Orange.evaluation.scoring.Specificity)] 
    2837        self.learners = [] # list of current learners from input channel, tuples (id, learner) 
    2938        self.data = None   # data on which to construct the learning curve 
     
    3847 
    3948        OWGUI.separator(self.controlArea) 
     49 
    4050        box = OWGUI.widgetBox(self.controlArea, "Evaluation Scores") 
    4151        scoringNames = [x[0] for x in self.scoring] 
    42         OWGUI.comboBox(box, self, "scoringF", items=scoringNames, callback=self.computeScores) 
     52        OWGUI.comboBox(box, self, "scoringF", items=scoringNames, 
     53                       callback=self.computeScores) 
    4354 
    4455        OWGUI.separator(self.controlArea) 
     56 
    4557        box = OWGUI.widgetBox(self.controlArea, "Options") 
    46         OWGUI.spin(box, self, 'folds', 2, 100, step=1, label='Cross validation folds:  ', 
     58        OWGUI.spin(box, self, 'folds', 2, 100, step=1, 
     59                   label='Cross validation folds:  ', 
    4760                   callback=lambda: self.computeCurve(self.commitOnChange)) 
    48         OWGUI.spin(box, self, 'steps', 2, 100, step=1, label='Learning curve points:  ', 
    49                    callback=[self.setCurvePoints, lambda: self.computeCurve(self.commitOnChange)]) 
     61 
     62        OWGUI.spin(box, self, 'steps', 2, 100, step=1, 
     63                   label='Learning curve points:  ', 
     64                   callback=[self.setCurvePoints, 
     65                             lambda: self.computeCurve(self.commitOnChange)]) 
    5066 
    5167        OWGUI.checkBox(box, self, 'commitOnChange', 'Apply setting on any change') 
    52         self.commitBtn = OWGUI.button(box, self, "Apply Setting", callback=self.computeCurve, disabled=1) 
     68        self.commitBtn = OWGUI.button(box, self, "Apply Setting", 
     69                                      callback=self.computeCurve, disabled=1) 
    5370 
    5471        # table widget 
    55         self.table = OWGUI.table(self.mainArea, selectionMode=QTableWidget.NoSelection) 
     72        self.table = OWGUI.table(self.mainArea, 
     73                                 selectionMode=QTableWidget.NoSelection) 
    5674                 
    5775        self.resize(500,200) 
    5876 
    59     ##############################################################################     
    60     # slots: handle input signals         
     77    ############################################################################## 
     78    # slots: handle input signals 
    6179 
    6280    def trainset(self, data): 
     
    7088            self.curves = [] 
    7189            self.scores = [] 
    72         self.commitBtn.setEnabled(self.data<>None) 
     90        self.commitBtn.setEnabled(self.data is not None) 
    7391 
    7492    def testset(self, testdata): 
     
    140158        pb = OWGUI.ProgressBar(self, iterations=self.steps*self.folds) 
    141159        if not self.testdata: 
    142             curve = orngTest.learningCurveN(learners, self.data, folds=self.folds, proportions=self.curvePoints, callback=pb.advance) 
    143         else: 
    144             curve = orngTest.learningCurveWithTestData(learners, 
    145               self.data, self.testdata, times=self.folds, proportions=self.curvePoints, callback=pb.advance)             
     160            curve = Orange.evaluation.testing.learning_curve_n( 
     161                learners, self.data, folds=self.folds, 
     162                proportions=self.curvePoints, 
     163                callback=pb.advance) 
     164        else: 
     165            curve = Orange.evaluation.testing.learning_curve_with_test_data( 
     166                learners, self.data, self.testdata, times=self.folds, 
     167                proportions=self.curvePoints, 
     168#                callback=pb.advance 
     169                ) 
    146170        pb.finish() 
    147171        return curve 
    148172 
    149173    def setCurvePoints(self): 
    150         self.curvePoints = [(x+1.)/self.steps for x in range(self.steps)] 
     174        self.curvePoints = [(x + 1.)/self.steps for x in range(self.steps)] 
    151175 
    152176    def setTable(self): 
     
    167191            self.table.setColumnWidth(i, 80) 
    168192 
    169 ############################################################################## 
    170 # Test the widget, run from prompt 
    171193 
    172194if __name__=="__main__": 
     
    175197    ow.show() 
    176198     
    177     l1 = orange.BayesLearner() 
     199    l1 = Orange.classification.bayes.NaiveLearner() 
    178200    l1.name = 'Naive Bayes' 
    179201    ow.learner(l1, 1) 
    180202 
    181     data = orange.ExampleTable('iris.tab') 
    182     indices = orange.MakeRandomIndices2(data, p0 = 0.7) 
     203    data = Orange.data.Table('iris.tab') 
     204    indices = Orange.data.sample.SubsetIndices2(data, p0 = 0.7) 
    183205    train = data.select(indices, 0) 
    184206    test = data.select(indices, 1) 
     
    187209    ow.testset(test) 
    188210 
    189     l2 = orange.BayesLearner() 
     211    l2 = Orange.classification.bayes.NaiveLearner() 
    190212    l2.name = 'Naive Bayes (m=10)' 
    191     l2.estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m=10) 
    192     l2.conditionalEstimatorConstructor = orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m=10)) 
     213    l2.estimatorConstructor = Orange.statistics.estimate.M(m=10) 
     214    l2.conditionalEstimatorConstructor = \ 
     215        Orange.statistics.estimate.ConditionalByRows( 
     216            estimatorConstructor = Orange.statistics.estimate.M(m=10)) 
    193217    ow.learner(l2, 2) 
    194218 
    195     import orngTree 
    196     l4 = orngTree.TreeLearner(minSubset=2) 
     219    l4 = Orange.classification.tree.TreeLearner(minSubset=2) 
    197220    l4.name = "Decision Tree" 
    198221    ow.learner(l4, 4) 
Note: See TracChangeset for help on using the changeset viewer.