source: orange/orange/Orange/multitarget/__init__.py @ 9553:be024c77a0df

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

Added some documentation for Orange.multitarget

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        :param learner: Base learner used to construct independent\
61            models for each class.
62        """
63
64        self.learner = learner
65        self.__dict__.update(kwargs)
66
67    def __call__(self, data, weight=0):
68        """
69        Learn independent models of the base learner for each class.
70
71        :param data: Multitarget data instances (with more than 1 class).
72        :type data: :class:`Orange.data.Table`
73
74        :param weight: Id of meta attribute with weights of instances
75        :type weight: :obj:`int`
76
77        :rtype: :class:`Orange.multitarget.MultitargetClassifier`
78        """
79
80        if not data.domain.class_vars:
81            raise Exception('No classes defined.')
82       
83        domains = [Orange.data.Domain(data.domain.attributes, y)
84                   for y in data.domain.class_vars]
85        classifiers = [self.learner(Orange.data.Table(dom, data), weight)
86                       for dom in domains]
87        return MultitargetClassifier(classifiers=classifiers, domains=domains)
88       
89
90class MultitargetClassifier(Orange.classification.Classifier):
91    """
92    Multitarget classifier returning a list of predictions from each
93    of the independent base classifiers.
94
95    .. attribute classifiers
96
97        List of individual classifiers for each class.
98    """
99
100    def __init__(self, classifiers, domains):
101        self.classifiers = classifiers
102        self.domains = domains
103
104    def __call__(self, instance, return_type=Orange.core.GetValue):
105        """
106        :param instance: Instance to be classified.
107        :type instance: :class:`Orange.data.Instance`
108
109        :param return_type: One of
110            :class:`Orange.classification.Classifier.GetValue`,
111            :class:`Orange.classification.Classifier.GetProbabilities` or
112            :class:`Orange.classification.Classifier.GetBoth`
113        """
114
115        predictions = [c(Orange.data.Instance(dom, instance), return_type)
116                       for c, dom in zip(self.classifiers, self.domains)]
117        return zip(*predictions) if return_type == Orange.core.GetBoth \
118               else predictions
119
Note: See TracBrowser for help on using the repository browser.