source: orange/docs/reference/rst/Orange.classification.rst @ 9708:418740ae829c

Revision 9708:418740ae829c, 4.3 KB checked in by Jure Zbontar <jure.zbontar@…>, 2 years ago (diff)

Started work on porting Classifiers in Python.

Line 
1.. automodule:: Orange.classification
2
3###################################
4Classification (``classification``)
5###################################
6
7All classifiers in Orange consist of two parts, a Learner and a Classifier. A
8learner is constructed with all parameters that will be used for learning.
9When a data table is passed to its __call__ method, a model is fitted to the
10data and return in a form of a Classifier, which is then used for predicting
11the dependent variable(s) of new instances.
12
13.. class:: Learner()
14
15    Base class for all orange learners.
16
17    .. method:: __call__(instances)
18
19        Fit a model and return it as an instance of :class:`Classifier`.
20
21        This method is abstract and needs to be implemented on each learner.
22
23.. class:: Classifier()
24
25    Base class for all orange classifiers.
26
27    .. attribute:: GetValue
28
29        Return value of the target class when performing prediction.
30
31    .. attribute:: GetProbabilities
32
33        Return probability of each target class when performing prediction.
34
35    .. attribute:: GetBoth
36
37        Return a tuple of target class value and probabilities for each class.
38
39
40    .. method:: __call__(instances, return_type)
41
42        Classify a new instance using this model.
43
44        This method is abstract and needs to be implemented on each classifier.
45
46        :param instance: data instance to be classified.
47        :type instance: :class:`~Orange.data.Instance`
48
49        :param return_type: what needs to be predicted
50        :type return_type: :obj:`GetBoth`,
51                           :obj:`GetValue`,
52                           :obj:`GetProbabilities`
53
54        :rtype: :class:`~Orange.data.Value`,
55              :class:`~Orange.statistics.distribution.Distribution` or a
56              tuple with both
57
58You can often program learners and classifiers as classes or functions
59written entirely in Python and independent from Orange, as shown in
60Orange for Beginners. Such classes can participate, for instance, in
61the common evaluation functions like those available in modules orngTest
62and orngStat.
63
64On the other hand, these classes can't be used as components for pure C++
65classes. For instance, TreeLearner's attribute nodeLearner should contain
66a (wrapped) C++ object derived from Learner, such as MajorityLearner
67or BayesLearner, and Variables's getValueFrom can only store classes
68derived from Classifier, like for instance ClassifierFromVar. They cannot
69accommodate Python's classes or even functions.
70
71There's a workaround, though. You can subtype Orange classes Learner
72or Classifier as if the two classes were defined in Python, but later
73use your derived Python classes as if they were written in Orange's
74core. That is, you can define your class in a Python script like this:
75
76    class MyLearner(orange.Learner):
77        def __call__(self, examples, weightID = 0):
78            <do something smart here>
79
80Such a learner can then be used as any regular learner written in
81Orange. You can, for instance, construct a tree learner and use your
82learner to learn node classifier:
83
84    treeLearner = orange.TreeLearner()
85    treeLearner.nodeLearner = MyLearner()
86
87If your learner or classifier is simple enough, you even don't need
88to derive a class yourself. You can define the learner or classifier
89as an ordinary Python function and assign it to an attribute of Orange
90class that would expect a Learner or a Classifier. Wrapping into a class
91derived from Learner or Classifier is done by Orange. ::
92
93    def myLearner(examples, weightID = 0):
94        <do something less smart here>
95   
96    treeLearner = orange.TreeLearner()
97    treeLearner.nodeLearner = myLearner
98
99Finally, if your learner is really simple (that is, trivial :-), you
100can even stuff it into a lambda function. ::
101
102    treeLearner = orange.TreeLearner()
103    treeLearner.nodeLearner = lambda examples, weightID = 0: <do something trivial>
104
105Detailed description of the mechanisms involved and example scripts are
106given in a separate documentation on subtyping Orange classes in Python.
107
108Orange contains implementations of various classifiers that are described in
109detail on separate pages.
110
111.. toctree::
112   :maxdepth: 2
113
114   Orange.classification.bayes
115   Orange.classification.knn
116   Orange.classification.logreg
117   Orange.classification.lookup
118   Orange.classification.majority
119   Orange.classification.rules
120   Orange.classification.svm
121   Orange.classification.tree
Note: See TracBrowser for help on using the repository browser.