Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

StandardScaler / MinMaxScaler / RobustScaler / MaxAbsScaler / Normalizer

API Reference

Signature

scaler = sp.StandardScaler(with_mean=True, with_std=True)
scaler = sp.MinMaxScaler(feature_range=(0.0, 1.0))
scaler = sp.RobustScaler(with_centering=True, with_scaling=True, quantile_range=(25.0, 75.0))
scaler = sp.MaxAbsScaler()
scaler = sp.Normalizer(norm="l2")

scaler.fit(X)
X_scaled  = scaler.transform(X)       -> ndarray
X_back    = scaler.inverse_transform(X_scaled) -> ndarray   # most scalers
X_scaled  = scaler.fit_transform(X)   -> ndarray

Constructor parameters — StandardScaler

ParameterTypeDefaultDescription
with_meanboolTrueSubtract the mean
with_stdboolTrueDivide by standard deviation

Constructor parameters — MinMaxScaler

ParameterTypeDefaultDescription
feature_rangetuple(float,float)(0.0, 1.0)Desired output range

Constructor parameters — RobustScaler

ParameterTypeDefaultDescription
with_centeringboolTrueSubtract the median
with_scalingboolTrueDivide by IQR
quantile_rangetuple(float,float)(25.0, 75.0)Quantile range for IQR

Constructor parameters — Normalizer

ParameterTypeDefaultDescription
normstr"l2"Per-sample norm: "l1", "l2", "max"

Attributes

AttributeTypeDescription
mean_list[float]Per-feature mean (StandardScaler)
scale_list[float]Per-feature scale factor
min_list[float]Per-feature minimum (MinMaxScaler)
data_range_list[float]Per-feature range (MinMaxScaler)
center_list[float]Per-feature median (RobustScaler)
max_abs_list[float]Per-feature maximum absolute value (MaxAbsScaler)
Example
import seraplot as sp
import numpy as np

X_train = np.random.randn(300, 4) * [5, 2, 0.1, 100]
X_test  = np.random.randn(50, 4)  * [5, 2, 0.1, 100]

scaler = sp.StandardScaler()
X_tr = scaler.fit_transform(X_train)
X_te = scaler.transform(X_test)
print(f"Train mean≈0: {X_tr.mean(0).round(3)}")

mm = sp.MinMaxScaler(feature_range=(0.0, 1.0))
print(f"MinMax range: {mm.fit_transform(X_train).min(0).round(3)}, {mm.fit_transform(X_train).max(0).round(3)}")

Algorithmic Functioning

Each scaler applies a column-wise linear transformation fitted on the training set and applied identically to any new data.


StandardScaler

Standardises features to zero mean and unit variance:

$$x'_j = \frac{x_j - \mu_j}{\sigma_j}$$

where $\mu_j = \frac{1}{n}\sum_i x_{ij}$ and $\sigma_j = \sqrt{\frac{1}{n}\sum_i (x_{ij}-\mu_j)^2}$.


MinMaxScaler

Rescales each feature into the interval $[a, b]$ (feature_range):

$$x'_j = a + \frac{x_j - \min_j}{\max_j - \min_j}(b - a)$$

Sensitive to outliers since it uses $\min$ and $\max$.


RobustScaler

Uses median and interquartile range (IQR), making it robust to outliers:

$$x'_j = \frac{x_j - Q_{50}(j)}{Q_{75}(j) - Q_{25}(j)}$$

where $Q_p(j)$ is the $p$-th percentile of feature $j$.


MaxAbsScaler

Scales each feature by its maximum absolute value, preserving sparsity and the origin:

$$x'_j = \frac{x_j}{\max_i |x_{ij}|}$$

Result lies in $[-1, 1]$.


Normalizer

Scales each sample (row) to unit norm, applied independently of fit:

$$x'_i = \frac{x_i}{\|x_i\|_p}, \qquad p \in \{1, 2, \infty\}$$

No fit step is required — the transformation is stateless.

Inverse transform is defined for StandardScaler, MinMaxScaler, RobustScaler, and MaxAbsScaler:

$$x_j = x'_j \cdot \text{scale}_j + \text{center}_j$$

Référence API

Signature

