Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def get_hyperparameter_search_space(self,
'RandomKitchenSinks',
'Nystroem',
'PolynomialFeatures',
'PowerTransformer',
'TruncatedSVD',
]
for default_ in defaults:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class PowerTransformer(BaseScaler):
to zero mean and unit variance.
"""
def __init__(self,
random_state: Optional[Union[np.random.RandomState, int]] = None):
random_state: Optional[np.random.RandomState] = None):
super().__init__()
self.random_state = random_state

def fit(self, X: Dict[str, Any], y: Any = None) -> BaseScaler:

self.check_requirements(X, y)

self.preprocessor['numerical'] = SklearnPowerTransformer(copy=False)
self.preprocessor['numerical'] = SklearnPowerTransformer(method='yeo-johnson', copy=False)
return self

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

class QuantileTransformer(BaseScaler):
"""
Transforms the features to follow a uniform or a normal distribution
Transform the features to follow a uniform or a normal distribution
using quantiles information.

For more details of each attribute, see:
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.QuantileTransformer.html
"""
Comment thread
ravinkohli marked this conversation as resolved.
def __init__(
self,
n_quantiles: int = 1000,
output_distribution: str = "normal",
random_state: Optional[Union[np.random.RandomState, int]] = None
output_distribution: str = "normal", # Literal["normal", "uniform"]
random_state: Optional[np.random.RandomState] = None
):
super().__init__()
self.random_state = random_state
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from typing import Any, Dict, Optional, Union

from ConfigSpace.configuration_space import ConfigurationSpace
from ConfigSpace.hyperparameters import (
UniformFloatHyperparameter,
)

import numpy as np

from sklearn.preprocessing import RobustScaler as SklearnRobustScaler

from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType
from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.scaling.base_scaler import BaseScaler
from autoPyTorch.utils.common import FitRequirement, HyperparameterSearchSpace, add_hyperparameter


class RobustScaler(BaseScaler):
"""
Remove the median and scale features according to the quantile_range to make
the features robust to outliers.

For more details of the preprocessor, see:
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.RobustScaler.html
"""
def __init__(
self,
q_min: float = 0.25,
q_max: float = 0.75,
random_state: Optional[np.random.RandomState] = None
):
super().__init__()
self.add_fit_requirements([
FitRequirement('issparse', (bool,), user_defined=True, dataset_property=True)])
self.random_state = random_state
self.q_min = q_min
self.q_max = q_max

def fit(self, X: Dict[str, Any], y: Any = None) -> BaseScaler:

self.check_requirements(X, y)
with_centering = False if X['dataset_properties']['issparse'] else True
Comment thread
ravinkohli marked this conversation as resolved.
Outdated

self.preprocessor['numerical'] = SklearnRobustScaler(quantile_range=(self.q_min, self.q_max),
with_centering=with_centering,
copy=False)

return self

@staticmethod
def get_hyperparameter_search_space(
dataset_properties: Optional[Dict[str, BaseDatasetPropertiesType]] = None,
q_min: HyperparameterSearchSpace = HyperparameterSearchSpace(hyperparameter="q_min",
value_range=(0.001, 0.3),
default_value=0.25),
q_max: HyperparameterSearchSpace = HyperparameterSearchSpace(hyperparameter="q_max",
value_range=(0.7, 0.999),
default_value=0.75)
) -> ConfigurationSpace:
cs = ConfigurationSpace()

add_hyperparameter(cs, q_min, UniformFloatHyperparameter)
add_hyperparameter(cs, q_max, UniformFloatHyperparameter)

return cs

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, BaseDatasetPropertiesType]] = None
) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'RobustScaler',
'name': 'RobustScaler',
'handles_sparse': True
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ def get_hyperparameter_search_space(self,
'MinMaxScaler',
'PowerTransformer',
'QuantileTransformer',
'RobustScaler',
'NoScaler'
]
for default_ in defaults:
if default_ in available_scalers:
if include is not None and default_ not in include:
continue
if exclude is not None and default_ in exclude:
continue
default = default_
break

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def random_state():
return 11


@pytest.fixture(params=['TruncatedSVD', 'PolynomialFeatures', 'PowerTransformer',
@pytest.fixture(params=['TruncatedSVD', 'PolynomialFeatures',
'Nystroem', 'KernelPCA', 'RandomKitchenSinks'])
def preprocessor(request):
return request.param
Expand Down
41 changes: 41 additions & 0 deletions test/test_pipeline/components/preprocessing/test_scalers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
PowerTransformer
from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.scaling.QuantileTransformer import \
QuantileTransformer
from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.scaling.RobustScaler import RobustScaler
from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.scaling.StandardScaler import StandardScaler


Expand Down Expand Up @@ -285,6 +286,46 @@ def test_power_transformer():
[0.993609, 1.001055, 1.005734]]), rtol=1e-06)


def test_robust_scaler():
data = np.array([[1, 2, 3],
[7, 8, 9],
[4, 5, 6],
[11, 12, 13],
[17, 18, 19],
[14, 15, 16]])
train_indices = np.array([0, 2, 5])
test_indices = np.array([1, 4, 3])
categorical_columns = list()
numerical_columns = [0, 1, 2]
dataset_properties = {'categorical_columns': categorical_columns,
'numerical_columns': numerical_columns,
'issparse': False}
X = {
'X_train': data[train_indices],
'dataset_properties': dataset_properties
}
scaler_component = RobustScaler()

scaler_component = scaler_component.fit(X)
X = scaler_component.transform(X)
scaler = X['scaler']['numerical']

# check if the fit dictionary X is modified as expected
assert isinstance(X['scaler'], dict)
assert isinstance(scaler, BaseEstimator)
assert X['scaler']['categorical'] is None

# make column transformer with returned encoder to fit on data
column_transformer = make_column_transformer((scaler, X['dataset_properties']['numerical_columns']),
remainder='passthrough')
column_transformer = column_transformer.fit(X['X_train'])
transformed = column_transformer.transform(data[test_indices])

assert_allclose(transformed, np.array([[100, 100, 100],
[433.33333333, 433.33333333, 433.33333333],
[233.33333333, 233.33333333, 233.33333333]]))


class TestQuantileTransformer():
def test_quantile_transformer_uniform(self):
data = np.array([[1, 2, 3],
Expand Down