StandardScaler
Standardize features by removing mean and scaling to unit variance. Formula:z = (x - μ) / σ
Constructor
Methods
Compute the mean and standard deviation from training data.Parameters:
X- Training data (2D tensor)
Standardize features using fitted statistics.Parameters:
X- Data to transform (2D tensor)
Fit to data and transform in one step.
Transform scaled data back to original scale.
Attributes
After fitting:mean_- Mean of each featurescale_- Standard deviation of each feature
MinMaxScaler
Scale features to a range [min, max]. Formula:X_scaled = (X - X.min) / (X.max - X.min) * (max - min) + min
Constructor
Methods
Compute minimum and maximum from training data.
Scale features to the configured range.
Fit and transform in one step.
Transform scaled data back to original scale.
Attributes
After fitting:dataMin_- Minimum value per featuredataMax_- Maximum value per feature
RobustScaler
Scale features using statistics robust to outliers (median and IQR).Constructor
Methods
Compute median and IQR from training data.
Scale features using robust statistics.
Fit and transform in one step.
Transform scaled data back to original scale.
Attributes
After fitting:center_- Median of each featurescale_- IQR (interquartile range) of each feature
MaxAbsScaler
Scale features by maximum absolute value to range [-1, 1]. Suitable for data already centered at zero.Constructor
Methods
Compute maximum absolute value per feature.
Scale features by maximum absolute value.
Fit and transform in one step.
Transform scaled data back to original scale.
Attributes
After fitting:maxAbs_- Maximum absolute value per feature
Normalizer
Normalize samples (rows) to unit norm. Scales each sample individually to have unit norm (L1, L2, or max).Constructor
Methods
No-op. Normalizer is stateless and does not require fitting.
Normalize each sample to unit norm.
- L2 norm: Euclidean norm √(Σx²)
- L1 norm: Manhattan norm Σ|x|
- Max norm: Maximum absolute value
Transform without fitting (Normalizer is stateless).
PowerTransformer
Apply power transform to make data more Gaussian-like. Supports Box-Cox (strictly positive data) and Yeo-Johnson (any data) transforms.Constructor
Methods
Estimate optimal lambda parameters for each feature using maximum likelihood.Note: Box-Cox requires strictly positive values.
Apply power transform using fitted lambda values.
Fit and transform in one step.
Transform data back to original space.
Attributes
After fitting:lambdas_- Optimal lambda parameter per featuremean_- Mean of transformed features (if standardize=true)scale_- Std of transformed features (if standardize=true)
QuantileTransformer
Transform features using quantile information. Maps features to uniform or normal distribution.Constructor
Methods
Compute quantiles from training data.
Transform features to uniform or normal distribution.
Fit and transform in one step.
Transform data back to original distribution.
Attributes
After fitting:quantiles_- Map of quantile values per feature
Preprocessing Pipeline Example
Combine multiple scalers in a preprocessing workflow:When to Use Each Scaler
StandardScaler
Best for normally distributed features. Sensitive to outliers.
MinMaxScaler
Best when you need a specific range. Sensitive to outliers.
RobustScaler
Best when data contains outliers. Uses median and IQR.
MaxAbsScaler
Best for sparse data that is already centered.
Normalizer
Best for normalizing individual samples (e.g., text vectors).
PowerTransformer
Best for making data more Gaussian. Handles skewed distributions.
QuantileTransformer
Best for non-linear transformations. Robust to outliers.