-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathbase_feature_validator.py
More file actions
151 lines (124 loc) · 4.91 KB
/
Copy pathbase_feature_validator.py
File metadata and controls
151 lines (124 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Base class for the feature validator given a task
* A wrapper class of the sklearn.base.BaseEstimator
* The feature validator for each task inherits this class
* Check if the provided feature can be processed in AutoPytorch
TODO:
* SUPPORTED_FEAT_TYPES --> Enumerator
* Describe the shape of X
* typing.<type> --> <type>
* logging.Logger --> Logger
"""
import logging
import typing
import numpy as np
import pandas as pd
import scipy.sparse
from sklearn.base import BaseEstimator
from autoPyTorch.utils.logging_ import PicklableClientLogger
SUPPORTED_FEAT_TYPES = typing.Union[
typing.List,
pd.DataFrame,
np.ndarray,
scipy.sparse.bsr_matrix,
scipy.sparse.coo_matrix,
scipy.sparse.csc_matrix,
scipy.sparse.csr_matrix,
scipy.sparse.dia_matrix,
scipy.sparse.dok_matrix,
scipy.sparse.lil_matrix,
]
class BaseFeatureValidator(BaseEstimator):
"""
A class to pre-process features. In this regards, the format of the data is checked,
and if applicable, features are encoded
Attributes:
feat_type (List[str]):
List of the column types found by this estimator during fit.
data_type (str):
Class name of the data type provided during fit.
encoder (typing.Optional[BaseEstimator])
Host a encoder object if the data requires transformation (for example,
if provided a categorical column in a pandas DataFrame)
enc_columns (typing.List[str])
List of columns that were encoded.
"""
def __init__(self,
logger: typing.Optional[typing.Union[PicklableClientLogger, logging.Logger
]] = None,
) -> None:
# Register types to detect unsupported data format changes
self.feat_type = None # type: typing.Optional[typing.List[str]]
self.data_type = None # type: typing.Optional[type]
self.dtypes = [] # type: typing.List[str]
self.column_order = [] # type: typing.List[str]
self.encoder = None # type: typing.Optional[BaseEstimator]
self.enc_columns = [] # type: typing.List[str]
self.logger: typing.Union[
PicklableClientLogger, logging.Logger
] = logger if logger is not None else logging.getLogger(__name__)
# Required for dataset properties
self.num_features = None # type: typing.Optional[int]
self.categories = [] # type: typing.List[typing.List[int]]
self.categorical_columns: typing.List[int] = []
self.numerical_columns: typing.List[int] = []
self._is_fitted = False
def fit(
self,
X_train: SUPPORTED_FEAT_TYPES,
X_test: typing.Optional[SUPPORTED_FEAT_TYPES] = None,
) -> BaseEstimator:
"""
Validates and fit a categorical encoder (if needed) to the features.
The supported data types are List, numpy arrays and pandas DataFrames.
CSR sparse data types are also supported
Arguments:
X_train (SUPPORTED_FEAT_TYPES):
A set of features that are going to be validated (type and dimensionality
checks) and a encoder fitted in the case the data needs encoding
X_test (typing.Optional[SUPPORTED_FEAT_TYPES]):
A hold out set of data used for checking
"""
# If a list was provided, it will be converted to pandas
if isinstance(X_train, list):
X_train, X_test = self.list_to_dataframe(X_train, X_test)
self._check_data(X_train)
if X_test is not None:
self._check_data(X_test)
if np.shape(X_train)[1] != np.shape(X_test)[1]:
raise ValueError("The feature dimensionality of the train and test "
"data does not match train({}) != test({})".format(
np.shape(X_train)[1],
np.shape(X_test)[1]
))
# Fit on the training data
self._fit(X_train)
self._is_fitted = True
return self
def _fit(
self,
X: SUPPORTED_FEAT_TYPES,
) -> BaseEstimator:
"""
Arguments:
X (SUPPORTED_FEAT_TYPES):
A set of features that are going to be validated (type and dimensionality
checks) and a encoder fitted in the case the data needs encoding
Returns:
self:
The fitted base estimator
"""
raise NotImplementedError()
def transform(
self,
X: SUPPORTED_FEAT_TYPES,
) -> np.ndarray:
"""
Arguments:
X_train (SUPPORTED_FEAT_TYPES):
A set of features, whose categorical features are going to be
transformed
Return:
np.ndarray:
The transformed array
"""
raise NotImplementedError()