#
source:
orange/Orange/regression/base.py
@
10238:d58af84b3ee5

Revision 10238:d58af84b3ee5, 3.0 KB checked in by anzeh <anze.staric@…>, 2 years ago (diff) |
---|

Line | |
---|---|

1 | """\ |

2 | ==================================== |

3 | Basic regression learner (``basic``) |

4 | ==================================== |

5 | |

6 | .. index:: regression |

7 | |

8 | .. autoclass:: BaseRegressionLearner |

9 | :members: |

10 | |

11 | """ |

12 | |

13 | import Orange |

14 | |

15 | class BaseRegressionLearner(Orange.core.Learner): |

16 | """ Base Regression Learner "learns" how to treat the discrete |

17 | variables and missing data. |

18 | """ |

19 | |

20 | def __new__(cls, table=None, weight_id=None, **kwds): |

21 | learner = Orange.core.Learner.__new__(cls, **kwds) |

22 | if table is not None: |

23 | learner.__init__(**kwds) |

24 | return learner(table, weight_id) |

25 | else: |

26 | return learner |

27 | |

28 | def __init__(self, imputer=None, continuizer=None): |

29 | self.imputer = None |

30 | self.continuizer = None |

31 | |

32 | self.set_imputer(imputer) |

33 | self.set_continuizer(continuizer) |

34 | |

35 | |

36 | def set_imputer(self, imputer=None): |

37 | """ Sets the imputer for missing values. |

38 | |

39 | :param imputer: function which imputes the missing values, |

40 | if None, the default imputer: mean for the continuous variables |

41 | and most frequent value (majority) for discrete variables |

42 | :type imputer: None or Orange.feature.imputation.ModelConstructor |

43 | """ |

44 | if imputer is not None: |

45 | self.imputer = imputer |

46 | else: # default imputer |

47 | self.imputer = Orange.feature.imputation.ModelConstructor() |

48 | self.imputer.learner_continuous = Orange.regression.mean.MeanLearner() |

49 | self.imputer.learner_discrete = Orange.classification.majority.MajorityLearner() |

50 | |

51 | def set_continuizer(self, continuizer=None): |

52 | """ Sets the continuizer of the discrete variables |

53 | |

54 | :param continuizer: function which replaces the categorical (dicrete) |

55 | variables with numerical variables. If None, the default continuizer |

56 | is used |

57 | :type continuizer: None or Orange.data.continuization.DomainContinuizer |

58 | """ |

59 | if continuizer is not None: |

60 | self.continuizer = continuizer |

61 | else: # default continuizer |

62 | self.continuizer = Orange.data.continuization.DomainContinuizer() |

63 | self.continuizer.multinomial_treatment = self.continuizer.FrequentIsBase |

64 | self.continuizer.zero_based = True |

65 | |

66 | def impute_table(self, table): |

67 | """ Imputes missing values. |

68 | Returns a new :class:`Orange.data.Table` object |

69 | |

70 | :param table: data instances. |

71 | :type table: :class:`Orange.data.Table` |

72 | """ |

73 | if table.has_missing_values(): |

74 | imputer = self.imputer(table) |

75 | table = imputer(table) |

76 | return table |

77 | |

78 | def continuize_table(self, table): |

79 | """ Continuizes the discrete variables. |

80 | Returns a new :class:`Orange.data.Table` object |

81 | |

82 | :param table: data instances. |

83 | :type table: :class:`Orange.data.Table` |

84 | """ |

85 | if table.domain.has_discrete_attributes(): |

86 | new_domain = self.continuizer(table) |

87 | table = table.translate(new_domain) |

88 | return table |

**Note:**See TracBrowser for help on using the repository browser.