# source:orange/Orange/multitarget/__init__.py@10331:51a92af40a17

Revision 10331:51a92af40a17, 4.0 KB checked in by Ales Erjavec <ales.erjavec@…>, 2 years ago (diff)

Added earth module import to Orange.multitarget (fixes #1102)

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
25some basic functionalities (part of
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
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
68        :param learner: Base learner used to construct independent
69                        models for each class.
70
71        """
72
73        self.learner = learner
74        self.__dict__.update(kwargs)
75
76    def __call__(self, data, weight=0):
77        """
78        Learn independent models of the base learner for each class.
79
80        :param data: Multitarget data instances (with more than 1 class).
81        :type data: :class:`Orange.data.Table`
82
83        :param weight: Id of meta attribute with weights of instances
84        :type weight: :obj:`int`
85
86        :rtype: :class:`Orange.multitarget.MultitargetClassifier`
87        """
88
89        if not data.domain.class_vars:
90            raise Exception('No classes defined.')
91
92        domains = [Orange.data.Domain(data.domain.attributes, y)
93                   for y in data.domain.class_vars]
94        classifiers = [self.learner(Orange.data.Table(dom, data), weight)
95                       for dom in domains]
96        return MultitargetClassifier(classifiers=classifiers, domains=domains)
97
98
99class MultitargetClassifier(Orange.classification.Classifier):
100    """
101    Multitarget classifier returning 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
Note: See TracBrowser for help on using the repository browser.