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

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

Moved orange to Orange (part 2)

Line 
1"""
2<name>Naive Bayes</name>
3<description>Naive Bayesian learner/classifier.</description>
4<icon>icons/NaiveBayes.png</icon>
5<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact>
6<priority>10</priority>
7"""
8
9from OWWidget import *
10import OWGUI, orange
11from orngWrap import PreprocessedLearner
12from exceptions import Exception
13
14import warnings
15warnings.filterwarnings("ignore", r"'BayesLearner': invalid conditional probability or no attributes \(the classifier will use apriori probabilities\)", orange.KernelWarning, ".*OWNaiveBayes", 136)
16
17
18class OWNaiveBayes(OWWidget):
19    settingsList = ["m_estimator.m", "name", "probEstimation", "condProbEstimation", "adjustThreshold", "windowProportion"]
20
21    def __init__(self, parent=None, signalManager = None, name='NaiveBayes'):
22        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
23        self.inputs = [("Data", ExampleTable, self.setData), ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
24        self.outputs = [("Learner", orange.Learner),("Naive Bayesian Classifier", orange.BayesClassifier)]
25
26        self.m_estimator = orange.ProbabilityEstimatorConstructor_m()
27        self.estMethods=[("Relative Frequency", orange.ProbabilityEstimatorConstructor_relative()),
28                         ("Laplace", orange.ProbabilityEstimatorConstructor_Laplace()),
29                         #("m-Estimate", self.m_estimator)
30                         ]
31        self.condEstMethods=[("<same as above>", None),
32                             ("Relative Frequency", orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor=orange.ProbabilityEstimatorConstructor_relative())),
33                             ("Laplace", orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor=orange.ProbabilityEstimatorConstructor_Laplace())),
34                             ("m-Estimate", orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor=self.m_estimator))]
35
36        self.m_estimator.m = 2.0
37        self.name = 'Naive Bayes'
38        self.probEstimation = 0
39        self.condProbEstimation = 0
40        self.adjustThreshold = 0
41        self.windowProportion = 0.5
42        self.loessPoints = 100
43
44        self.preprocessor = None
45        self.data = None
46        self.loadSettings()
47
48
49        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', \
50                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
51        OWGUI.separator(self.controlArea)
52
53        glay = QGridLayout()
54        box = OWGUI.widgetBox(self.controlArea, 'Probability estimation', orientation = glay)
55
56        #glay.addWidget(OWGUI.separator(box, height=5), 0, 0)
57
58        glay.addWidget(OWGUI.widgetLabel(box, "Prior"), 1, 0)
59
60        glay.addWidget(OWGUI.comboBox(box, self, 'probEstimation', items=[e[0] for e in self.estMethods], tooltip='Method for estimating the prior probability.'),
61                        1, 2)
62
63        glay.addWidget(OWGUI.widgetLabel(box, "Conditional (for discrete)"), 2, 0)
64        glay.addWidget(OWGUI.comboBox(box, self, 'condProbEstimation', items=[e[0] for e in self.condEstMethods], tooltip='Conditional probability estimation method used for discrete attributes.', callback=self.refreshControls),
65                       2, 2)
66
67        glay.addWidget(OWGUI.widgetLabel(box, "     " + "Parameter for m-estimate" + " "), 3, 0)
68        mValid = QDoubleValidator(self.controlArea)
69        mValid.setRange(0,10000,1)
70        self.mwidget = OWGUI.lineEdit(box, self, 'm_estimator.m', valueType = float, validator = mValid)
71        glay.addWidget(self.mwidget, 3, 2)
72
73        glay.addWidget(OWGUI.separator(box), 4, 0)
74
75        glay.addWidget(OWGUI.widgetLabel(box, 'Size of LOESS window'), 5, 0)
76        kernelSizeValid = QDoubleValidator(self.controlArea)
77        kernelSizeValid.setRange(0,1,3)
78        glay.addWidget(OWGUI.lineEdit(box, self, 'windowProportion',
79                       tooltip='Proportion of examples used for local learning in loess.\nUse 0 to learn from few local instances (3) and 1 to learn from all in the data set (this kind of learning is not local anymore).',
80                       valueType = float, validator = kernelSizeValid),
81                       5, 2)
82
83        glay.addWidget(OWGUI.widgetLabel(box, 'LOESS sample points'), 6, 0)
84        pointsValid = QIntValidator(20, 1000, self.controlArea)
85        glay.addWidget(OWGUI.lineEdit(box, self, 'loessPoints',
86                       tooltip='Number of points in computation of LOESS (20-1000).',
87                       valueType = int, validator = pointsValid),
88                       6, 2)
89
90        OWGUI.separator(self.controlArea)
91
92        OWGUI.checkBox(self.controlArea, self, "adjustThreshold", "Adjust threshold (for binary classes)", box = "Threshold")
93
94        OWGUI.separator(self.controlArea)
95#        box = OWGUI.widgetBox(self.controlArea, "Apply", orientation=1)
96        applyButton = OWGUI.button(self.controlArea, self, "&Apply", callback=self.applyLearner, default=True)
97
98        OWGUI.rubber(self.controlArea)
99        self.refreshControls()
100        self.applyLearner()
101
102
103    def refreshControls(self, *a):
104        self.mwidget.setEnabled(self.condProbEstimation==3)
105
106    def applyLearner(self):
107        self.warning(0)
108        if float(self.m_estimator.m) < 0:
109            self.warning(0, "Parameter m should be positive")
110            self.learner = None
111
112        elif float(self.windowProportion) < 0 or float(self.windowProportion) > 1:
113            self.warning(0, "Window proportion for LOESS should be between 0.0 and 1.0")
114            self.learner = None
115
116        else:
117            self.learner = orange.BayesLearner(name = self.name, adjustThreshold = self.adjustThreshold)
118            self.learner.estimatorConstructor = self.estMethods[self.probEstimation][1]
119            if self.condProbEstimation:
120                self.learner.conditionalEstimatorConstructor = self.condEstMethods[self.condProbEstimation][1]
121                self.learner.conditionalEstimatorConstructorContinuous = orange.ConditionalProbabilityEstimatorConstructor_loess(
122                   windowProportion = self.windowProportion, nPoints = self.loessPoints)
123
124            if self.preprocessor:
125                self.learner = self.preprocessor.wrapLearner(self.learner)
126
127        self.send("Learner", self.learner)
128        self.applyData()
129        self.changed = False
130
131
132    def applyData(self):
133        self.error(1)
134        if self.data and self.learner:
135            try:
136                if self.preprocessor:
137                    classifier, data = self.learner(self.data, getData = True)
138                    classifier.setattr("data", data)
139                else:
140                    classifier = self.learner(self.data)
141                    classifier.setattr("data", self.data)
142                classifier.name = self.name
143            except Exception, (errValue):
144                classifier = None
145                self.error(1, "Naive Bayes error: " + str(errValue))
146        else:
147            classifier = None
148           
149        self.send("Naive Bayesian Classifier", classifier)
150
151
152    def setPreprocessor(self, pp):
153        self.preprocessor = pp
154        self.applyLearner()
155       
156    def setData(self,data):
157        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
158        self.applyData()
159
160
161    def sendReport(self):
162        self.reportSettings("Learning parameters",
163                            [("Probability estimation", self.estMethods[self.probEstimation][0]),
164                             self.condProbEstimation and ("Conditional probability", self.condEstMethods[self.condProbEstimation][0]),
165                             self.condProbEstimation==3 and ("m for m-estimate", "%.1f" % self.m_estimator.m),
166                             ("LOESS window size", "%.1f" % self.windowProportion),
167                             ("Number of points in LOESS", "%i" % self.loessPoints),
168                             ("Adjust classification threshold", OWGUI.YesNo[self.adjustThreshold])
169                            ])
170        self.reportData(self.data)
171
172
173if __name__=="__main__":
174    a=QApplication(sys.argv)
175    ow=OWNaiveBayes()
176
177    ow.show()
178    a.exec_()
179    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.