source: orange/Orange/feature/imputation.py @ 11459:fc07a5c346be

Revision 11459:fc07a5c346be, 2.0 KB checked in by Ales Erjavec <ales.erjavec@…>, 12 months ago (diff)

Fixed checks for passed dataset table argument in new methods.

Use 'instances is not None' idiom and not a boolean test to guard against cases
where the passed dataset length is 0.

RevLine 
[8042]1import Orange.core as orange
[10071]2from orange import\
3    Imputer as Imputer,\
4    ImputerConstructor as Constructor,\
5    ImputerConstructor_minimal as MinimalConstructor,\
6    ImputerConstructor_maximal as MaximalConstructor,\
7    ImputerConstructor_average as AverageConstructor,\
8    ImputerConstructor_model as ModelConstructor,\
9    ImputerConstructor_asValue as AsValueConstructor,\
10    ImputerConstructor_random as RandomConstructor,\
11    Imputer_defaults as Defaults,\
12    Imputer_model as Model,\
13    Imputer_random as Random,\
14    Imputer_asValue as AsValue,\
15    ImputeClassifier as Classifier
[8042]16
17import Orange.misc
18
19class ImputeLearner(orange.Learner):
[11459]20    def __new__(cls, data=None, weight_id=0, **keyw):
[8042]21        self = orange.Learner.__new__(cls, **keyw)
22        self.dont_impute_classifier = False
23        self.__dict__.update(keyw)
[11459]24        if data is not None:
[10071]25            return self.__call__(data, weight_id)
[8042]26        else:
27            return self
28       
29    def __call__(self, data, weight=0):
30        trained_imputer = self.imputer_constructor(data, weight)
31        imputed_data = trained_imputer(data, weight)
32        base_classifier = self.base_learner(imputed_data, weight)
33        if self.dont_impute_classifier:
34            return base_classifier
35        else:
36            return ImputeClassifier(base_classifier, trained_imputer)
37
[10580]38ImputeLearner = Orange.utils.deprecated_members(
[8042]39  {
40      "dontImputeClassifier": "dont_impute_classifier",
41      "imputerConstructor": "imputer_constructor",
42      "baseLearner": "base_learner",
43      "weightID": "weight_id"
44  })(ImputeLearner)
45
46
47class ImputeClassifier(orange.Classifier):
48    def __init__(self, base_classifier, imputer, **argkw):
49        self.base_classifier = base_classifier
50        self.imputer = imputer
51        self.__dict__.update(argkw)
52
[10071]53    def __call__(self, i, what=orange.GetValue):
54        return self.base_classifier(self.imputer(i), what)
[8042]55
[10580]56ImputeClassifier = Orange.utils.deprecated_members(
[8042]57  {
58      "baseClassifier": "base_classifier"
59  })(ImputeClassifier)
Note: See TracBrowser for help on using the repository browser.