পাঠ ২০ · ৩০-এর মধ্যে · মডিউল ৩

Feature scaling — standardize, minmax

Feature scaling
৭ মিনিট পড়া মাঝারি · Intermediate StandardScaler, MinMax

এই পাঠে যা শিখবেন

  • Standardization vs normalization — পার্থক্য
  • StandardScaler, MinMaxScaler, RobustScaler — কখন কোনটা
  • Log ও Box-Cox transformation
  • কোন মডেলে scaling অপরিহার্য, কোনটিতে অপ্রয়োজন
  • Train-test scaling pipeline (no leakage)

১ · Scaling কেন দরকার

ভাবুন একটি Daraz customer dataset। "age" 18-65, "income" 5,000-5,00,000 BDT, "n_orders" 0-50। এই তিন column-এর scale ১০০০ গুণ ভিন্ন। distance-based বা gradient-based model এই inequality-তে break করে।

Feature scalingFeature Scalingnumeric column-এর মান একটি comparable range-এ আনা — যাতে বড়-scale variable অন্য variable-কে dominate না করে এবং gradient descent দ্রুত converge হয়। = সব numeric column-কে একটি common scale-এ আনা।

২ · কেন distance-based model affect হয়

Euclidean distance:

$$d = \sqrt{(age_1 - age_2)^2 + (income_1 - income_2)^2}$$

Income-এর difference ১,০০,০০০-এ — square করলে ১০¹⁰। Age difference ৫-এ — square করলে ২৫। Distance প্রায় income-এই! Age-এর effect প্রায় ০।

kNN, k-Means, SVM (RBF kernel), PCA — সবই distance-sensitive। Scaling না করলে — শুধু high-scale feature-এ classify।

৩ · কেন gradient-based model affect হয়

Gradient descent: $w := w - \eta \nabla L$।

  • Income-এর gradient বড় (large input)।
  • Age-এর gradient ছোট।
  • Same learning rate-এ — income weight oscillate, age weight slow learn।
  • Convergence slow বা unstable।

Linear regression, logistic regression, neural network — সবার gradient descent-এ scaling সাহায্য করে।

ভাবুন একটি competition যেখানে আপনি ১০০-মিটার দৌড়াবেন আর প্রতিদ্বন্দ্বী ১০ কিলোমিটার। তুলনা অর্থহীন। যদি দু'জনকে "ফিনিশ লাইন-এর কত শতাংশ" measure করেন — fair comparison। Feature scaling এই concept।

৪ · StandardScaler (z-score)

$$x_{scaled} = \frac{x - \mu}{\sigma}$$

Result: mean = 0, std = 1। Distribution shape-এ কোনো পরিবর্তন না — শুধু shifted ও rescaled।

  • সুবিধা: Normal-distributed data-এ best; linear model-এ standard।
  • সীমা: outlier-এ sensitive (mean ও std affected)।
  • Range: bounded না — যেকোনো value হতে পারে।

৫ · MinMaxScaler

$$x_{scaled} = \frac{x - x_{min}}{x_{max} - x_{min}}$$

Result: [0, 1] range।

  • সুবিধা: bounded range; pixel data, image processing-এ।
  • সীমা: outlier-এ catastrophic — একটি extreme value সব other-কে [0, 0.01]-এ squash।
  • Use: neural network input (sigmoid output-এর সাথে natural fit), embedding visualization।

৬ · RobustScaler

$$x_{scaled} = \frac{x - \text{median}}{IQR}$$

Median ও IQR — outlier-এ stable। Outlier-heavy data-তে preferred।

Sklearn-এ RobustScaler।

৭ · Log ও Box-Cox transformation

Skewed distribution-এ standardize কাজ করে না — distribution-ই বিকৃত। Log transform shape বদলায়।

Log: np.log1p(x) = $\log(1 + x)$ — handles 0।

Box-Cox:

$$y(\lambda) = \begin{cases} \frac{y^\lambda - 1}{\lambda} & \lambda \ne 0 \\ \log y & \lambda = 0 \end{cases}$$

$\lambda$ data থেকে maximum-likelihood-এ estimate। positive value-এ। Yeo-Johnson — negative-ও।

