API Reference / preprocessing / scaling /

standardize.py

StandardScaler transformer.

Z-score standardization (zero mean, unit variance).

Classes

StandardScaler

class preprocessing.scaling.standardize.StandardScaler(Transformer)

Zero-mean and unit-variance standardization.

Standardizes features by removing the mean and scaling to unit variance. This is often a prerequisite for many machine learning estimators.
Constructor
__init__(
    self,
    with_mean: bool = True,
    with_std: bool = True,
    columns: Optional[List[int]] = None,
)

Overview

Standardization (or Z-score normalization) transforms each feature such that it has a mean of 0 and a standard deviation of 1.

Theory

The standard score of a sample x is calculated as:

z = \frac{x - \mu}{\sigma}

where \mu is the mean of the training samples and \sigma is the standard deviation.

Parameters

with_mean
bool = True
If True, center the data before scaling by subtracting the mean.
with_std
bool = True
If True, scale the data to unit variance (standard deviation of 1).
columns
list of int
Indices of columns to transform. If None, all numerical columns are transformed.

Attributes

mean_
np.ndarray of shape (n_selected_columns,)
The mean value for each feature in the training set.
std_
np.ndarray of shape (n_selected_columns,)
The standard deviation for each feature in the training set.

Notes

When to use:
  • For algorithms sensitive to the scale of features (e.g., SVM, k-NN, Logistic Regression).
  • When features follow a Gaussian-like distribution.
  • Before dimensionality reduction (PCA).
Implementation Details:
  • Uses np.nanmean and np.nanstd to be robust to missing values.
  • Handles zero variance by setting the scale to 1.0 to avoid division by zero.

Standardize a simple 2D array:

python
>>> from tuiml.preprocessing.scaling import StandardScaler
>>> import numpy as np
>>> X = np.array([[1, 2], [3, 4], [5, 6]])
>>> scaler = StandardScaler()
>>> X_std = scaler.fit_transform(X)
>>> print(np.round(X_std.mean(axis=0), 2))
[0. 0.]

Methods

get_parameter_schema (cls)
fit (self, X: np.ndarray, y: Optional[np.ndarray]=None, feature_names: Optional[List[str]]=None) -> 'StandardScaler'
transform (self, X: np.ndarray) -> np.ndarray
inverse_transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str