source: orange/orange/Orange/multitarget/__init__.py @ 9554:2485f84aeb8c

Revision 9554:2485f84aeb8c, 3.8 KB checked in by lanz <lan.zagar@…>, 2 years ago (diff)

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