৮ · কোন মডেলে scaling লাগে

Model Scaling দরকার? কেন
Linear Regressionহ্যাঁ (regularization)L1/L2 penalty fair
Logistic Regressionহ্যাঁgradient descent
SVM (RBF)অপরিহার্যdistance kernel
kNNঅপরিহার্যdistance metric
k-Meansঅপরিহার্যdistance metric
PCAঅপরিহার্যvariance-based
Neural Networkঅপরিহার্যgradient + activation
Decision Treeনাsplit-based
Random Forestনাtree ensemble
XGBoost / LightGBMনাtree ensemble
Naive Bayesনা (mostly)probability-based
Tree model scaling-অজ্ঞ — কারণ split "feature ≤ threshold" — monotonic transformation invariant। আপনি age scale করলে — split point automatically adjust।
Feature scaling — choice flow Distribution + outlier + model কোন model? Tree (XGB, RF) No scaling Distance/Gradient scaling required Outlier আছে? distribution skew? RobustScaler (median/IQR) StandardScaler (z-score) Tree → no scaling Outlier → robust Normal → standard
Model-type ও distribution অনুযায়ী scaling method।

৯ · Sklearn-এ scaling — leakage-free pipeline

Python · sklearn
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from sklearn.model_selection import train_test_split

np.random.seed(0)
df = pd.DataFrame({
    'age': np.random.randint(18, 65, 200),
    'income': np.random.lognormal(10, 1, 200),
    'n_orders': np.random.poisson(5, 200)
})

X_train, X_test = train_test_split(df, test_size=0.3, random_state=0)

# Standard
ss = StandardScaler().fit(X_train)
print("Standard scaled (train mean ≈ 0, std ≈ 1):")
print(pd.DataFrame(ss.transform(X_train), columns=df.columns).describe().round(2).loc[['mean','std']])

# MinMax
mm = MinMaxScaler().fit(X_train)
print("\nMinMax scaled (train range [0,1]):")
print(pd.DataFrame(mm.transform(X_train), columns=df.columns).describe().round(2).loc[['min','max']])

# Robust
rs = RobustScaler().fit(X_train)
print("\nRobust scaled (median ≈ 0):")
print(pd.DataFrame(rs.transform(X_train), columns=df.columns).describe().round(2).loc[['min','max','50%']])

    
Critical: fit only on training; transform on both। Test-এ fit করলে — leakage।

১০ · Pipeline-এ wrap

Python · sklearn pipeline
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score

X, y = load_breast_cancer(return_X_y=True)

# Without scaling
pipe_no = Pipeline([
    ('lr', LogisticRegression(max_iter=200))
])

# With scaling
pipe_with = Pipeline([
    ('scaler', StandardScaler()),
    ('lr', LogisticRegression(max_iter=200))
])

import warnings; warnings.filterwarnings('ignore')
print(f"No scaling: {cross_val_score(pipe_no, X, y, cv=5).mean():.4f}")
print(f"With scaling: {cross_val_score(pipe_with, X, y, cv=5).mean():.4f}")

    
Logistic regression-এ scaling-এর সাথে accuracy ও convergence improvement। Pipeline সবসময় preprocessing + model wrap করুন — leakage-proof।

১১ · Bangladesh ML pipeline-এ scaling

  • bKash transaction model: log(amount) → StandardScaler → XGBoost (scaling tree-এ optional, কিন্তু log helpful)।
  • Pathao surge prediction: RobustScaler — outlier (Eid surge) frequent।
  • Daraz CTR (linear/NN): MinMaxScaler — output sigmoid-এর সাথে natural।
  • BRAC microfinance: RobustScaler + log(income) — extreme variance handle।

ভাবনার প্রশ্ন

প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।

প্র ০১ কেন test-এ fit_transform ভুল? "Data leakage"-এর মাধ্যমে accuracy কীভাবে inflated?

এটি ML pipeline-এর সবচেয়ে subtle bug। Beginner-রা প্রায় সবাই এই ভুল করেন।

