Changeset 8862:8870cc76ab29 in orange
 Timestamp:
 08/31/11 13:50:43 (3 years ago)
 Branch:
 default
 Convert:
 e4e5eda5768b1accf93c942de9989e069de508b2
 Location:
 orange
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/classification/bayes.py
r8107 r8862 1 1 import Orange 2 import Orange.core 2 3 from Orange.core import BayesClassifier as _BayesClassifier 3 4 from Orange.core import BayesLearner as _BayesLearner … … 10 11 If data instances are provided to the constructor, the learning algorithm 11 12 is called and the resulting classifier is returned instead of the learner. 12 13 ..14 :param adjust_threshold: sets the corresponding attribute15 :type adjust_threshold: boolean16 :param m: sets the :obj:`estimatorConstructor` to17 :class:`orange.ProbabilityEstimatorConstructor_m` with specified m18 :type m: integer19 :param estimator_constructor: sets the corresponding attribute20 :type estimator_constructor: orange.ProbabilityEstimatorConstructor21 :param conditional_estimator_constructor: sets the corresponding attribute22 :type conditional_estimator_constructor:23 :class:`orange.ConditionalProbabilityEstimatorConstructor`24 :param conditional_estimator_constructor_continuous: sets the corresponding25 attribute26 :type conditional_estimator_constructor_continuous:27 :class:`orange.ConditionalProbabilityEstimatorConstructor`28 13 29 14 :rtype: :class:`Orange.classification.bayes.NaiveLearner` or … … 43 28 44 29 m for mestimate. If set, mestimation of probabilities 45 will be used using :class:` orange.ProbabilityEstimatorConstructor_m`.46 This attribute is ignored if you also set estimator Constructor.30 will be used using :class:`Orange.statistics.estimate.ProbabilityEstimatorConstructor_m`. 31 This attribute is ignored if you also set estimator_constructor. 47 32 48 33 .. attribute:: estimator_constructor … … 50 35 Probability estimator constructor for 51 36 prior class probabilities. Defaults to 52 :class:` orange.ProbabilityEstimatorConstructor_relative`.37 :class:`Orange.statistics.estimate.ProbabilityEstimatorConstructor_relative`. 53 38 Setting this attribute disables the above described attribute m. 54 39 … … 63 48 Probability estimator constructor for conditional probabilities for 64 49 continuous features. Defaults to 65 :class:` orange.ConditionalProbabilityEstimatorConstructor_loess`.66 """ 67 68 def __new__(cls, instances= None, weight_id = 0, **argkw):50 :class:`Orange.statistics.estimate.ProbabilityEstimatorConstructor_loess`. 51 """ 52 53 def __new__(cls, data = None, weight_id = 0, **argkw): 69 54 self = Orange.classification.Learner.__new__(cls, **argkw) 70 if instances:55 if data: 71 56 self.__init__(**argkw) 72 return self.__call__( instances, weight_id)57 return self.__call__(data, weight_id) 73 58 else: 74 59 return self … … 84 69 self.__dict__.update(argkw) 85 70 86 def __call__(self, instances, weight=0):71 def __call__(self, data, weight=0): 87 72 """Learn from the given table of data instances. 88 73 89 :param instances: Data instances to learn from.90 :type instances: Orange.data.Table74 :param data: Data instances to learn from. 75 :type data: Orange.data.Table 91 76 :param weight: Id of meta attribute with weights of instances 92 :type weight: int eger93 :rtype: :class:`Orange.classification.bayes.Naive BayesClassifier`77 :type weight: int 78 :rtype: :class:`Orange.classification.bayes.NaiveClassifier` 94 79 """ 95 80 bayes = _BayesLearner() … … 112 97 if self.adjust_threshold: 113 98 bayes.adjust_threshold = self.adjust_threshold 114 return NaiveClassifier(bayes( instances, weight))99 return NaiveClassifier(bayes(data, weight)) 115 100 NaiveLearner = Orange.misc.deprecated_members( 116 101 { "adjustThreshold": "adjust_threshold", … … 172 157 173 158 :rtype: :class:`Orange.data.Value`, 174 :class:`Orange.statistics. Distribution` or a tuple with both159 :class:`Orange.statistics.distribution.Distribution` or a tuple with both 175 160 """ 176 161 return self.native_bayes_classifier(instance, result_type, *args, **kwdargs) … … 190 175 returned from __call__. 191 176 192 :param class_: class va riable for which the probability should be177 :param class_: class value for which the probability should be 193 178 output. 194 :type class_: :class:`Orange.data.Va riable`179 :type class_: :class:`Orange.data.Value` 195 180 :param instance: instance to be classified. 196 181 :type instance: :class:`Orange.data.Instance` 
orange/doc/Orange/rst/Orange.classification.bayes.rst
r8170 r8862 10 10 ********************************** 11 11 12 The most primitive Bayesian classifier is :obj:`NaiveLearner`. 13 `Naive Bayes classification algorithm <http://en.wikipedia.org/wiki/Naive_Bayes_classifier>`_ 14 estimates conditional probabilities from training data and uses them 15 for classification of new data instances. The algorithm learns very fast if all features 16 in the training data set are discrete. If a number of features are continues, though, the 17 algorithm runs slower due to time spent to estimate continuous conditional distributions. 12 A `Naive Bayes classifier <http://en.wikipedia.org/wiki/Naive_Bayes_classifier>`_ 13 is a simple probabilistic classifier that estimates conditional probabilities of the dependant variable 14 from training data and uses them for classification of new data instances. The algorithm is very 15 fast if all features in the training data set are discrete. If a number of features are continuous, 16 though, the algorithm runs slower due to time spent to estimate continuous conditional distributions. 18 17 19 18 The following example demonstrates a straightforward invocation of … … 58 57 59 58 Probabilities for continuous features are estimated with \ 60 :class:` ProbabilityEstimatorConstructor_loess`.59 :class:`Orange.statistics.estimate.ProbabilityEstimatorConstructor_loess`. 61 60 (`bayesplotiris.py`_, uses `iris.tab`_): 62 61 … … 81 80 .. _titanic.tab: code/iris.tab 82 81 .. _lenses.tab: code/lenses.tab 83 84 Implementation details85 ======================86 87 The following two classes are implemented in C++ (*bayes.cpp*). They are not88 intended to be used directly. Here we provide implementation details for those89 interested.90 91 Orange.core.BayesLearner92 93 Fields estimatorConstructor, conditionalEstimatorConstructor and94 conditionalEstimatorConstructorContinuous are empty (None) by default.95 96 If estimatorConstructor is left undefined, p(C) will be estimated by relative97 frequencies of examples (see ProbabilityEstimatorConstructor_relative).98 When conditionalEstimatorConstructor is left undefined, it will use the same99 constructor as for estimating unconditional probabilities (estimatorConstructor100 is used as an estimator in ConditionalProbabilityEstimatorConstructor_ByRows).101 That is, by default, both will use relative frequencies. But when102 estimatorConstructor is set to, for instance, estimate probabilities by103 mestimate with m=2.0, the same estimator will be used for estimation of104 conditional probabilities, too.105 P(cvi) for continuous attributes are, by default, estimated with loess (a106 variant of locally weighted linear regression), using107 ConditionalProbabilityEstimatorConstructor_loess.108 The learner first constructs an estimator for p(C). It tries to get a109 precomputed distribution of probabilities; if the estimator is capable of110 returning it, the distribution is stored in the classifier's field distribution111 and the just constructed estimator is disposed. Otherwise, the estimator is112 stored in the classifier's field estimator, while the distribution is left113 empty.114 115 The same is then done for conditional probabilities. Different constructors are116 used for discrete and continuous attributes. If the constructed estimator can117 return all conditional probabilities in form of Contingency, the contingency is118 stored and the estimator disposed. If not, the estimator is stored. If there119 are no contingencies when the learning is finished, the resulting classifier's120 conditionalDistributions is None. Alternatively, if all probabilities are121 stored as contingencies, the conditionalEstimators fields is None.122 123 Field normalizePredictions is copied to the resulting classifier.124 125 Orange.core.BayesClassifier126 127 Class NaiveClassifier represents a naive bayesian classifier. Probability of128 class C, knowing that values of features :math:`F_1, F_2, ..., F_n` are129 :math:`v_1, v_2, ..., v_n`, is computed as :math:`p(Cv_1, v_2, ..., v_n) = \130 p(C) \\cdot \\frac{p(Cv_1)}{p(C)} \\cdot \\frac{p(Cv_2)}{p(C)} \\cdot ... \131 \\cdot \\frac{p(Cv_n)}{p(C)}`.132 133 Note that when relative frequencies are used to estimate probabilities, the134 more usual formula (with factors of form :math:`\\frac{p(v_iC)}{p(v_i)}`) and135 the above formula are exactly equivalent (without any additional assumptions of136 independency, as one could think at a first glance). The difference becomes137 important when using other ways to estimate probabilities, like, for instance,138 mestimate. In this case, the above formula is much more appropriate.139 140 When computing the formula, probabilities p(C) are read from distribution, which141 is of type Distribution, and stores a (normalized) probability of each class.142 When distribution is None, BayesClassifier calls estimator to assess the143 probability. The former method is faster and is actually used by all existing144 methods of probability estimation. The latter is more flexible.145 146 Conditional probabilities are computed similarly. Field conditionalDistribution147 is of type DomainContingency which is basically a list of instances of148 Contingency, one for each attribute; the outer variable of the contingency is149 the attribute and the inner is the class. Contingency can be seen as a list of150 normalized probability distributions. For attributes for which there is no151 contingency in conditionalDistribution a corresponding estimator in152 conditionalEstimators is used. The estimator is given the attribute value and153 returns distributions of classes.154 155 If neither, nor precomputed contingency nor conditional estimator exist, the156 attribute is ignored without issuing any warning. The attribute is also ignored157 if its value is undefined; this cannot be overriden by estimators.158 159 Any field (distribution, estimator, conditionalDistributions,160 conditionalEstimators) can be None. For instance, BayesLearner normally161 constructs a classifier which has either distribution or estimator defined.162 While it is not an error to have both, only distribution will be used in that163 case. As for the other two fields, they can be both defined and used164 complementarily; the elements which are missing in one are defined in the165 other. However, if there is no need for estimators, BayesLearner will not166 construct an empty list; it will not construct a list at all, but leave the167 field conditionalEstimators empty.168 169 If you only need probabilities of individual class call BayesClassifier's170 method p(class, example) to compute the probability of this class only. Note171 that this probability will not be normalized and will thus, in general, not172 equal the probability returned by the call operator. 
orange/doc/Orange/rst/code/bayesrun.py
r8042 r8862 6 6 7 7 import Orange 8 t able= Orange.data.Table("titanic.tab")8 titanic = Orange.data.Table("titanic.tab") 9 9 10 10 learner = Orange.classification.bayes.NaiveLearner() 11 classifier = learner(t able)11 classifier = learner(titanic) 12 12 13 13 for ex in table[:5]:
Note: See TracChangeset
for help on using the changeset viewer.