torch_concepts.data.scalers.standard.StandardScaler¶
- class StandardScaler(axis: int | Tuple = 0)[source]¶
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)
- __init__(axis: int | Tuple = 0)[source]¶
Initialize the StandardScaler. :param axis: Axis or axes along which to compute statistics (default: 0).
Methods
__init__([axis])Initialize the StandardScaler.
fit(x)Compute mean and standard deviation along specified dimension.
fit_transform(x[, dim])Fit the scaler and transform the input data in one operation.
Reverse the standardization to recover original scale.
transform(x)Standardize the input tensor using fitted statistics.