def createInstance(self): bayes = orange.BayesLearner() if hasattr(self, "estimatorConstructor"): bayes.estimatorConstructor = self.estimatorConstructor if hasattr(self, "m"): if hasattr(bayes.estimatorConstructor, "m"): raise AttributeError( "invalid combination of attributes: 'estimatorConstructor' does not expect 'm'" ) else: self.estimatorConstructor.m = self.m elif hasattr(self, "m"): bayes.estimatorConstructor = orange.ProbabilityEstimatorConstructor_m( m=self.m) if hasattr(self, "conditionalEstimatorConstructor"): bayes.conditionalEstimatorConstructor = self.conditionalEstimatorConstructor elif bayes.estimatorConstructor: bayes.conditionalEstimatorConstructor = orange.ConditionalProbabilityEstimatorConstructor_ByRows( ) bayes.conditionalEstimatorConstructor.estimatorConstructor = bayes.estimatorConstructor if hasattr(self, "conditionalEstimatorConstructorContinuous"): bayes.conditionalEstimatorConstructorContinuous = self.conditionalEstimatorConstructorContinuous return bayes
def test_m(self): pec = orange.ProbabilityEstimatorConstructor_m(m=3) pe = pec([22, 5, 2], [1, 1, 1]) self.assertEqual(list(pe()), [23 / 32, 6 / 32, 3 / 32]) pe = pec([22, 5, 2], [6, 2, 2]) self.assertEqual(list(pe()), [(22 + .6 * 3) / 32, (5 + .2 * 3) / 32, (2 + .2 * 3) / 32]) self.assertRaises(ValueError, pec, [22, 5, 2])
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()
# Description: Shows how to use probability estimators with measure of attribute quality # Category: attribute quality # Classes: MeasureAttribute, MeasureAttribute_info, ProbabilityEstimatorConstructor_m, ConditionalProbabilityEstimatorConstructor_ByRows # Uses: lenses # Referenced: MeasureAttribute.htm import orange data = orange.ExampleTable("lenses") ms = (0, 2, 5, 10, 20) measures = [] for m in ms: meas = orange.MeasureAttribute_info() meas.estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m=m) meas.conditionalEstimatorConstructor = orange.ConditionalProbabilityEstimatorConstructor_ByRows( ) meas.conditionalEstimatorConstructor.estimatorConstructor = meas.estimatorConstructor measures.append(meas) print "%15s\t%5i\t%5i\t%5i\t%5i\t%5i\t" % (("attr", ) + ms) for attr in data.domain.attributes: print "%15s\t%5.3f\t%5.3f\t%5.3f\t%5.3f\t%5.3f" % ( (attr.name, ) + tuple([meas(attr, data) for meas in measures]))