source: orange/Orange/data/discretization.py @ 11576:fb4551130eaf

Revision 11576:fb4551130eaf, 2.2 KB checked in by markotoplak, 11 months ago (diff)

Added missing dicretizers to the Orange hierarchy.

Line 
1import Orange
2
3from Orange.core import\
4    EquiNDiscretization as EqualFreq,\
5    EquiDistDiscretization as EqualWidth,\
6    EntropyDiscretization as Entropy,\
7    BiModalDiscretization as BiModal,\
8    Preprocessor_discretize
9
10
11class DiscretizeTable(object):
12    """Discretizes all continuous features of the data table.
13
14    :param data: Data to discretize.
15    :type data: :class:`Orange.data.Table`
16
17    :param features: Data features to discretize. `None` (default) to
18        discretize all features.
19    :type features: list of :class:`Orange.feature.Descriptor`
20
21    :param method: Feature discretization method.
22    :type method: :class:`Orange.feature.discretization.Discretization`
23
24    :param clean: Clean the data domain after discretization. If `True`,
25        features discretized to a constant will be removed. Useful only
26        for discretizers which infer number of discretization intervals
27        from data, like :class:`Orange.feature.discretize.Entropy`
28        (default: `True`).
29    :type clean: bool
30
31    """
32    def __new__(cls, data=None, features=None, discretize_class=False,
33                method=EqualFreq(n=3), clean=True):
34        if data is None:
35            self = object.__new__(cls)
36            return self
37        else:
38            self = cls(features=features, discretize_class=discretize_class,
39                       method=method, clean=clean)
40            return self(data)
41
42    def __init__(self, features=None, discretize_class=False,
43                 method=EqualFreq(n=3), clean=True):
44        self.features = features
45        self.discretize_class = discretize_class
46        self.method = method
47        self.clean = clean
48
49    def __call__(self, data):
50        pp = Preprocessor_discretize(attributes=self.features,
51                                     discretize_class=self.discretize_class)
52        pp.method = self.method
53        ddata = pp(data)
54
55        if self.clean:
56            features = [x for x in ddata.domain.features if len(x.values) > 1]
57            domain = Orange.data.Domain(features, ddata.domain.class_var,
58                                        class_vars=ddata.domain.class_vars)
59            return Orange.data.Table(domain, ddata)
60        else:
61            return ddata
Note: See TracBrowser for help on using the repository browser.