|
1
|
|
|
from abc import ABC |
|
2
|
|
|
import pandas as pd |
|
3
|
|
|
|
|
4
|
|
|
from data.discretization import BaseBinner, BinnerFactory |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class BasePDBinner(BaseBinner, ABC): |
|
8
|
|
|
|
|
9
|
|
|
@classmethod |
|
10
|
|
|
def register_as_subclass(cls, binner_type): |
|
11
|
|
|
def wrapper(subclass): |
|
12
|
|
|
cls.subclasses[binner_type] = subclass |
|
13
|
|
|
return subclass |
|
14
|
|
|
return wrapper |
|
15
|
|
|
|
|
16
|
|
|
@classmethod |
|
17
|
|
|
def create(cls, binner_type, *args, **kwargs): |
|
18
|
|
|
if binner_type not in cls.subclasses: |
|
19
|
|
|
raise ValueError('Bad "BinnerFactory Backend type" type \'{}\''.format(binner_type)) |
|
20
|
|
|
return cls.subclasses[binner_type](*args, **kwargs) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
@BaseDFBinner.register_as_subclass('same-length') |
|
|
|
|
|
|
24
|
|
|
class DFSameLengthBinning(BasePDBinner): |
|
25
|
|
|
|
|
26
|
|
|
def bin(self, values, nb_bins): |
|
27
|
|
|
return pd.cut(values, nb_bins) |
|
28
|
|
|
|
|
29
|
|
|
@BaseDFBinner.register_as_subclass('quantisized') |
|
|
|
|
|
|
30
|
|
|
class DFQBinning(BasePDBinner): |
|
31
|
|
|
|
|
32
|
|
|
def bin(self, values, nb_bins): |
|
33
|
|
|
return pd.qcut(values, nb_bins) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
@BinnerFactory.register_as_subclass('pandas') |
|
37
|
|
|
class PDBinnerFactory(BinnerFactory): |
|
38
|
|
|
|
|
39
|
|
|
def equal_length_binner(self, *args, **kwargs) -> DFSameLengthBinning: |
|
40
|
|
|
return DFSameLengthBinning() |
|
41
|
|
|
|
|
42
|
|
|
def quantisized_binner(self, *args, **kwargs) -> DFQBinning: |
|
43
|
|
|
return DFQBinning() |
|
44
|
|
|
|
|
45
|
|
|
def create_binner(self, *args, **kwargs) -> BasePDBinner: |
|
46
|
|
|
BasePDBinner.create(*args, **kwargs) |
|
47
|
|
|
|
|
48
|
|
|
|