def __init__(self, parent=None, signalManager = None, name='NaiveBayes'): OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0) self.inputs = [("Data", ExampleTable, self.setData), ("Preprocess", PreprocessedLearner, self.setPreprocessor)] self.outputs = [("Learner", orange.Learner),("Naive Bayesian Classifier", orange.BayesClassifier)] self.m_estimator = orange.ProbabilityEstimatorConstructor_m() self.estMethods=[("Relative Frequency", orange.ProbabilityEstimatorConstructor_relative()), ("Laplace", orange.ProbabilityEstimatorConstructor_Laplace()), #("m-Estimate", self.m_estimator) ] self.condEstMethods=[("<same as above>", None), ("Relative Frequency", orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor=orange.ProbabilityEstimatorConstructor_relative())), ("Laplace", orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor=orange.ProbabilityEstimatorConstructor_Laplace())), ("m-Estimate", orange.ConditionalProbabilityEstimatorConstructor_ByRows(estimatorConstructor=self.m_estimator))] self.m_estimator.m = 2.0 self.name = 'Naive Bayes' self.probEstimation = 0 self.condProbEstimation = 0 self.adjustThreshold = 0 self.windowProportion = 0.5 self.loessPoints = 100 self.preprocessor = None self.data = None self.loadSettings() OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', \ tooltip='Name to be used by other widgets to identify your learner/classifier.') OWGUI.separator(self.controlArea) glay = QGridLayout() box = OWGUI.widgetBox(self.controlArea, 'Probability estimation', orientation = glay) #glay.addWidget(OWGUI.separator(box, height=5), 0, 0) glay.addWidget(OWGUI.widgetLabel(box, "Prior"), 1, 0) glay.addWidget(OWGUI.comboBox(box, self, 'probEstimation', items=[e[0] for e in self.estMethods], tooltip='Method for estimating the prior probability.'), 1, 2) glay.addWidget(OWGUI.widgetLabel(box, "Conditional (for discrete)"), 2, 0) 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), 2, 2) glay.addWidget(OWGUI.widgetLabel(box, " " + "Parameter for m-estimate" + " "), 3, 0) mValid = QDoubleValidator(self.controlArea) mValid.setRange(0,10000,1) self.mwidget = OWGUI.lineEdit(box, self, 'm_estimator.m', valueType = float, validator = mValid) glay.addWidget(self.mwidget, 3, 2) glay.addWidget(OWGUI.separator(box), 4, 0) glay.addWidget(OWGUI.widgetLabel(box, 'Size of LOESS window'), 5, 0) kernelSizeValid = QDoubleValidator(self.controlArea) kernelSizeValid.setRange(0,1,3) glay.addWidget(OWGUI.lineEdit(box, self, 'windowProportion', 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).', valueType = float, validator = kernelSizeValid), 5, 2) glay.addWidget(OWGUI.widgetLabel(box, 'LOESS sample points'), 6, 0) pointsValid = QIntValidator(20, 1000, self.controlArea) glay.addWidget(OWGUI.lineEdit(box, self, 'loessPoints', tooltip='Number of points in computation of LOESS (20-1000).', valueType = int, validator = pointsValid), 6, 2) OWGUI.separator(self.controlArea) OWGUI.checkBox(self.controlArea, self, "adjustThreshold", "Adjust threshold (for binary classes)", box = "Threshold") OWGUI.separator(self.controlArea) # box = OWGUI.widgetBox(self.controlArea, "Apply", orientation=1) applyButton = OWGUI.button(self.controlArea, self, "&Apply", callback=self.applyLearner, default=True) OWGUI.rubber(self.controlArea) self.refreshControls() self.applyLearner()
print(m.coeff_names[i][0], ':') for x in m.coeff_names[i][1:]: print('\t', x, '=', vector[j]) j += 1 print("beta:", -m.beta) #t = orange.ExampleTable('c:/proj/domains/voting.tab') # discrete t = orange.ExampleTable( r"E:\Development\Orange Datasets\UCI\shuttle.tab") # discrete #t = orange.ExampleTable('c_cmc.tab') # continuous print("NAIVE BAYES") print("===========") bl = orange.BayesLearner() bl.estimatorConstructor = orange.ProbabilityEstimatorConstructor_Laplace() # prevent too many estimation points # increase the smoothing level bl.conditionalEstimatorConstructorContinuous = orange.ConditionalProbabilityEstimatorConstructor_loess( windowProportion=0.5, nPoints=10) c = bl(t) printmodel(t, c, printexamples=0) print("\n\nLOGISTIC REGRESSION") print("===================") c = orngLR_Jakulin.BasicLogisticLearner()(t) printmodel(t, c, printexamples=0) print("\n\nLINEAR SVM") print("==========") l = orngSVM.BasicSVMLearner()
def test_Laplace(self): pec = orange.ProbabilityEstimatorConstructor_Laplace() pe = pec([22, 5, 2]) self.assertEqual(list(pe()), [23 / 32, 6 / 32, 3 / 32])