source: orange/orange/Orange/multitarget/__init__.py @ 9534:2fb4bdb46798

Revision 9534:2fb4bdb46798, 2.4 KB checked in by lanz <lan.zagar@…>, 2 years ago (diff)

Documentation for Orange.multitarget.tree.

Line 
1import Orange
2
3import tree
4
5# Other algorithms which also work with multitarget data
6from Orange.regression import pls
7# change the default value of multi_label=True in init
8##from Orange.regression import earth
9
10
11class MultitargetLearner(Orange.classification.Learner):
12    """
13    Wrapper for multitarget problems that constructs independent models
14    of a base learner for each class variable.
15
16    .. attribute:: learner
17
18        The base learner used to learn models for each class.
19    """
20
21    def __new__(cls, learner, data=None, weight=0, **kwargs):
22        self = Orange.classification.Learner.__new__(cls, **kwargs)
23        if data:
24            self.__init__(learner, **kwargs)
25            return self.__call__(data, weight)
26        else:
27            return self
28   
29    def __init__(self, learner, **kwargs):
30        self.learner = learner
31        self.__dict__.update(kwargs)
32
33    def __call__(self, data, weight=0):
34        """Learn independent models of the base learner for each class.
35
36        :param data: Multitarget data instances (with more than 1 class).
37        :type data: Orange.data.Table
38        :param weight: Id of meta attribute with weights of instances
39        :type weight: int
40        :rtype: :class:`Orange.multitarget.MultitargetClassifier`
41        """
42
43        if not data.domain.class_vars:
44            raise Exception('No classes defined.')
45       
46        domains = [Orange.data.Domain(data.domain.attributes, y)
47                   for y in data.domain.class_vars]
48        classifiers = [self.learner(Orange.data.Table(dom, data), weight)
49                       for dom in domains]
50        return MultitargetClassifier(classifiers=classifiers, domains=domains)
51       
52
53class MultitargetClassifier(Orange.classification.Classifier):
54    """
55    Multitarget classifier returning a list of predictions from each
56    of the independent base classifiers.
57
58    .. attribute classifiers
59
60        List of individual classifiers for each class.
61    """
62
63    def __init__(self, classifiers, domains):
64        self.classifiers = classifiers
65        self.domains = domains
66
67    def __call__(self, instance, return_type=Orange.core.GetValue):
68        predictions = [c(Orange.data.Instance(dom, instance), return_type)
69                       for c, dom in zip(self.classifiers, self.domains)]
70        return zip(*predictions) if return_type == Orange.core.GetBoth \
71               else predictions
72
Note: See TracBrowser for help on using the repository browser.