সঠিক pattern:

  • scaler.fit(X_train) — শুধু train data দেখে mean, std compute।
  • scaler.transform(X_train) — train data scale।
  • scaler.transform(X_test) — same fitted parameter দিয়ে test scale।

ভুল pattern:

  • scaler.fit_transform(X_test) — test-এর own statistics দেখে scale।
  • বা: scaler.fit(X_full) তারপর train/test split — entire data থেকে statistics।

Leakage কেন:

  • Test data future-এর প্রতিনিধি — production-এ সেই data আমরা জানি না।
  • Test-এর mean/std দেখা = future থেকে info "leak" current model-এ।
  • Real production-এ — incoming data scale-এর জন্য historical (train) statistics ব্যবহার।

Concrete example:

  • Train income mean = ৩০,০০০। Test mean = ৫০,০০০ (different distribution)।
  • Wrong: test-এ fit_transform — test-এর mean ০, std ১।
  • Correct: train-এর scaler apply — test-এ mean ~০.৪, std ~১.২।
  • Production-এ মডেল train scale expect করে; test-এ আলাদা fit মানে — model-এর input distribution train-এর সাথে align না।

Accuracy কীভাবে inflated:

(১) Subtle case — same scale

  • Test ও train similar distribution হলে — leakage minor।
  • Accuracy ০.১-১% inflated।
  • Detection difficult।

(২) Severe case — distribution shift

  • Train historical, test recent।
  • Test-এ fit করে "look correct" — কিন্তু model production-এ fail।
  • Apparent accuracy ৯৫%, production reality ৭০%।

(৩) Cross-validation leakage

  • fit on full data, then split — fold-এর holdout-এর info-ও fit-এ entered।
  • K-fold CV inflated।

Sklearn Pipeline-এর role:

  • Pipeline([('scaler', ...), ('model', ...)]) — automatic correct।
  • CV-এ প্রতিটি fold-এ scaler নতুন fit।
  • Manual scaling করার দরকার নেই; Pipeline-এ wrap।

Time-series-এ আরও critical:

  • Train: ২০২০-২০২৩, test: ২০২৪।
  • ২০২৪-এর data দিয়ে fit = future leakage absolute।
  • Walk-forward validation: প্রতিটি step-এ train পর্যন্ত fit।

Detection signs:

  • Train accuracy ও test accuracy gap বড়।
  • Cross-validation score > production score।
  • Distribution drift evident।

Real-world consequences:

  • Bangladesh fintech model: leakage-due to "fraud rate ৯৯%" claim — production ৭০%।
  • Healthcare model: train-test confusion → FDA review reject।
  • Reputation damage — ML team trust।

Best practices:

  • Pipeline সর্বদা।
  • Train/test split first, preprocess second।
  • Save fitted scaler — production-এ reuse।
  • Test-এ separate validation set।

মূল উপলব্ধি: Test = future। Future থেকে info current decision-এ leak করা = unethical + technically wrong। Pipeline + discipline = leakage-free ML।

প্র ০২ একটি skewed income column-এ আপনি কোনটা করবেন: log(x) → StandardScaler বা শুধু StandardScaler? কেন?

এটি real production decision। Log + Standard প্রায় সব time better — কেন?

Skewed data-তে শুধু StandardScaler-এর সমস্যা:

  • Distribution shape unchanged — শুধু shift ও rescale।
  • Skewness preserved।
  • Long tail এখনো long (just shifted)।
  • Outlier still outlier।

Income-এর realistic distribution (Bangladesh):

  • Median ~২৫,০০০ BDT।
  • ৯০th percentile ~৭৫,০০০।
  • ৯৯th percentile ~৩,০০,০০০।
  • Top earner ~৫০,০০,০০০+।
  • Long right tail — extreme skewness।

StandardScaler-এ:

  • Mean ~৪০,০০০, std ~৩,০০,০০০ (extreme outlier inflate)।
  • Most user z-score = (২৫০০০ - ৪০০০০)/৩০০০০০ = -০.০৫।
  • সবাই মাঝামাঝি, কিন্তু extreme এ +১০, +২০।
  • Linear/NN model-এ — extreme dominate gradient।

