source: orange/Orange/regression/base.py @ 10396:0d620bca064c

Revision 10396:0d620bca064c, 3.2 KB checked in by janezd <janez.demsar@…>, 2 years ago (diff)

Removed Orange.regression.base.rst; moved the text to Orange.regression.rst, which also got some introduction

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