Skip to content

Commit 7fd5fd3

Browse files
committed
[refactor] Change some method names
1 parent 048656e commit 7fd5fd3

2 files changed

Lines changed: 35 additions & 51 deletions

File tree

autoPyTorch/data/base_feature_validator.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class BaseFeatureValidator(BaseEstimator):
2727
column_transformer (Optional[BaseEstimator])
2828
Host a encoder object if the data requires transformation (for example,
2929
if provided a categorical column in a pandas DataFrame)
30-
transformed_columns (List[str])
31-
List of columns that were encoded.
30+
enc_columns (List[str])
31+
The list of column names that should be encoded.
3232
"""
3333
def __init__(
3434
self,
@@ -41,7 +41,7 @@ def __init__(
4141
self.column_order: List[str] = []
4242

4343
self.column_transformer: Optional[BaseEstimator] = None
44-
self.transformed_columns: List[str] = []
44+
self.enc_columns: List[str] = []
4545

4646
self.logger: Union[
4747
PicklableClientLogger, logging.Logger
@@ -75,7 +75,8 @@ def fit(
7575

7676
# If a list was provided, it will be converted to pandas
7777
if isinstance(X_train, list):
78-
X_train, X_test = self.list_to_dataframe(X_train, X_test)
78+
X_train = self.list_to_pandas(X_train)
79+
X_test = self.list_to_pandas(X_test) if X_test is not None else None
7980

8081
self._check_data(X_train)
8182

autoPyTorch/data/tabular_feature_validator.py

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class TabularFeatureValidator(BaseFeatureValidator):
8686
List for which an element at each index is a
8787
list containing the categories for the respective
8888
categorical column.
89-
transformed_columns (List[str])
89+
enc_columns (List[str])
9090
List of columns that were transformed.
9191
column_transformer (Optional[BaseEstimator])
9292
Hosts an imputer and an encoder object if the data
@@ -154,7 +154,7 @@ def _fit(
154154
# The final output of a validator is a numpy array. But pandas
155155
# gives us information about the column dtype
156156
if isinstance(X, np.ndarray):
157-
X = self.numpy_array_to_pandas(X)
157+
X = self.numpy_to_pandas(X)
158158

159159
if ispandas(X) and not issparse(X):
160160
X = cast(pd.DataFrame, X)
@@ -175,16 +175,16 @@ def _fit(
175175
if not X.select_dtypes(include='object').empty:
176176
X = self.infer_objects(X)
177177

178-
self.transformed_columns, self.feat_type = self._get_columns_to_encode(X)
178+
self.enc_columns, self.feat_type = self._get_columns_to_encode(X)
179179

180180
assert self.feat_type is not None
181181

182-
if len(self.transformed_columns) > 0:
182+
if len(self.enc_columns) > 0:
183183

184184
preprocessors = get_tabular_preprocessors()
185185
self.column_transformer = _create_column_transformer(
186186
preprocessors=preprocessors,
187-
categorical_columns=self.transformed_columns,
187+
categorical_columns=self.enc_columns,
188188
)
189189

190190
# Mypy redefinition
@@ -241,10 +241,9 @@ def transform(
241241

242242
# If a list was provided, it will be converted to pandas
243243
if isinstance(X, list):
244-
X, _ = self.list_to_dataframe(X)
245-
246-
if isinstance(X, np.ndarray):
247-
X = self.numpy_array_to_pandas(X)
244+
X = self.list_to_pandas(X)
245+
elif isinstance(X, np.ndarray):
246+
X = self.numpy_to_pandas(X)
248247

249248
if ispandas(X) and not issparse(X):
250249
if np.any(pd.isnull(X)):
@@ -374,7 +373,7 @@ def _check_data(
374373

375374
# Define the column to be encoded here as the feature validator is fitted once
376375
# per estimator
377-
self.transformed_columns, self.feat_type = self._get_columns_to_encode(X)
376+
self.enc_columns, self.feat_type = self._get_columns_to_encode(X)
378377

379378
column_order = [column for column in X.columns]
380379
if len(self.column_order) > 0:
@@ -412,17 +411,17 @@ def _get_columns_to_encode(
412411
checks) and an encoder fitted in the case the data needs encoding
413412
414413
Returns:
415-
transformed_columns (List[str]):
414+
enc_columns (List[str]):
416415
Columns to encode, if any
417416
feat_type:
418417
Type of each column numerical/categorical
419418
"""
420419

