source: orange/Orange/OrangeWidgets/Classify/OWMajority.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 2.7 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

Line 
1"""
2<name>Majority</name>
3<description>Majority class learner/classifier.</description>
4<icon>icons/Majority.png</icon>
5<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact>
6<priority>20</priority>
7"""
8from OWWidget import *
9import OWGUI
10from exceptions import Exception
11
12from orngWrap import PreprocessedLearner
13class OWMajority(OWWidget):
14    settingsList = ["name"]
15
16    def __init__(self, parent=None, signalManager = None):
17        OWWidget.__init__(self, parent, signalManager, 'Majority', wantMainArea = 0, resizingEnabled = 0)
18
19        self.callbackDeposit = []
20
21        self.inputs = [("Data", ExampleTable, self.setData), ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
22        self.outputs = [("Learner", orange.Learner),("Classifier", orange.Classifier)]
23
24        self.name = 'Majority'
25       
26        self.data = None
27        self.preprocessor = None
28
29        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', \
30                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
31
32        OWGUI.separator(self.controlArea)
33
34        OWGUI.button(self.controlArea, self, "&Apply", callback=self.setLearner, disabled=0, default=True)
35       
36        OWGUI.rubber(self.controlArea)
37
38        self.learner = orange.MajorityLearner()
39        self.setLearner()
40        self.resize(100,100)
41
42    def setLearner(self):
43        if self.preprocessor:
44            self.learner = self.preprocessor.wrapLearner(orange.MajorityLearner())
45        self.learner.name = self.name
46        self.send("Learner", self.learner)
47
48    def setData(self,data):
49        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
50
51        if self.data:
52            try:
53                self.classifier = self.learner(self.data)
54                self.classifier.name = self.name
55                self.error(1)
56            except Exception, (errValue):
57                self.classifier = None
58                self.error(1, str(errValue))
59        else:
60            self.classifier = None
61        self.send("Classifier", self.classifier)
62       
63    def setPreprocessor(self, pp):
64        self.preprocessor = pp
65        self.setLearner()
66        self.setData(self.data)
67
68    def sendReport(self):
69        self.reportData(self.data)
70
71##############################################################################
72# Test the widget, run from DOS prompt
73# > python OWDataTable.py)
74# Make sure that a sample data set (adult_sample.tab) is in the directory
75
76if __name__=="__main__":
77    a=QApplication(sys.argv)
78    ow=OWMajority()
79
80##    dataset = orange.ExampleTable('adult_sample')
81##    ow.setData(dataset)
82
83    ow.show()
84    a.exec_()
85    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.