Scalers

This module provides data scaling utilities for normalization and standardization.

Summary

Scaler Classes

StandardScaler

Z-score normalization scaler for PyTorch tensors.

Class Documentation

class StandardScaler(axis: int | Tuple = 0)[source]

Bases: Scaler

Z-score normalization scaler for PyTorch tensors.

Standardizes features by removing the mean and scaling to unit variance:

z = (x - μ) / σ

This scaler is useful for: - Normalizing input features before training - Ensuring all features are on the same scale - Improving gradient flow and training stability

Parameters:

axis (Union[int, Tuple], optional) – Axis or axes along which to compute mean and standard deviation. Typically 0 (across samples) for feature-wise normalization. Defaults to 0.

mean

Computed mean value(s) from fitted data.

Type:

Tensor

std

Computed standard deviation(s) from fitted data.

Type:

Tensor

Example

>>> # Normalize a batch of features
>>> scaler = StandardScaler(axis=0)
>>> X_train = torch.randn(1000, 50)  # 1000 samples, 50 features
>>> X_train_scaled = scaler.fit_transform(X_train)
>>>
>>> # Transform test data using training statistics
>>> X_test = torch.randn(200, 50)
>>> X_test_scaled = scaler.transform(X_test)
>>>
>>> # Inverse transform to original scale
>>> X_recovered = scaler.inverse_transform(X_test_scaled)
fit(x: Tensor) StandardScaler[source]

Compute mean and standard deviation along specified dimension. :param x: Input tensor to compute statistics from.

Returns:

The fitted scaler instance for method chaining.

Return type:

self

transform(x: Tensor) Tensor[source]

Standardize the input tensor using fitted statistics. :param x: Input tensor to standardize.

Returns:

Standardized tensor with zero mean and unit variance.

inverse_transform(x: Tensor) Tensor[source]

Reverse the standardization to recover original scale. :param x: Standardized tensor to inverse-transform.

Returns:

Tensor in original scale.