Log + StandardScaler:

  • $\log(25000) = 10.13$, $\log(50000) = 10.82$, $\log(5000000) = 15.42$।
  • Log-income-এর mean ~১১, std ~১.৫।
  • Distribution near-Gaussian।
  • Z-score বেশিরভাগ [-2, +2]; extreme +৩।
  • Linear model — well-behaved।

Why log:

  • Income-এ multiplicative structure: ১০x increase = comparable jump।
  • Log additive-এ convert।
  • "৫০K vs ৫০০K" — logically same step (10x) as "৫০০K vs ৫০০০K"।
  • Linear coefficient interpretation: "log-income unit increase → y change" = "income 2.71x → y change"।

Mathematical justification — Lognormal distribution:

  • Income often lognormal (multiplicative process)।
  • Log-income normal — Gaussian assumptions hold।
  • Linear regression OLS ভালো work।

When log-StandardScaler ভাল:

  • Right-skewed data: income, transaction, population, prices।
  • Multiplicative relationship।
  • Log-normal distribution suspected।

When just StandardScaler enough:

  • Already near-normal (e.g., heights, BMI)।
  • Bounded data (e.g., percentages, ratings)।
  • Already log-transformed at source।

When neither — RobustScaler:

  • Outlier extreme but not necessarily skewed।
  • Median + IQR robust।

Box-Cox alternative:

  • PowerTransformer(method='box-cox') — log-এর generalization।
  • $\lambda$ data-driven choice।
  • Need positive values; Yeo-Johnson handles negative।

Practical recipe:

from sklearn.preprocessing import StandardScaler
import numpy as np

X_log = np.log1p(X_income)
scaler = StandardScaler().fit(X_log_train)
X_scaled = scaler.transform(X_log)

Or scikit-learn pipeline:

from sklearn.preprocessing import FunctionTransformer
pipe = Pipeline([
    ('log', FunctionTransformer(np.log1p)),
    ('scale', StandardScaler()),
    ('model', LogisticRegression())
])

Empirical check:

  • Plot histogram before/after।
  • Skewness before: 5+; after log: ~0।
  • Q-Q plot — log-transformed data straight line near-Normal।

Bangladesh use cases:

  • Microfinance loan amount: log মাস্ট।
  • bKash transaction: log।
  • Real estate price: log।
  • Grameenphone usage MB: log।

মূল উপলব্ধি: StandardScaler "skew" সরায় না — শুধু center ও rescale। Skewed data → log first, scale second। দু'ধাপ pipeline financial/economic feature-এ standard।

প্র ০৩ "XGBoost-এ scaling-এ accuracy improvement নেই" — এই claim true? কোন situation-এ exception?

মোটামুটি true, কিন্তু nuance আছে। Tree-based model-এর mathematical invariance কেন এবং কখন break হয়।

কেন tree scaling-অজ্ঞ:

  • Decision tree-এর split: "feature ≤ threshold"।
  • Threshold value-এ optimize হয়।
  • $x \to a \cdot x + b$ — threshold automatically adjust।
  • Information gain (or Gini) split-এ same।
  • মডেল identical performance, identical structure।

Mathematical statement:

Tree model monotonic transformation-এর প্রতি invariant। $f(x)$ monotonic হলে — tree on $x$ ও tree on $f(x)$ same predictions।

Validation:

import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

rf1 = RandomForestClassifier(random_state=0).fit(X, y)
rf2 = RandomForestClassifier(random_state=0).fit(X_scaled, y)

print(f"Original: {rf1.score(X, y):.4f}")
print(f"Scaled: {rf2.score(X_scaled, y):.4f}")

Result: identical accuracy। Same random_state সব split same threshold-value (scaled adjusted)।

Exception ১: Categorical interaction

  • One-hot-এ ০/১ values।
  • Numeric "income" 100,000-এ — split গভীরে ঢুকে।
  • One-hot column কম informative নয়, কিন্তু tree algorithm subtly biased toward continuous feature।
  • Workaround: gradient boosting parameters (min_samples_split, min_child_weight) tune।

