source: orange/orange/Orange/multitarget/__init__.py @ 9550:bd71f96b33d5

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

Documentation fixes.

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