421-
if len(self.transformed_columns) > 0 and self.feat_type is not None:
422-
return self.transformed_columns, self.feat_type
420+
if len(self.enc_columns) > 0 and self.feat_type is not None:
421+
return self.enc_columns, self.feat_type
423422

424423
# Register if a column needs encoding
425-
transformed_columns = []
424+
enc_columns = []
426425

427426
# Also, register the feature types for the estimator
428427
feat_type = []
@@ -431,7 +430,7 @@ def _get_columns_to_encode(
431430
for i, column in enumerate(X.columns):
432431
if X[column].dtype.name in ['category', 'bool']:
433432

434-
transformed_columns.append(column)
433+
enc_columns.append(column)
435434
feat_type.append('categorical')
436435
# Move away from np.issubdtype as it causes
437436
# TypeError: data type not understood in certain pandas types
@@ -473,49 +472,33 @@ def _get_columns_to_encode(
473472
)
474473
else:
475474
feat_type.append('numerical')
476-
return transformed_columns, feat_type
475+
return enc_columns, feat_type
477476

478-
def list_to_dataframe(
479-
self,
480-
X_train: SupportedFeatTypes,
481-
X_test: Optional[SupportedFeatTypes] = None,
482-
) -> Tuple[pd.DataFrame, Optional[pd.DataFrame]]:
477+
def list_to_pandas(self, X: SupportedFeatTypes) -> pd.DataFrame:
483478
"""
484-
Converts a list to a pandas DataFrame. In this process, column types are inferred.
485-
486-
If test data is provided, we proactively match it to train data
479+
Convert a list to a pandas DataFrame. In this process, column types are inferred.
487480
488481
Args:
489-
X_train (SupportedFeatTypes):
482+
X (SupportedFeatTypes):
490483
A set of features that are going to be validated (type and dimensionality
491-
checks) and a encoder fitted in the case the data needs encoding
492-
X_test (Optional[SupportedFeatTypes]):
493-
A hold out set of data used for checking
484+
checks) and an encoder fitted in the case the data needs encoding
494485
495486
Returns:
496487
pd.DataFrame:
497-
transformed train data from list to pandas DataFrame
498-
pd.DataFrame:
499-
transformed test data from list to pandas DataFrame
488+
transformed data from list to pandas DataFrame
500489
"""
501490

502491
# If a list was provided, it will be converted to pandas
503-
X_train = pd.DataFrame(data=X_train).infer_objects()
504-
self.logger.warning("The provided feature types to AutoPyTorch are of type list."
505-
"Features have been interpreted as: {}".format([(col, t) for col, t in
506-
zip(X_train.columns, X_train.dtypes)]))
507-
if X_test is not None:
508-
if not isinstance(X_test, list):
509-
self.logger.warning("Train features are a list while the provided test data"
510-
"is {}. X_test will be casted as DataFrame.".format(type(X_test))
511-
)
512-
X_test = pd.DataFrame(data=X_test).infer_objects()
513-
return X_train, X_test
514-
515-
def numpy_array_to_pandas(
516-
self,
517-
X: np.ndarray,
518-
) -> pd.DataFrame:
492+
X = pd.DataFrame(data=X).infer_objects()
493+
data_info = [(col, t) for col, t in zip(X.columns, X.dtypes)]
494+
self.logger.warning(
495+
"The provided feature types to AutoPyTorch are list."
496+
f"Features have been interpreted as: {data_info}"
497+
)
498+
499+
return X
500+
501+
def numpy_to_pandas(self, X: np.ndarray) -> pd.DataFrame:
519502
"""
520503
Converts a numpy array to pandas for type inference
521504

0 commit comments

Comments
 (0)