scaler = sp.StandardScaler(with_mean=True, with_std=True)
scaler = sp.MinMaxScaler(feature_range=(0.0, 1.0))
scaler = sp.RobustScaler(with_centering=True, with_scaling=True, quantile_range=(25.0, 75.0))
scaler = sp.MaxAbsScaler()
scaler = sp.Normalizer(norm="l2")

scaler.fit(X)
X_scaled  = scaler.transform(X)       -> ndarray
X_back    = scaler.inverse_transform(X_scaled) -> ndarray   # la plupart des scalers
X_scaled  = scaler.fit_transform(X)   -> ndarray

Paramètres du constructeur — StandardScaler

ParamètreTypeDéfautDescription
with_meanboolTrueSoustraire la moyenne
with_stdboolTrueDiviser par l'écart-type

Paramètres du constructeur — MinMaxScaler

ParamètreTypeDéfautDescription
feature_rangetuple(float,float)(0.0, 1.0)Plage de sortie souhaitée

Paramètres du constructeur — RobustScaler

ParamètreTypeDéfautDescription
with_centeringboolTrueSoustraire la médiane
with_scalingboolTrueDiviser par l'IQR
quantile_rangetuple(float,float)(25.0, 75.0)Plage de quantiles pour l'IQR

Paramètres du constructeur — Normalizer

ParamètreTypeDéfautDescription
normstr"l2"Norme par échantillon : "l1", "l2", "max"

Attributs

AttributTypeDescription
mean_list[float]Moyenne par feature (StandardScaler)
scale_list[float]Facteur d'échelle par feature
min_list[float]Minimum par feature (MinMaxScaler)
data_range_list[float]Plage par feature (MinMaxScaler)
center_list[float]Médiane par feature (RobustScaler)
max_abs_list[float]Valeur absolue maximale par feature (MaxAbsScaler)
Exemple
import seraplot as sp
import numpy as np

X_train = np.random.randn(300, 4) * [5, 2, 0.1, 100]
X_test  = np.random.randn(50, 4)  * [5, 2, 0.1, 100]

scaler = sp.StandardScaler()
X_tr = scaler.fit_transform(X_train)
X_te = scaler.transform(X_test)
print(f"Moyenne train≈0 : {X_tr.mean(0).round(3)}")

mm = sp.MinMaxScaler(feature_range=(0.0, 1.0))
print(f"Plage MinMax : {mm.fit_transform(X_train).min(0).round(3)}, {mm.fit_transform(X_train).max(0).round(3)}")

Fonctionnement algorithmique

Chaque scaler applique une transformation linéaire colonne par colonne ajustée sur l'ensemble d'entraînement et appliquée de manière identique à toute nouvelle donnée.


StandardScaler

Standardise les features à moyenne nulle et variance unitaire :

$$x'_j = \frac{x_j - \mu_j}{\sigma_j}$$

où $\mu_j = \frac{1}{n}\sum_i x_{ij}$ et $\sigma_j = \sqrt{\frac{1}{n}\sum_i (x_{ij}-\mu_j)^2}$.


MinMaxScaler

Redimensionne chaque feature dans l'intervalle $[a, b]$ (feature_range) :

$$x'_j = a + \frac{x_j - \min_j}{\max_j - \min_j}(b - a)$$

Sensible aux valeurs aberrantes car il utilise $\min$ et $\max$.


RobustScaler

Utilise la médiane et l'écart interquartile (IQR), le rendant robuste aux valeurs aberrantes :

$$x'_j = \frac{x_j - Q_{50}(j)}{Q_{75}(j) - Q_{25}(j)}$$

où $Q_p(j)$ est le $p$-ième percentile de la feature $j$.


MaxAbsScaler

Met à l'échelle chaque feature par sa valeur absolue maximale, préservant la sparsité et l'origine :

$$x'_j = \frac{x_j}{\max_i |x_{ij}|}$$

Le résultat est dans $[-1, 1]$.


Normalizer

Met à l'échelle chaque échantillon (ligne) à une norme unitaire, appliqué indépendamment du fit :

$$x'_i = \frac{x_i}{\|x_i\|_p}, \qquad p \in \{1, 2, \infty\}$$

Aucune étape fit n'est requise — la transformation est sans état.

Transformation inverse définie pour StandardScaler, MinMaxScaler, RobustScaler et MaxAbsScaler :

$$x_j = x'_j \cdot \text{scale}_j + \text{center}_j$$