data_preprocessing.scaler
- class Scaler(scaler_type='standard', scale_categorical_features=True, categorical_features=None, **params)[source]
Bases:
DataTransformerPreprocessing step for scaling numerical (and optionally categorical) features.
Supports ‘standard’ (StandardScaler) and ‘minmax’ (MinMaxScaler) scalers. Automatically enforces float conversion for all columns and optionally excludes encoded categorical features from scaling depending on scale_categorical_features.
- scaler_type
Type of scaler selected (‘standard’ or ‘minmax’).
- Type:
str
- scale_categorical_features
Whether to scale features matched in categorical_features.
- Type:
bool
- categorical_features
List of column names treated as categorical (matched by substring, to cover one-hot encoded columns).
- Type:
List[str]
- scaler
Instantiated sklearn scaler object (e.g., StandardScaler).
- Type:
SklearnScaler
- feature_names_in_
Names of features seen during fit().
- Type:
List[str]
- feature_names_out_
Names of features output after transform() (same as input).
- Type:
List[str]
- columns_dropped_
Placeholder (unused in current implementation; preserved for API).
- Type:
List[str]
- params
Keyword arguments passed to the underlying sklearn scaler constructor.
- Type:
dict
Initializes the Scaler with specified strategy and feature selection logic.
- Parameters:
scaler_type (
str) – Type of scaler to use. Supported: ‘standard’ (default), ‘minmax’.scale_categorical_features (
bool) – Whether to include categorical (e.g., one-hot encoded) features in scaling. If False, only non-encoded features are scaled. Defaults to True.categorical_features (
list) – List of column names to treat as categorical (matched by substring, to cover one-hot encoded columns such as category_col_A). Used only when scale_categorical_features=False to determine which features to exclude. Defaults to None (interpreted as empty list).**params – Additional keyword arguments passed to the underlying sklearn scaler constructor. Example: {‘with_mean’: False, ‘with_std’: True}.
- Raises:
ValueError – If scaler_type is not supported (must be ‘standard’ or ‘minmax’).
- SCALER_REGISTRY = {'minmax': <class 'sklearn.preprocessing._data.MinMaxScaler'>, 'standard': <class 'sklearn.preprocessing._data.StandardScaler'>}
- fit(x, y=None)[source]
Fits the scaler on the input data, optionally excluding categorical features.
Enforces float dtype for all columns. Scales either all columns or only non-encoded numerical features, depending on scale_categorical_features.
- Parameters:
x (
DataFrame) – Input feature DataFrame.y (
Series) – Target variable. Ignored; included for scikit-learn API compatibility.
- Returns:
The fitted transformer instance (self), for method chaining.
- Return type:
- Raises:
ValueError – If any column cannot be converted to float.
ValueError – If column dtypes are inconsistent across features.
- get_feature_names_out(input_features=None)[source]
Returns the list of output feature names (same as input feature names).
Preserves original column order and names seen during fit().
- Parameters:
input_features – Ignored; included for scikit-learn API compatibility.
- Returns:
Ordered list of output feature names.
- Return type:
list- Raises:
ValueError – If scaler has not been fitted (via check_is_fitted).
- inverse_transform(x)[source]
Applies the inverse scaling transformation to revert scaled data.
Requires that the scaler was fitted and that input column structure matches fit().
- Parameters:
x (
DataFrame) – Scaled input DataFrame, with same columns and order as fit() output.- Returns:
Inversely transformed (descaled) DataFrame, with same shape/index as input.
- Return type:
DataFrame- Raises:
ValueError – If scaler has not been fitted (via check_is_fitted).
ValueError – If input feature names/order do not match those seen during fit().
- transform(x)[source]
Applies the learned scaling transformation to input data.
Enforces float dtypes and validates feature alignment with fit(). Scales either all columns or only numerical (non-encoded) columns, depending on scale_categorical_features.
- Parameters:
x (
DataFrame) – Input feature DataFrame, with column order and names matching those seen in fit().- Returns:
Scaled DataFrame, same shape and index as input.
- Return type:
DataFrame- Raises:
ValueError – If input feature names/order do not match those seen during fit().
ValueError – If any column cannot be converted to float.
ValueError – If scaler has not been fitted (via check_is_fitted).