source: orange/Orange/OrangeWidgets/Prototypes/OWMLkNN.py @ 10502:6b593a8cd5a0

Revision 10502:6b593a8cd5a0, 4.1 KB checked in by Matija Polajnar <matija.polajnar@…>, 2 years ago (diff)

Make multi-label warning and errors more clear on what a 'multi-label dataset' is from our perspective; Miha warned me students had troubles with this.

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 with class values 0 and 1 is "
75                            "expected on the input.")
76            return
77        self.warning(0, None)
78       
79        self.data = data
80        self.set_learner()
81
82    def set_preprocessor(self, pp):
83        self.preprocessor = pp
84        self.set_learner()
85         
86    def set_learner(self):
87        self.learner = Orange.multilabel.MLkNNLearner(k = self.k, smooth = self.smooth)
88        if self.preprocessor:
89            self.learner = self.preprocessor.wrapLearner(self.learner)
90        self.learner.name = self.name
91
92        self.send("Learner", self.learner)
93
94        self.learn()
95
96    def learn(self):
97        self.classifier = None
98        if self.data and self.learner:
99            try:
100                self.classifier = self.learner(self.data)
101                self.classifier.name = self.name
102            except Exception, (errValue):
103                self.classifier = None
104                self.error(str(errValue))
105        self.send("ML-kNN Classifier", self.classifier)
106
107##############################################################################
108# Test the widget.
109# Make sure that a sample data set (emotions.tab) is in the directory.
110
111if __name__=="__main__":
112    a=QApplication(sys.argv)
113    ow=OWMLkNN()
114
115    dataset = Orange.data.Table('emotions.tab')
116    ow.set_data(dataset)
117
118    ow.show()
119    a.exec_()
120    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.