Exception ২: Regularization

  • XGBoost reg_alpha (L1), reg_lambda (L2)।
  • Penalty leaf weight-এ — leaf weight feature scale-এর সাথে correlated।
  • Scaled feature-এ penalty more uniform।
  • Marginal effect; usually negligible।

Exception ৩: Distance-based loss

  • Standard XGBoost loss (binary cross-entropy, RMSE) scaling-অজ্ঞ।
  • Custom distance-based loss (rare) সাজাতে পারে।

Exception ৪: Numerical precision

  • Feature value 10^10 vs 10^-10 — float precision issue।
  • Practical না; production data এত extreme rare।

Exception ৫: Skewness

  • Tree-এ scaling-অজ্ঞ, কিন্তু log-transform-এ improvement আসতে পারে।
  • Long tail-এ split point কম optimal — log compress করে।
  • Tested: usually 0.5-1% AUC improvement।

Practical guidance for tree models:

  • Don't waste time scaling — same accuracy।
  • Skewness extreme হলে — log transform try।
  • Categorical feature handling — native (LightGBM) বা target encoding।
  • Missing — XGBoost native handle।

When NOT scaling helps tree pipeline:

  • Faster training: no preprocessing step।
  • Interpretability: original feature value visible in feature_importance।
  • Debugging: raw value-এ understand splits।
  • Production: simpler pipeline, less code।

Hybrid pipeline scenario:

  • Stacking: tree (no scale) + linear model (scale)।
  • Different preprocessing per branch।
  • Feature pipeline complexity।

Production Bangladesh examples:

  • Daraz CTR (XGBoost): no scaling।
  • bKash fraud (LightGBM): no scaling।
  • Pathao surge (linear+RF): scaling for linear, not RF।

Counterintuitive finding (research):

  • Some papers report 0.1-0.3% improvement with scaling for XGBoost।
  • Statistical noise + random_state variation।
  • No reliable systematic gain।

মূল উপলব্ধি: Tree model scaling-এ time waste। Skewness থাকলে — log transform। Pipeline simplicity gain। অন্য model-এর সাথে compare-এ — same/better baseline।

প্র ০৪ StandardScaler vs MinMaxScaler vs RobustScaler — তিনটির দাঁড়ানোর জায়গা কোথায়? Examples দিয়ে compare।

তিনটি scaler-এর use case ভিন্ন। Decision criteria বুঝলে — সঠিক choice automatic।

StandardScaler — z-score

$$x' = \frac{x - \mu}{\sigma}$$

  • Mean = 0, std = 1।
  • Range unbounded।
  • Normal-distributed-এ optimal।

Use case:

  • Linear regression — Gaussian assumption।
  • Logistic regression — gradient descent।
  • SVM (linear, RBF) — margin computation।
  • PCA — variance-based।
  • Most ML default।

Limitation:

  • Outlier-এ mean ও std affected।
  • Skewed data-এ misleading।

Bangladesh example:

  • Customer age (18-65, near-normal): StandardScaler perfect।
  • Test score (0-100, near-normal): StandardScaler।
  • BMI: StandardScaler।

MinMaxScaler — [0, 1]

$$x' = \frac{x - x_{min}}{x_{max} - x_{min}}$$

  • Bounded।
  • All values [0, 1]।
  • Distribution shape unchanged।

Use case:

  • Image pixel data (0-255 → 0-1)।
  • Neural network input (sigmoid output natural)।
  • Features must be bounded (e.g., embedding similarity)।
  • Visualization (axes 0-1)।

Limitation:

  • Outlier extreme: একটি extreme value সব others-কে [0, 0.001]-এ squash।
  • New extreme value test-এ > 1 (out of [0,1] range)।

Bangladesh example:

  • Image classification (Daraz product image): pixel/255।
  • Daraz rating (1-5): MinMax → (rating-1)/4।
  • Probability score (0-1): already normalized, MinMax identity।

RobustScaler — median ও IQR

$$x' = \frac{x - \text{median}}{IQR}$$

  • Median ও IQR — outlier-এ stable।
  • Range unbounded।
  • Normal-এ standard-এর চেয়ে কম সাপ্ত (factor ~1.35×)।

Use case:

  • Outlier-heavy data: income, transaction amount।
  • Sensor data with malfunction।
  • Financial returns (occasional crash)।

