Changeset 7534:287405691042 in orange
 Timestamp:
 02/04/11 21:14:41 (3 years ago)
 Branch:
 default
 Convert:
 0d225967ebec449806ed88da5d1b4533519b17e8
 Location:
 orange
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/classification/bayes.py
r7498 r7534 1 1 """ 2 2 .. index:: naive Bayes classifier 3 3 4 4 .. index:: 5 5 single: classification; naive Bayes classifier 6 6 7 ====================== 7 ********************** 8 8 Naive Bayes Classifier 9 ====================== 9 ********************** 10 10 11 11 The most primitive bayesian classifier is :obj:`NaiveLearner` 12 12 (http://en.wikipedia.org/wiki/Naive_Bayes_classifier). 13 13 The class estimates conditional probabilities from training data and uses them 14 for classification of new examples.14 for classification of new data instances. 15 15 16 16 Example (`bayesrun.py`_, uses `iris.tab`_) … … 33 33 mestimate. 34 34 35 Example (`bayesmestimate.py`_, uses ` iris.tab`_)35 Example (`bayesmestimate.py`_, uses `lenses.tab`_) 36 36 37 37 .. literalinclude:: code/bayesmestimate.py 38 38 :lines: 7 39 39 40 Observing probabilities shows a shift towards the second class  41 as compared to probabilities above, where relative frequencies were used. 42 Note that the change in error estimation did not have any effect on apriori 40 Observing conditional probabilities in an mestimate based classifier shows a 41 shift towards the second class  as compared to probabilities above, where 42 relative frequencies were used. Note that the change in error estimation did 43 not have any effect on apriori 43 44 probabilities. 44 45 … … 48 49 :lines: 7 49 50 50 Setting adjustThreshold paramater can sometimes improve the results. :: 51 Setting adjustThreshold parameter can sometimes improve the results. Those are 52 the classification accuracies of 10fold crossvalidation of a normal naive 53 bayesian classifier, and one with an adjusted threshold:: 51 54 52 55 [0.7901746265516516, 0.8280138859667578] … … 57 60 .. _adultsample.tab: code/adultsample.tab 58 61 .. _iris.tab: code/iris.tab 59 60 ====================== 62 .. _lenses.tab: code/lenses.tab 63 61 64 Implementation Details 62 65 ====================== 63 66 67 The following two classes are implemented in C++ (*bayes.cpp*). They are not 68 intended to be used directly. Here we give implementation details for those 69 interested. 70 64 71 Orange.core.BayesLearner 65 ======================== 66 The first three fields are empty (None) by default. 72  73 Fields estimatorConstructor, conditionalEstimatorConstructor and 74 conditionalEstimatorConstructorContinuous are empty (None) by default. 67 75 68 76 If estimatorConstructor is left undefined, p(C) will be estimated by relative … … 70 78 When conditionalEstimatorConstructor is left undefined, it will use the same 71 79 constructor as for estimating unconditional probabilities (estimatorConstructor 72 is used as an estimator in (ConditionalProbabilityEstimatorConstructor_ByRows).80 is used as an estimator in ConditionalProbabilityEstimatorConstructor_ByRows). 73 81 That is, by default, both will use relative frequencies. But when 74 82 estimatorConstructor is set to, for instance, estimate probabilities by 75 mestimate with m=2.0, mestimates with m=2.0will be used for estimation of83 mestimate with m=2.0, the same estimator will be used for estimation of 76 84 conditional probabilities, too. 77 P(cvi) for continuous attributes are, by default estimated with loess (a85 P(cvi) for continuous attributes are, by default, estimated with loess (a 78 86 variant of locally weighted linear regression), using 79 87 ConditionalProbabilityEstimatorConstructor_loess. … … 96 104 97 105 Orange.core.BayesClassifier 98 =========================== 99 Class NaiveClassifier represents a naive Bayesian classifier. Probability of106  107 Class NaiveClassifier represents a naive bayesian classifier. Probability of 100 108 class C, knowing that values of features :math:`F_1, F_2, ..., F_n` are 101 109 :math:`v_1, v_2, ..., v_n`, is computed as :math:`p(Cv_1, v_2, ..., v_n) = \ … … 110 118 mestimate. In this case, the above formula is much more appropriate. 111 119 112 When computing the formula, probabilities p(C) are read from distribution which113 is of type Distribution and stores a (normalized) probability of each class.120 When computing the formula, probabilities p(C) are read from distribution, which 121 is of type Distribution, and stores a (normalized) probability of each class. 114 122 When distribution is None, BayesClassifier calls estimator to assess the 115 123 probability. The former method is faster and is actually used by all existing … … 132 140 conditionalEstimators) can be None. For instance, BayesLearner normally 133 141 constructs a classifier which has either distribution or estimator defined. 134 While it is not an error ,to have both, only distribution will be used in that142 While it is not an error to have both, only distribution will be used in that 135 143 case. As for the other two fields, they can be both defined and used 136 144 complementarily; the elements which are missing in one are defined in the … … 156 164 is called and the resulting classifier is returned instead of the learner. 157 165 158 :param adjustTreshold: sets the corresponding attribute 159 :type adjustTreshold: boolean 160 :param m: sets the estimatorConstructor to \ 161 :class:`orange.ProbabilityEstimatorConstructor_m` with specified m 162 :type m: integer 163 :param estimatorConstructor: sets the corresponding attribute 164 :type estimatorConstructor: orange.ProbabilityEstimatorConstructor 165 :param conditionalEstimatorConstructor: sets the corresponding attribute 166 :type conditionalEstimatorConstructor: 167 :class:`orange.ConditionalProbabilityEstimatorConstructor` 168 :param conditionalEstimatorConstructorContinuous: sets the corresponding 169 attribute 170 :type conditionalEstimatorConstructorContinuous: 171 :class:`orange.ConditionalProbabilityEstimatorConstructor` 172 :rtype: :class:`Orange.classification.bayes.NaiveBayesLearner` or 173 :class:`Orange.classification.bayes.NaiveBayesClassifier` 174 175 All attributes can also be set as constructor parameters. 166 .. 167 :param adjustTreshold: sets the corresponding attribute 168 :type adjustTreshold: boolean 169 :param m: sets the :obj:`estimatorConstructor` to 170 :class:`orange.ProbabilityEstimatorConstructor_m` with specified m 171 :type m: integer 172 :param estimatorConstructor: sets the corresponding attribute 173 :type estimatorConstructor: orange.ProbabilityEstimatorConstructor 174 :param conditionalEstimatorConstructor: sets the corresponding attribute 175 :type conditionalEstimatorConstructor: 176 :class:`orange.ConditionalProbabilityEstimatorConstructor` 177 :param conditionalEstimatorConstructorContinuous: sets the corresponding 178 attribute 179 :type conditionalEstimatorConstructorContinuous: 180 :class:`orange.ConditionalProbabilityEstimatorConstructor` 181 182 :rtype: :class:`Orange.classification.bayes.NaiveLearner` or 183 :class:`Orange.classification.bayes.NaiveClassifier` 184 185 Constructor parameters set the corresponding attributes. 176 186 177 187 .. attribute:: adjustTreshold … … 186 196 187 197 m for mestimate. If set, mestimation of probabilities 188 will be used using :class:`orange.ProbabilityEstimatorConstructor_m` 198 will be used using :class:`orange.ProbabilityEstimatorConstructor_m`. 189 199 This attribute is ignored if you also set estimatorConstructor. 190 200 … … 193 203 Probability estimator constructor for 194 204 prior class probabilities. Defaults to 195 :class:`orange.ProbabilityEstimatorConstructor_relative` 205 :class:`orange.ProbabilityEstimatorConstructor_relative`. 196 206 Setting this attribute disables the above described attribute m. 197 207 … … 206 216 Probability estimator constructor for conditional probabilities for 207 217 continuous features. Defaults to 208 :class:`orange.ConditionalProbabilityEstimatorConstructor_loess` 218 :class:`orange.ConditionalProbabilityEstimatorConstructor_loess`. 209 219 """ 210 220 … … 280 290 .. attribute:: conditionalEstimators 281 291 282 A list of estimators for conditional probabilities 292 A list of estimators for conditional probabilities. 283 293 284 294 .. attribute:: adjustThreshold … … 298 308 def __call__(self, instance, result_type=Orange.classification.Classifier.GetValue, 299 309 *args, **kwdargs): 300 """Classify a new instance 301 302 :param instance: instance to be classifie r310 """Classify a new instance. 311 312 :param instance: instance to be classified. 303 313 :type instance: :class:`Orange.data.Instance` 304 314 :param result_type: :class:`Orange.classification.Classifier.GetValue` or \ … … 320 330 321 331 def p(self, class_, instance): 322 """Return probability of single class 332 """ 333 Return probability of a single class. 323 334 Probability is not normalized and can be different from probability 324 returned from __call__ 335 returned from __call__. 325 336 326 337 :param class_: class variable for which the probability should be 327 output ed338 output. 328 339 :type class_: :class:`Orange.data.Variable` 329 :param instance: instance to be classified 340 :param instance: instance to be classified. 330 341 :type instance: :class:`Orange.data.Instance` 331 342 … … 334 345 335 346 def printModel(self): 336 """Print classifi cator in human friendly format"""347 """Print classifier in human friendly format.""" 337 348 nValues=len(self.classVar.values) 338 349 frmtStr=' %10.3f'*nValues 
orange/doc/Orange/rst/code/bayesmestimate.py
r7477 r7534 15 15 16 16 print bayes.conditionalDistributions 17 """<<'prepresbyopic': <0.625, 0.125, 0.250>, 'presbyopic': <0.750, 0.125, 0.125>, ...>>""" 17 # prints: <<'prepresbyopic': <0.625, 0.125, 0.250>, 'presbyopic': <0.750, 0.125, 0.125>, ...>> 18 18 print bayesWithM.conditionalDistributions 19 """<<'prepresbyopic': <0.625, 0.133, 0.242>, 'presbyopic': <0.725, 0.133, 0.142>, ...>>""" 19 # prints: <<'prepresbyopic': <0.625, 0.133, 0.242>, 'presbyopic': <0.725, 0.133, 0.142>, ...>> 20 20 21 21 print bayes.distribution 22 """<0.625, 0.167, 0.208>""" 22 # prints: <0.625, 0.167, 0.208> 23 23 print bayesWithM.distribution 24 """<0.625, 0.167, 0.208>""" 24 # prints: <0.625, 0.167, 0.208>
Note: See TracChangeset
for help on using the changeset viewer.