source: orange/Orange/regression/base.py @ 10394:962c24741ced

Revision 10394:962c24741ced, 3.2 KB checked in by janezd <janez.demsar@…>, 2 years ago (diff)

Fixes in documentation about base regression learner

Line 
1"""\
2====================================
3Base regression learner (``basic``)
4====================================
5
6.. index:: regression
7
8.. autoclass:: BaseRegressionLearner
9    :members:
10
11"""
12
13import Orange
14
15class BaseRegressionLearner(Orange.core.Learner):
16    """Fitting regressors typically requires data that has only
17    continuous-valued features and no missing values. This class
18    provides methods for appropriate transformation of the data and serves as a base class for various regressor classes.
19    """
20
21    def __new__(cls, table=None, weight_id=None, **kwds):
22        learner = Orange.core.Learner.__new__(cls, **kwds)
23        if table is not None:
24            learner.__init__(**kwds)
25            return learner(table, weight_id)
26        else:
27            return learner
28
29    def __init__(self, imputer=None, continuizer=None):
30        self.imputer = None
31        self.continuizer = None
32       
33        self.set_imputer(imputer)
34        self.set_continuizer(continuizer)
35       
36
37    def set_imputer(self, imputer=None):
38        """ Set the imputer for missing data.
39
40        :param imputer: function which constructs the imputer for the
41            missing values, if ``None``, the default imputer replaces
42            missing continuous data with the average of the
43            corresponding variable and missing discrete data with the
44            most frequent value.
45        :type imputer: None or Orange.feature.imputation.ModelConstructor
46        """
47        if imputer is not None:
48            self.imputer = imputer
49        else: # default imputer
50            self.imputer = Orange.feature.imputation.ModelConstructor()
51            self.imputer.learner_continuous = Orange.regression.mean.MeanLearner()
52            self.imputer.learner_discrete = Orange.classification.majority.MajorityLearner()
53
54    def set_continuizer(self, continuizer=None):
55        """Set the continuizer of the discrete variables
56
57        :param continuizer: function which replaces the categorical
58            (dicrete) variables with numerical variables. If ``None``,
59            the default continuizer is used
60        :type continuizer: None or Orange.data.continuization.DomainContinuizer
61        """
62        if continuizer is not None:
63            self.continuizer = continuizer
64        else: # default continuizer
65            self.continuizer = Orange.data.continuization.DomainContinuizer()
66            self.continuizer.multinomial_treatment = self.continuizer.FrequentIsBase
67            self.continuizer.zero_based = True
68
69    def impute_table(self, table):
70        """Impute missing values and return a new
71        :class:`Orange.data.Table` object
72
73        :param table: data instances.
74        :type table: :class:`Orange.data.Table`
75        """
76        if table.has_missing_values():
77            imputer = self.imputer(table)
78            table = imputer(table)
79        return table
80
81    def continuize_table(self, table):
82        """Replace discrete variables with continuous and return a new
83        instance of :class:`Orange.data.Table`.
84
85        :param table: data instances.
86        :type table: :class:`Orange.data.Table`
87        """
88        if table.domain.has_discrete_attributes():
89            new_domain = self.continuizer(table)
90            table = table.translate(new_domain)
91        return table
Note: See TracBrowser for help on using the repository browser.