source: orange/Orange/multitarget/__init__.py @ 9994:1073e0304a87

Revision 9994:1073e0304a87, 4.1 KB checked in by Matija Polajnar <matija.polajnar@…>, 2 years ago (diff)

Remove links from documentation to datasets. Remove datasets reference directory.

Line 
1"""
2Wrapper for constructing multi-target learners
3==============================================
4
5This module also contains a wrapper, an auxilary learner, that can be used
6to construct simple multi-target learners from standard learners designed
7for data with a single class. The wrapper uses the specified base learner
8to construct independent models for each class.
9
10.. index:: MultitargetLearner
11.. autoclass:: Orange.multitarget.MultitargetLearner
12    :members:
13    :show-inheritance:
14
15.. index:: MultitargetClassifier
16.. autoclass:: Orange.multitarget.MultitargetClassifier
17    :members:
18    :show-inheritance:
19
20Examples
21========
22
23The following example uses a simple multi-target data set (generated with
24:download:`generate_multitarget.py <code/generate_multitarget.py>`) to show
25some basic functionalities (part of
26:download:`multitarget.py <code/multitarget.py>`).
27
28.. literalinclude:: code/multitarget.py
29    :lines: 1-6
30
31Multi-target learners can be used to build prediction models (classifiers)
32which then predict (multiple) class values for a new instance (continuation of
33:download:`multitarget.py <code/multitarget.py>`):
34
35.. literalinclude:: code/multitarget.py
36    :lines: 8-
37
38"""
39
40import Orange
41
42# Other algorithms which also work with multitarget data
43from Orange.regression import pls
44# change the default value of multi_label=True in init
45##from Orange.regression import earth
46
47
48class MultitargetLearner(Orange.classification.Learner):
49    """
50    Wrapper for multitarget problems that constructs independent models
51    of a base learner for each class variable.
52
53    .. attribute:: learner
54
55        The base learner used to learn models for each class.
56    """
57
58    def __new__(cls, learner, data=None, weight=0, **kwargs):
59        self = Orange.classification.Learner.__new__(cls, **kwargs)
60        if data:
61            self.__init__(learner, **kwargs)
62            return self.__call__(data, weight)
63        else:
64            return self
65   
66    def __init__(self, learner, **kwargs):
67        """
68
69        :param learner: Base learner used to construct independent
70                        models for each class.
71                       
72        """
73
74        self.learner = learner
75        self.__dict__.update(kwargs)
76
77    def __call__(self, data, weight=0):
78        """
79        Learn independent models of the base learner for each class.
80
81        :param data: Multitarget data instances (with more than 1 class).
82        :type data: :class:`Orange.data.Table`
83
84        :param weight: Id of meta attribute with weights of instances
85        :type weight: :obj:`int`
86
87        :rtype: :class:`Orange.multitarget.MultitargetClassifier`
88        """
89
90        if not data.domain.class_vars:
91            raise Exception('No classes defined.')
92       
93        domains = [Orange.data.Domain(data.domain.attributes, y)
94                   for y in data.domain.class_vars]
95        classifiers = [self.learner(Orange.data.Table(dom, data), weight)
96                       for dom in domains]
97        return MultitargetClassifier(classifiers=classifiers, domains=domains)
98       
99
100class MultitargetClassifier(Orange.classification.Classifier):
101    """
102    Multitarget classifier returning a list of predictions from each
103    of the independent base classifiers.
104
105    .. attribute classifiers
106
107        List of individual classifiers for each class.
108    """
109
110    def __init__(self, classifiers, domains):
111        self.classifiers = classifiers
112        self.domains = domains
113
114    def __call__(self, instance, return_type=Orange.core.GetValue):
115        """
116        :param instance: Instance to be classified.
117        :type instance: :class:`Orange.data.Instance`
118
119        :param return_type: One of
120            :class:`Orange.classification.Classifier.GetValue`,
121            :class:`Orange.classification.Classifier.GetProbabilities` or
122            :class:`Orange.classification.Classifier.GetBoth`
123        """
124
125        predictions = [c(Orange.data.Instance(dom, instance), return_type)
126                       for c, dom in zip(self.classifiers, self.domains)]
127        return zip(*predictions) if return_type == Orange.core.GetBoth \
128               else predictions
129
Note: See TracBrowser for help on using the repository browser.