1
1
import logging
2
- import typing
2
+ from typing import List , Optional , Union , cast
3
3
4
4
import numpy as np
5
5
12
12
from autoPyTorch .utils .logging_ import PicklableClientLogger
13
13
14
14
15
- SUPPORTED_TARGET_TYPES = typing . Union [
16
- typing . List ,
15
+ SUPPORTED_TARGET_TYPES = Union [
16
+ List ,
17
17
pd .Series ,
18
18
pd .DataFrame ,
19
19
np .ndarray ,
@@ -35,48 +35,50 @@ class BaseTargetValidator(BaseEstimator):
35
35
is_classification (bool):
36
36
A bool that indicates if the validator should operate in classification mode.
37
37
During classification, the targets are encoded.
38
- encoder (typing. Optional[BaseEstimator]):
38
+ encoder (Optional[BaseEstimator]):
39
39
Host a encoder object if the data requires transformation (for example,
40
40
if provided a categorical column in a pandas DataFrame)
41
- enc_columns (typing. List[str])
41
+ enc_columns (List[str])
42
42
List of columns that where encoded
43
43
"""
44
44
def __init__ (self ,
45
45
is_classification : bool = False ,
46
- logger : typing .Optional [typing .Union [PicklableClientLogger , logging .Logger
47
- ]] = None ,
48
- ) -> None :
46
+ logger : Optional [Union [PicklableClientLogger ,
47
+ logging .Logger
48
+ ]
49
+ ] = None ,
50
+ ):
49
51
self .is_classification = is_classification
50
52
51
- self .data_type = None # type: typing. Optional[type]
53
+ self .data_type : Optional [type ] = None
52
54
53
- self .encoder = None # type: typing. Optional[BaseEstimator]
55
+ self .encoder : Optional [BaseEstimator ] = None
54
56
55
- self .out_dimensionality = None # type: typing. Optional[int]
56
- self .type_of_target = None # type: typing. Optional[str]
57
+ self .out_dimensionality : Optional [int ] = None
58
+ self .type_of_target : Optional [str ] = None
57
59
58
- self .logger : typing . Union [
60
+ self .logger : Union [
59
61
PicklableClientLogger , logging .Logger
60
62
] = logger if logger is not None else logging .getLogger (__name__ )
61
63
62
64
# Store the dtype for remapping to correct type
63
- self .dtype = None # type: typing. Optional[type]
65
+ self .dtype : Optional [type ] = None
64
66
65
67
self ._is_fitted = False
66
68
67
69
def fit (
68
70
self ,
69
71
y_train : SUPPORTED_TARGET_TYPES ,
70
- y_test : typing . Optional [SUPPORTED_TARGET_TYPES ] = None ,
72
+ y_test : Optional [SUPPORTED_TARGET_TYPES ] = None ,
71
73
) -> BaseEstimator :
72
74
"""
73
75
Validates and fit a categorical encoder (if needed) to the targets
74
76
The supported data types are List, numpy arrays and pandas DataFrames.
75
77
76
- Arguments :
78
+ Args :
77
79
y_train (SUPPORTED_TARGET_TYPES)
78
80
A set of targets set aside for training
79
- y_test (typing. Union[SUPPORTED_TARGET_TYPES])
81
+ y_test (Union[SUPPORTED_TARGET_TYPES])
80
82
A hold out set of data used of the targets. It is also used to fit the
81
83
categories of the encoder.
82
84
"""
@@ -95,7 +97,8 @@ def fit(
95
97
np .shape (y_test )
96
98
))
97
99
if isinstance (y_train , pd .DataFrame ):
98
- y_test = typing .cast (pd .DataFrame , y_test )
100
+ y_train = cast (pd .DataFrame , y_train )
101
+ y_test = cast (pd .DataFrame , y_test )
99
102
if y_train .columns .tolist () != y_test .columns .tolist ():
100
103
raise ValueError (
101
104
"Train and test targets must both have the same columns, yet "
@@ -126,24 +129,24 @@ def fit(
126
129
def _fit (
127
130
self ,
128
131
y_train : SUPPORTED_TARGET_TYPES ,
129
- y_test : typing . Optional [SUPPORTED_TARGET_TYPES ] = None ,
132
+ y_test : Optional [SUPPORTED_TARGET_TYPES ] = None ,
130
133
) -> BaseEstimator :
131
134
"""
132
- Arguments :
135
+ Args :
133
136
y_train (SUPPORTED_TARGET_TYPES)
134
137
The labels of the current task. They are going to be encoded in case
135
138
of classification
136
- y_test (typing. Optional[SUPPORTED_TARGET_TYPES])
139
+ y_test (Optional[SUPPORTED_TARGET_TYPES])
137
140
A holdout set of labels
138
141
"""
139
142
raise NotImplementedError ()
140
143
141
144
def transform (
142
145
self ,
143
- y : typing . Union [SUPPORTED_TARGET_TYPES ],
146
+ y : Union [SUPPORTED_TARGET_TYPES ],
144
147
) -> np .ndarray :
145
148
"""
146
- Arguments :
149
+ Args :
147
150
y (SUPPORTED_TARGET_TYPES)
148
151
A set of targets that are going to be encoded if the current task
149
152
is classification
@@ -160,8 +163,8 @@ def inverse_transform(
160
163
"""
161
164
Revert any encoding transformation done on a target array
162
165
163
- Arguments :
164
- y (typing. Union[np.ndarray, pd.DataFrame, pd.Series]):
166
+ Args :
167
+ y (Union[np.ndarray, pd.DataFrame, pd.Series]):
165
168
Target array to be transformed back to original form before encoding
166
169
Returns:
167
170
np.ndarray:
0 commit comments