source: orange/Orange/multitarget/__init__.py @ 10847:0404e6b125e1

Revision 10847:0404e6b125e1, 4.2 KB checked in by Lan Zagar <lan.zagar@…>, 2 years ago (diff)

Fix pickling of multitarget learners and classifiers.

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 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
44from Orange.regression import earth
45
46
47class MultitargetLearner(Orange.classification.Learner):
48    """
49    Wrapper for multitarget problems that constructs independent models
50    of a base learner for each class variable.
51
52    .. attribute:: learner
53
54        The base learner used to learn models for each class.
55    """
56
57    def __new__(cls, learner, data=None, weight=0, **kwargs):
58        self = Orange.classification.Learner.__new__(cls, **kwargs)
59        if data:
60            self.__init__(learner, **kwargs)
61            return self.__call__(data, weight)
62        else:
63            return self
64   
65    def __init__(self, learner, **kwargs):
66        """
67        :param learner: Base learner used to construct independent
68                        models for each class.
69        """
70        self.learner = learner
71        self.__dict__.update(kwargs)
72
73    def __call__(self, data, weight=0):
74        """
75        Learn independent models of the base learner for each class.
76
77        :param data: Multitarget data instances (with more than 1 class).
78        :type data: :class:`Orange.data.Table`
79
80        :param weight: Id of meta attribute with weights of instances
81        :type weight: :obj:`int`
82
83        :rtype: :class:`Orange.multitarget.MultitargetClassifier`
84        """
85
86        if not data.domain.class_vars:
87            raise Exception('No classes defined.')
88       
89        domains = [Orange.data.Domain(data.domain.attributes, y)
90                   for y in data.domain.class_vars]
91        classifiers = [self.learner(Orange.data.Table(dom, data), weight)
92                       for dom in domains]
93        return MultitargetClassifier(classifiers=classifiers, domains=domains)
94       
95    def __reduce__(self):
96        return type(self), (self.learner,), dict(self.__dict__)
97
98
99class MultitargetClassifier(Orange.classification.Classifier):
100    """
101    Multitarget classifier that returns a list of predictions from each
102    of the independent base classifiers.
103
104    .. attribute classifiers
105
106        List of individual classifiers for each class.
107    """
108
109    def __init__(self, classifiers, domains):
110        self.classifiers = classifiers
111        self.domains = domains
112
113    def __call__(self, instance, return_type=Orange.core.GetValue):
114        """
115        :param instance: Instance to be classified.
116        :type instance: :class:`Orange.data.Instance`
117
118        :param return_type: One of
119            :class:`Orange.classification.Classifier.GetValue`,
120            :class:`Orange.classification.Classifier.GetProbabilities` or
121            :class:`Orange.classification.Classifier.GetBoth`
122        """
123
124        predictions = [c(Orange.data.Instance(dom, instance), return_type)
125                       for c, dom in zip(self.classifiers, self.domains)]
126        return zip(*predictions) if return_type == Orange.core.GetBoth \
127               else predictions
128
129    def __reduce__(self):
130        return type(self), (self.classifiers, self.domains), dict(self.__dict__)
131
Note: See TracBrowser for help on using the repository browser.