← Back to Data Preprocessing Technique
🔍🧹📊 Data Preprocessing Technique

Box-Cox Transformation

📅 May 15, 2026

Machine Learning · Statistical Transformation

Box-Cox Transformation

Core Formula
y(λ) = (xᵛ − 1) / λ   [λ ≠ 0]
y(λ) = log(x)          [λ = 0]
1

Box-Cox Transformation भनेको के हो?

💡 Simple भाषामा: "Data अनुसार best transformation आफैं खोज्ने SMART technique"
📉
Skewness घटाउने
⚖️
Variance Stabilize
🔔
Normal बनाउने
🤖
Model Improve
 
2

Visual Graph — Before vs After Transformation

❌ Before — Right Skewed
█████████████████ ← 100%
█████████████ ← 75%
████████ ← 50%
█████ ← 28%
███ ← 14%
← 6%
← 2%
Low ◄─────────────────► High (outlier)
Tail right मा लामो — most data left मा थुप्रिएको
✅ After Box-Cox — Near Normal
░░░░█████████░░░░ ← peak
░░█████████████░░ ← 85%
███████████████ ← 95%
█████████████████ ← 100%
███████████████ ← 95%
░░█████████████░░ ← 85%
░░░░█████████░░░░ ← peak
Low ◄──── Bell Curve ─────► High
Bell Curve — symmetric, balanced ✓
 
3

Lambda (λ) — The Heart of Box-Cox

λ Value अनुसार Transformation Shape
λ = -1   Reciprocal (1/x) — rapid drop
████████████████████ x=1
██████████ x=2
███████ x=3
█████ x=4
████ x=5
λ = 0   Log Transform ⭐ — gradual slow curve
██████ x=1
█████████ x=2
███████████ x=3
████████████ x=4
█████████████ x=5
λ = 0.5   Square Root (√x) — moderate curve
████████ x=1
███████████ x=2
█████████████ x=3
███████████████ x=4
████████████████ x=5
λ = 1   No Change (x) — straight line
████ x=1
████████ x=2
████████████ x=3
████████████████ x=4
████████████████████ x=5
λ = 2   Square (x²) — rapid growth
████ x=1
████████████████ x=2
████████████████████████████████████ x=3
→ exponential growth
λ Transformation Formula Use When
-1 Reciprocal 1/x Heavy left skew
0 ⭐ Log Transform log(x) Most common!
0.5 Square Root √x Moderate skew
1 No Change x Already normal
2 Square Left skewed data
 

Most Important Insight

Key Concept
Log Transformation वास्तवमा
Box-Cox को Special Case हो!
When λ = 0  →  Box-Cox = Log(x)
Algorithm — Best λ कसरी छान्छ? (Log-Likelihood)
███ λ = -2.0   (low)
██████ λ = -1.5
█████████ λ = -1.0
████████████ λ = -0.5
████████████████████ ← λ = 0   BEST! (highest)
████████████ λ = 0.5
█████████ λ = 1.0
██████ λ = 1.5
███ λ = 2.0   (low)
↑ Log-Likelihood Score (algorithm automatically picks highest)
 
4

Log vs Box-Cox — Visual Comparison

Same Skewed Data — तीनवटाको Distribution Effect
① Original (Skewed)
████████████████
███████████
███████
████
██
Right skewed ✗
② After Log
████
████████████
████████████████
████████████
███████
███
Better — slight skew
③ Box-Cox (optimal λ)
██
████████
████████████████
████████████████████
████████████████
████████
██
Most Normal ✓✓
Feature Log Box-Cox
Type Fixed formula Adaptive
Flexibility ❌ Less ✅ High
Optimization ❌ Manual ✅ Auto (MLE)
Speed ✅ Fast ⚡ Slower
Best Normalization ⚡ Good ✅ Best
 
5

Variance Stabilization

❌ Before — Heteroscedasticity
x=1: ▌▌ (small spread)
x=2: ▌▌▌▌
x=3: ▌▌▌▌▌▌▌▌
x=4: ▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌
x=5: ▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌ ← huge!
Variance x बढ्दै जाँदा बढ्छ ↑
✅ After — Homoscedasticity
x=1: ▌▌▌▌ (stable)
x=2: ▌▌▌▌▌
x=3: ▌▌▌▌
x=4: ▌▌▌▌▌
x=5: ▌▌▌▌ ← same!
Variance everywhere same ✓
 
6

Zero / Negative Values + Python Code

⚠️ Important: Box-Cox only works for x > 0 (positive only)
❌ Problem
BoxCox(0) → Error!
BoxCox(-5) → Error!
✅ Solution — Shift गर्नुस्
x_new = x - min(x) + 1
BoxCox(x_new) → Works ✓
🐍 Scikit-Learn Code
from sklearn.preprocessing import PowerTransformer
pt = PowerTransformer(method='box-cox')
X_t = pt.fit_transform(X)
# For negative values use:
pt2 = PowerTransformer(method='yeo-johnson')
 
7

कुन ML Model मा Use गर्ने?

Model Useful? Reason
Linear Regression ✅ Very Important Normality assume गर्छ
Ridge / Lasso / ElasticNet ✅ Important Coefficient stability
ARIMA / Time Series ✅ Important Stationarity improve
ANOVA / t-test ✅ Important Normality required
Random Forest / XGBoost ⚡ Optional Distribution-independent
 
⭐ Interview Answer
Q: "Difference between Log and Box-Cox?"
Log = fixed transformation. Box-Cox = data अनुसार optimal power transformation auto-select गर्छ। Log वास्तवमा Box-Cox को λ=0 special case हो।
Log
"ठूलो value compress गर्ने simple fixed technique"
Box-Cox
"Data अनुसार best transformation खोज्ने smart technique"
Box-Cox Transformation · Complete Notes · ML Feature Engineering

← Back to Data Preprocessing Technique