# Changeset 8862:8870cc76ab29 in orange

Ignore:
Timestamp:
08/31/11 13:50:43 (3 years ago)
Branch:
default
Convert:
e4e5eda5768b1accf93c942de9989e069de508b2
Message:

Fixed links in documentation and did some minor corrections.
Closes #933

Location:
orange
Files:
3 edited

Unmodified
Added
Removed
• ## orange/Orange/classification/bayes.py

 r8107 import Orange import Orange.core from Orange.core import BayesClassifier as _BayesClassifier from Orange.core import BayesLearner as _BayesLearner If data instances are provided to the constructor, the learning algorithm is called and the resulting classifier is returned instead of the learner. .. :param adjust_threshold: sets the corresponding attribute :type adjust_threshold: boolean :param m: sets the :obj:estimatorConstructor to :class:orange.ProbabilityEstimatorConstructor_m with specified m :type m: integer :param estimator_constructor: sets the corresponding attribute :type estimator_constructor: orange.ProbabilityEstimatorConstructor :param conditional_estimator_constructor: sets the corresponding attribute :type conditional_estimator_constructor: :class:orange.ConditionalProbabilityEstimatorConstructor :param conditional_estimator_constructor_continuous: sets the corresponding attribute :type conditional_estimator_constructor_continuous: :class:orange.ConditionalProbabilityEstimatorConstructor :rtype: :class:Orange.classification.bayes.NaiveLearner or m for m-estimate. If set, m-estimation of probabilities will be used using :class:orange.ProbabilityEstimatorConstructor_m. This attribute is ignored if you also set estimatorConstructor. will be used using :class:Orange.statistics.estimate.ProbabilityEstimatorConstructor_m. This attribute is ignored if you also set estimator_constructor. .. attribute:: estimator_constructor Probability estimator constructor for prior class probabilities. Defaults to :class:orange.ProbabilityEstimatorConstructor_relative. :class:Orange.statistics.estimate.ProbabilityEstimatorConstructor_relative. Setting this attribute disables the above described attribute m. Probability estimator constructor for conditional probabilities for continuous features. Defaults to :class:orange.ConditionalProbabilityEstimatorConstructor_loess. """ def __new__(cls, instances = None, weight_id = 0, **argkw): :class:Orange.statistics.estimate.ProbabilityEstimatorConstructor_loess. """ def __new__(cls, data = None, weight_id = 0, **argkw): self = Orange.classification.Learner.__new__(cls, **argkw) if instances: if data: self.__init__(**argkw) return self.__call__(instances, weight_id) return self.__call__(data, weight_id) else: return self self.__dict__.update(argkw) def __call__(self, instances, weight=0): def __call__(self, data, weight=0): """Learn from the given table of data instances. :param instances: Data instances to learn from. :type instances: Orange.data.Table :param data: Data instances to learn from. :type data: Orange.data.Table :param weight: Id of meta attribute with weights of instances :type weight: integer :rtype: :class:Orange.classification.bayes.NaiveBayesClassifier :type weight: int :rtype: :class:Orange.classification.bayes.NaiveClassifier """ bayes = _BayesLearner() if self.adjust_threshold: bayes.adjust_threshold = self.adjust_threshold return NaiveClassifier(bayes(instances, weight)) return NaiveClassifier(bayes(data, weight)) NaiveLearner = Orange.misc.deprecated_members( {     "adjustThreshold": "adjust_threshold", :rtype: :class:Orange.data.Value, :class:Orange.statistics.Distribution or a tuple with both :class:Orange.statistics.distribution.Distribution or a tuple with both """ return self.native_bayes_classifier(instance, result_type, *args, **kwdargs) returned from __call__. :param class_: class variable for which the probability should be :param class_: class value for which the probability should be output. :type class_: :class:Orange.data.Variable :type class_: :class:Orange.data.Value :param instance: instance to be classified. :type instance: :class:Orange.data.Instance
• ## orange/doc/Orange/rst/Orange.classification.bayes.rst

 r8170 ********************************** The most primitive Bayesian classifier is :obj:NaiveLearner. Naive Bayes classification algorithm _ estimates conditional probabilities from training data and uses them for classification of new data instances. The algorithm learns very fast if all features in the training data set are discrete. If a number of features are continues, though, the algorithm runs slower due to time spent to estimate continuous conditional distributions. A Naive Bayes classifier _ is a simple probabilistic classifier that estimates conditional probabilities of the dependant variable from training data and uses them for classification of new data instances. The algorithm is very fast if all features in the training data set are discrete. If a number of features are continuous, though, the algorithm runs slower due to time spent to estimate continuous conditional distributions. The following example demonstrates a straightforward invocation of Probabilities for continuous features are estimated with \ :class:ProbabilityEstimatorConstructor_loess. :class:Orange.statistics.estimate.ProbabilityEstimatorConstructor_loess. (bayes-plot-iris.py_, uses iris.tab_): .. _titanic.tab: code/iris.tab .. _lenses.tab: code/lenses.tab Implementation details ====================== The following two classes are implemented in C++ (*bayes.cpp*). They are not intended to be used directly. Here we provide implementation details for those interested. Orange.core.BayesLearner ------------------------ Fields estimatorConstructor, conditionalEstimatorConstructor and conditionalEstimatorConstructorContinuous are empty (None) by default. If estimatorConstructor is left undefined, p(C) will be estimated by relative frequencies of examples (see ProbabilityEstimatorConstructor_relative). When conditionalEstimatorConstructor is left undefined, it will use the same constructor as for estimating unconditional probabilities (estimatorConstructor is used as an estimator in ConditionalProbabilityEstimatorConstructor_ByRows). That is, by default, both will use relative frequencies. But when estimatorConstructor is set to, for instance, estimate probabilities by m-estimate with m=2.0, the same estimator will be used for estimation of conditional probabilities, too. P(c|vi) for continuous attributes are, by default, estimated with loess (a variant of locally weighted linear regression), using ConditionalProbabilityEstimatorConstructor_loess. The learner first constructs an estimator for p(C). It tries to get a precomputed distribution of probabilities; if the estimator is capable of returning it, the distribution is stored in the classifier's field distribution and the just constructed estimator is disposed. Otherwise, the estimator is stored in the classifier's field estimator, while the distribution is left empty. The same is then done for conditional probabilities. Different constructors are used for discrete and continuous attributes. If the constructed estimator can return all conditional probabilities in form of Contingency, the contingency is stored and the estimator disposed. If not, the estimator is stored. If there are no contingencies when the learning is finished, the resulting classifier's conditionalDistributions is None. Alternatively, if all probabilities are stored as contingencies, the conditionalEstimators fields is None. Field normalizePredictions is copied to the resulting classifier. Orange.core.BayesClassifier --------------------------- Class NaiveClassifier represents a naive bayesian classifier. Probability of class C, knowing that values of features :math:F_1, F_2, ..., F_n are :math:v_1, v_2, ..., v_n, is computed as :math:p(C|v_1, v_2, ..., v_n) = \ p(C) \\cdot \\frac{p(C|v_1)}{p(C)} \\cdot \\frac{p(C|v_2)}{p(C)} \\cdot ... \ \\cdot \\frac{p(C|v_n)}{p(C)}. Note that when relative frequencies are used to estimate probabilities, the more usual formula (with factors of form :math:\\frac{p(v_i|C)}{p(v_i)}) and the above formula are exactly equivalent (without any additional assumptions of independency, as one could think at a first glance). The difference becomes important when using other ways to estimate probabilities, like, for instance, m-estimate. In this case, the above formula is much more appropriate. When computing the formula, probabilities p(C) are read from distribution, which is of type Distribution, and stores a (normalized) probability of each class. When distribution is None, BayesClassifier calls estimator to assess the probability. The former method is faster and is actually used by all existing methods of probability estimation. The latter is more flexible. Conditional probabilities are computed similarly. Field conditionalDistribution is of type DomainContingency which is basically a list of instances of Contingency, one for each attribute; the outer variable of the contingency is the attribute and the inner is the class. Contingency can be seen as a list of normalized probability distributions. For attributes for which there is no contingency in conditionalDistribution a corresponding estimator in conditionalEstimators is used. The estimator is given the attribute value and returns distributions of classes. If neither, nor pre-computed contingency nor conditional estimator exist, the attribute is ignored without issuing any warning. The attribute is also ignored if its value is undefined; this cannot be overriden by estimators. Any field (distribution, estimator, conditionalDistributions, conditionalEstimators) can be None. For instance, BayesLearner normally constructs a classifier which has either distribution or estimator defined. While it is not an error to have both, only distribution will be used in that case. As for the other two fields, they can be both defined and used complementarily; the elements which are missing in one are defined in the other. However, if there is no need for estimators, BayesLearner will not construct an empty list; it will not construct a list at all, but leave the field conditionalEstimators empty. If you only need probabilities of individual class call BayesClassifier's method p(class, example) to compute the probability of this class only. Note that this probability will not be normalized and will thus, in general, not equal the probability returned by the call operator.
• ## orange/doc/Orange/rst/code/bayes-run.py

 r8042 import Orange table = Orange.data.Table("titanic.tab") titanic = Orange.data.Table("titanic.tab") learner = Orange.classification.bayes.NaiveLearner() classifier = learner(table) classifier = learner(titanic) for ex in table[:5]:
Note: See TracChangeset for help on using the changeset viewer.