source: orange/orange/OrangeWidgets/Prototypes/OWMLkNN.py @ 9599:69c91d52d3e4

Revision 9599:69c91d52d3e4, 4.1 KB checked in by Matija Polajnar <matija.polajnar@…>, 2 years ago (diff)

Multi-label classificaiton widgets. Merged in from Wencan Luo's work with some modifications.

Line 
1"""
2<name>ML-kNN</name>
3<description>ML-kNN Multi-label Learner.</description>
4<icon>icons/Unknown.png</icon>
5<contact>Wencan Luo (wencanluo.cn(@at@)gmail.com)</contact>
6<priority>100</priority>
7"""
8from OWWidget import *
9import OWGUI
10from exceptions import Exception
11from orngWrap import PreprocessedLearner
12
13import Orange
14
15class OWMLkNN(OWWidget):
16    settingsList = ["name", "k", "smooth"]
17
18    def __init__(self, parent=None, signalManager = None, name='ML-kNN'):
19        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
20
21        self.callbackDeposit = []
22
23        self.inputs = [("Examples", ExampleTable, self.set_data), 
24                       ("Preprocess", PreprocessedLearner, self.set_preprocessor)
25                       ]
26        self.outputs = [("Learner", orange.Learner),("ML-kNN Classifier", Orange.multilabel.MLkNNClassifier)]
27
28        # Settings
29        self.name = 'ML-kNN'
30        self.k = 1
31        self.smooth = 1.0
32       
33        self.loadSettings()
34
35        self.data = None                    # input data set
36        self.preprocessor = None            # no preprocessing as default
37        self.set_learner()                  # this just sets the learner, no data
38                                            # has come to the input yet
39
40        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', \
41                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
42
43        OWGUI.separator(self.controlArea)
44
45        wbN = OWGUI.widgetBox(self.controlArea, "Neighbours")
46        OWGUI.spin(wbN, self, "k", 1, 100, 1, None, "Number of neighbours", orientation="horizontal")
47       
48        OWGUI.separator(self.controlArea)
49        OWGUI.widgetLabel(self.controlArea, 'Smoothing parameter')
50        kernelSizeValid = QDoubleValidator(self.controlArea)
51        kernelSizeValid.setRange(0,10,3)
52        OWGUI.lineEdit(self.controlArea, self, 'smooth',
53                       tooltip='Smoothing parameter controlling the strength of uniform prior (Default value is set to 1 which yields the Laplace smoothing).',
54                       valueType = float, validator = kernelSizeValid)
55                       
56        OWGUI.separator(self.controlArea)
57
58        OWGUI.button(self.controlArea, self, "&Apply", callback=self.set_learner, disabled=0, default=True)
59       
60        OWGUI.rubber(self.controlArea)
61
62        self.resize(100,250)
63
64    def send_report(self):
65        self.reportSettings("Learning parameters",
66                            [("base_learner", self.baselearnerList[self.base_learner][0])])
67        self.reportData(self.data)
68           
69    def set_data(self,data): 
70        if data == None:
71            return
72
73        if not Orange.multilabel.is_multilabel(data):
74            self.warning(0, "Multi-label data is expected on the input.")
75            return
76        self.warning(0, None)
77       
78        self.data = data
79        self.set_learner()
80
81    def set_preprocessor(self, pp):
82        self.preprocessor = pp
83        self.set_learner()
84         
85    def set_learner(self):
86        self.learner = Orange.multilabel.MLkNNLearner(k = self.k, smooth = self.smooth)
87        if self.preprocessor:
88            self.learner = self.preprocessor.wrapLearner(self.learner)
89        self.learner.name = self.name
90
91        self.send("Learner", self.learner)
92
93        self.learn()
94
95    def learn(self):
96        self.classifier = None
97        if self.data and self.learner:
98            try:
99                self.classifier = self.learner(self.data)
100                self.classifier.name = self.name
101            except Exception, (errValue):
102                self.classifier = None
103                self.error(str(errValue))
104        self.send("ML-kNN Classifier", self.classifier)
105
106##############################################################################
107# Test the widget.
108# Make sure that a sample data set (emotions.tab) is in the directory.
109
110if __name__=="__main__":
111    a=QApplication(sys.argv)
112    ow=OWMLkNN()
113
114    dataset = Orange.data.Table('emotions.tab')
115    ow.set_data(dataset)
116
117    ow.show()
118    a.exec_()
119    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.