Skip to main content

Lesson 6 of 9

Non-Linear Feature Transformations: Box-Cox, Yeo-Johnson & Quantile

Many real-world features exhibit non-Gaussian distributions: power laws, Pareto tails, and heavy bimodal spreads. Non-linear transformations reshape skewed feature distributions into Gaussian-like shapes. Scikit-Learn's `PowerTransformer` (Box-Cox and Yeo-Johnson) and `QuantileTransformer` map empirical distributions directly onto uniform or normal targets, stabilizing variance and enhancing linear model fit.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Comparing PowerTransformer vs QuantileTransformer in Code

Comparing PowerTransformer vs QuantileTransformer in Code
import numpy as np
import scipy.stats as stats
from sklearn.preprocessing import PowerTransformer, QuantileTransformer

# Generate lognormal data with extreme right-skew
np.random.seed(42)
raw_data = np.random.lognormal(mean=3.0, sigma=1.5, size=2000).reshape(-1, 1)

print(f"Raw Data Skewness: {stats.skew(raw_data)[0]:.4f}")

# 1. Yeo-Johnson Power Transformation (parametric variance stabilizing)
# Solves for optimal parameter lambda via maximum likelihood estimation
pt = PowerTransformer(method='yeo-johnson', standardize=True)
data_yj = pt.fit_transform(raw_data)
print(f"Yeo-Johnson Skewness: {stats.skew(data_yj)[0]:.4f} (Near zero = symmetric Gaussian!)")

# 2. QuantileTransformer (Non-parametric rank transformation)
# Maps empirical CDF directly onto a Gaussian normal distribution
qt = QuantileTransformer(output_distribution='normal', n_quantiles=1000, random_state=42)
data_qt = qt.fit_transform(raw_data)
print(f"QuantileTransformer Skewness: {stats.skew(data_qt)[0]:.4f}")

# Verify tail behavior
print(f"Raw 99th percentile: {np.percentile(raw_data, 99):.2f}")
print(f"Yeo-Johnson 99th percentile: {np.percentile(data_yj, 99):.2f} (Standard Gaussian z-score)")
print(f"Quantile 99th percentile: {np.percentile(data_qt, 99):.2f}")

Pro Tip: QuantileTransformer for Neutralizing Outliers

`QuantileTransformer` is a non-linear rank transformer that ranks all values and maps them onto a uniform or normal distribution. Because it operates on percentiles rather than absolute magnitudes, extreme outliers are automatically pulled into the standard distribution tails without needing manual clipping.

Diagram
Loading diagram…

MlFeatureTransformations Visual Representation

Hands-On Challenge: Build a Reversible Target Transformed Regressor

Use `sklearn.compose.TransformedTargetRegressor` to wrap a `Ridge` model with a `PowerTransformer(method='box-cox')` target transformation. Verify that predictions automatically transform back into natural dollar units during `.predict()`.

Quick Quiz & Key Concept Checks

Common Pitfall: Using Too Few Quantiles in QuantileTransformer

If `n_quantiles` is set larger than the number of training samples, Scikit-Learn will raise an error. Ensure `n_quantiles <= n_samples` and use sensible subsampling when dealing with multi-million row datasets.

Topics in This Module

0/9 done