Limitation:

  • Normal data-এ slightly less efficient than StandardScaler।
  • Outlier preserve — drop করতে পারে না।

Bangladesh example:

  • bKash transaction amount (extreme outlier): RobustScaler।
  • BSEC stock returns: RobustScaler।
  • Microfinance loan recovery: RobustScaler (default extreme)।

Side-by-side comparison example:

  • Data: $[1, 2, 3, 4, 5, 100]$ (one outlier)।
  • Mean = 19.2, std = 39.5।
  • Median = 3.5, IQR = 2.75।

StandardScaler:

  • $[1, 2, 3, 4, 5, 100] \to [-0.46, -0.44, -0.41, -0.39, -0.36, 2.04]$
  • প্রায় সবাই -0.4 কাছাকাছি; একটি 2.0।

MinMaxScaler:

  • $\to [0, 0.01, 0.02, 0.03, 0.04, 1.0]$
  • বেশিরভাগ near-০; একটি ১।
  • Extreme outlier domination।

RobustScaler:

  • $\to [-0.91, -0.55, -0.18, 0.18, 0.55, 35.1]$
  • Inlier-গুলো well-spread; outlier extreme।
  • Best for inlier discrimination।

Decision flowchart:

  1. Outlier আছে? → RobustScaler।
  2. Bounded range দরকার (image, NN)? → MinMaxScaler।
  3. Normal-distributed? → StandardScaler।
  4. Skewed? → log + StandardScaler।

Empirical recommendation:

  • Default: StandardScaler।
  • Outlier > 5%: RobustScaler।
  • Image/NN: MinMaxScaler।
  • Highly skewed: PowerTransformer (Yeo-Johnson)।

Multi-scaler ensemble:

  • প্রতিটি feature-এ different scaler — ColumnTransformer।
  • income: log + StandardScaler।
  • age: StandardScaler।
  • rating: MinMaxScaler।
  • Production code complexity বাড়ে কিন্তু performance optimal।

মূল উপলব্ধি: "একটি scaler সবকিছুতে" — naive approach। Feature-by-feature scaling decision — production ML excellence-এর pattern।

অনুশীলন

  1. Manual z-score: $[10, 20, 30, 40, 50]$-এ z-score বের করুন।

    Mean = 30, std (population) = 14.14।

    Z = $(x - 30)/14.14$:

    $[(10-30)/14.14, ..., (50-30)/14.14] = [-1.41, -0.71, 0, 0.71, 1.41]$।

    Sklearn StandardScaler default population std use; numpy std-এর ddof=0 default।

  2. Pipeline practice: Sklearn-এ Logistic Regression-এর জন্য একটি Pipeline বানান যেখানে log + StandardScaler + LR। Breast Cancer dataset-এ cross-validate।
    from sklearn.pipeline import Pipeline
    from sklearn.preprocessing import StandardScaler, FunctionTransformer
    from sklearn.linear_model import LogisticRegression
    from sklearn.datasets import load_breast_cancer
    from sklearn.model_selection import cross_val_score
    import numpy as np
    
    X, y = load_breast_cancer(return_X_y=True)
    pipe = Pipeline([
        ('log', FunctionTransformer(np.log1p)),
        ('scale', StandardScaler()),
        ('lr', LogisticRegression(max_iter=500))
    ])
    print(f"CV mean: {cross_val_score(pipe, X, y, cv=5).mean():.4f}")
  3. RobustScaler vs StandardScaler: ১০০ row normal data + ৫ outlier inject। দু'টি scaler দিয়ে scale করে test set-এ kNN classify। কোনটা ভাল?

    RobustScaler সাধারণত better — কারণ outlier-এ mean ও std inflated, valid inlier-গুলো compressed। RobustScaler median/IQR — inlier-এ অপরিবর্তিত।

    from sklearn.preprocessing import StandardScaler, RobustScaler
    from sklearn.neighbors import KNeighborsClassifier
    # compare cross_val_score

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? Google Colab ব্যবহার করুন।
পূর্ববর্তী পাঠ
পাঠ ১৯ · Categorical encoding