source: orange/orange/Orange/multitarget/__init__.py @ 9604:d64445907a61

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

Added a multitarget data set and expanded documentation.

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