পাঠ ১৬ · ৪৫-এর মধ্যে · মডিউল ২
Home / AI Courses / Machine Learning / Elastic Net

Elastic Net

Elastic Net
৬ মিনিট পড়া মাঝারি · Intermediate sklearn কোডসহ

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

  • Pure Lasso-এর সীমাবদ্ধতা — কেন Ridge মেশানো দরকার
  • Elastic Net formula, geometry, ও hyperparameters
  • Group selection — correlated features কীভাবে handle হয়
  • scikit-learn দিয়ে hyperparameter tuning

১ · Lasso-এর সমস্যা

L15-এ আমরা Lasso-র sparsity দেখেছি। কিন্তু Lasso-র দু'টি limitation আছে:

  • $N < p$ regime: Lasso সর্বোচ্চ $N$ feature select করে। ১০০০ features, ১০০ samples — মাত্র ১০০ ধরবে।
  • Correlated features instability: দু'টি highly correlated features-এর মধ্যে Lasso randomly একটি বাছে — অন্যটি drop। Different sample-এ different choice।
  • Group of correlated features: Lasso "all-or-nothing" — পুরো group select বা drop করতে পারে না।

Real data — features often correlated। Genomics, finance, marketing — সর্বত্র। Lasso pure form-এ unstable।

২ · Elastic Net — formula

Elastic NetElastic NetL1 ও L2 penalty mix। Lasso-এর sparsity + Ridge-এর stability। Zou & Hastie ২০০৫। loss:

$$L = \frac{1}{N} \|\mathbf{y} - X\mathbf{w}\|^2 + \lambda \left[ \alpha \|\mathbf{w}\|_1 + \frac{1-\alpha}{2} \|\mathbf{w}\|_2^2 \right]$$

Two hyperparameters:

  • $\lambda$ — overall regularization strength।
  • $\alpha \in [0, 1]$ — L1 vs L2 mix।
    • $\alpha = 1$: pure Lasso।
    • $\alpha = 0$: pure Ridge।
    • $\alpha = 0.5$: balanced।
sklearn-এ l1_ratio = $\alpha$। alpha = $\lambda$। Naming convention সাবধান।

৩ · Geometric view

Constraint shape — diamond ও circle-এর mix। Hybrid shape: vertices আছে (sparsity) কিন্তু rounded edges (stability)। Optimal point — vertex-এ থাকতে পারে (some $w_j = 0$) বা edge-এ (continuous shrinkage)।

Regularization Spectrum α = L1/L2 mixing ratio Ridge (α=0) smooth shrink Elastic Net (α=0.5) rounded diamond sparse + stable Lasso (α=1) sharp corners exact zeros add L1 drop L2
Spectrum — pure Ridge থেকে Elastic Net থেকে pure Lasso। $\alpha$ slide। Practice-এ middle often best।

৪ · Group selection property

Elastic Net-এর key advantage — correlated features-এর "grouping effect"। যদি দু'টি feature highly correlated থাকে — Elastic Net both-কে similar weight দেয় (একসাথে keep বা drop)।

Ridge-এর contribution: similar features-এর coefficient similar রাখা।
Lasso-এর contribution: total sparsity preserve করা।
Together: group sparsity।

৫ · sklearn দিয়ে — Elastic Net

Python · scikit-learn
import numpy as np
from sklearn.linear_model import ElasticNet, Lasso, Ridge
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

# Synthetic data with correlated features
np.random.seed(0)
N, n = 100, 50
# Make some features correlated
base = np.random.randn(N, 10)
X = np.hstack([base, base + np.random.randn(N, 10) * 0.1,  # correlated copies
               np.random.randn(N, 30)])  # noise
true_w = np.zeros(50)
true_w[0:5] = [1.0, -1.0, 0.5, -0.5, 0.8]
y = X @ true_w + np.random.randn(N) * 0.5

X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3)
sc = StandardScaler()
X_tr = sc.fit_transform(X_tr)
X_te = sc.transform(X_te)

models = {
    "Lasso (α=1.0)": Lasso(alpha=0.05),
    "Ridge (α=0.0)": Ridge(alpha=1.0),
    "Elastic (l1=0.5)": ElasticNet(alpha=0.05, l1_ratio=0.5),
    "Elastic (l1=0.8)": ElasticNet(alpha=0.05, l1_ratio=0.8),
}

for name, m in models.items():
    m.fit(X_tr, y_tr)
    score = m.score(X_te, y_te)
    nz = np.sum(np.abs(m.coef_) > 1e-4)
    print(f"{name:18s}: R²={score:.4f}, non-zero={nz:2d}")

    
Lasso — sparse but unstable। Ridge — dense, stable। Elastic — sparse + correlated groups grouped। $\alpha$ ০.৫-০.৮ — typical sweet spot।

৬ · ElasticNetCV — auto-tuning

Python · scikit-learn
from sklearn.linear_model import ElasticNetCV
import numpy as np

np.random.seed(0)
X = np.random.randn(200, 30)
y = X[:, 0] + 2*X[:, 1] - X[:, 2] + np.random.randn(200) * 0.5

# Auto-search both alpha and l1_ratio
en_cv = ElasticNetCV(
    l1_ratio=[0.1, 0.3, 0.5, 0.7, 0.9, 0.95, 1.0],
    n_alphas=50,
    cv=5,
    random_state=0,
).fit(X, y)

print(f"Best alpha    = {en_cv.alpha_:.5f}")
print(f"Best l1_ratio = {en_cv.l1_ratio_:.2f}")
print(f"Score         = {en_cv.score(X, y):.4f}")
print(f"Non-zero      = {np.sum(np.abs(en_cv.coef_) > 1e-4)}/30")

    
ElasticNetCV — both hyperparameters together tune। Production-এ এটাই use। Time-consuming কিন্তু robust।

৭ · কোথায় Elastic Net rules

  • Genomics: ১০,০০০+ genes correlated — Elastic Net standard।
  • Finance: Macroeconomic features correlated — stable selection।
  • Marketing: Many channels overlap — group selection।
  • Sensor data: Adjacent sensors correlate।
  • Production credit scoring: Default choice।

৮ · Practical tips

  • Default starter: $\lambda = 0.1$, $\alpha = 0.5$।
  • Scale features: Standardize প্রথমে।
  • Cross-validate: Both hyperparameters।
  • Warm starts: Path solution available — fast।
  • Convergence: Coordinate descent — usually fast, sometimes need tolerance tuning।
Elastic Net — "magic" নয়। Pure Lasso vs Elastic Net — performance gap often small। Tuning overhead higher। Project priority assess করুন: simplicity vs marginal performance gain।

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

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

প্র ০১ "Grouping effect" mathematically prove করুন। Highly correlated features-এ Elastic Net কেন similar coefficients দেয়?

Grouping effect — Elastic Net-এর foundational result। Zou & Hastie ২০০৫ paper।

Theorem statement:

  • $\hat{w}_i, \hat{w}_j$ — Elastic Net coefficients।
  • Features $i, j$ correlation $\rho$।
  • If $\hat{w}_i \hat{w}_j > 0$ (same sign):
  • $|\hat{w}_i - \hat{w}_j| \leq \frac{\sqrt{2(1-\rho)}}{N \cdot \lambda_2} \|y\|$।

Interpretation:

  • $\rho \to 1$ (highly correlated): bound $\to 0$ — coefficients near-equal।
  • $\lambda_2 = \lambda(1-\alpha)$ — L2 strength।
  • $\lambda_2 \to 0$: bound $\to \infty$ — Lasso behavior।

Proof sketch:

  • KKT conditions for Elastic Net optimum।
  • Gradient zero at minimum।
  • L2 derivative — $2\lambda_2 w$।
  • Subtract conditions for $i$ and $j$।
  • Bound difference via correlation।

Why pure Lasso fails:

  • L1 — subgradient $\text{sign}(w)$।
  • Same sign — same subgradient।
  • Coefficient difference unconstrained — chooses arbitrarily।
  • Sample-to-sample instability।

L2-এর role:

  • Quadratic — penalizes coefficient differences।
  • Strong correlation + L2 → pulls together।
  • Smooth coefficient patterns।

Practical example:

  • Features: temperature_celsius, temperature_fahrenheit (perfectly correlated)।
  • True coefficient: 0.5 on either।
  • Lasso: 0.5 on one, 0 on other (random choice)।
  • Ridge: 0.25 on each।
  • Elastic Net: 0.25-0.4 on each।

Empirical demonstration:

  • Resample data, refit Lasso — coefficient pattern shifts wildly।
  • Resample, refit Elastic Net — pattern stable।
  • Standard error analysis confirms।

Group sparsity perspective:

  • Correlated features → "natural group"।
  • Elastic Net — implicit group selection।
  • Group Lasso — explicit (predefined groups)।

Implications for science:

  • Genetics — correlated SNPs in linkage disequilibrium।
  • Lasso would arbitrarily pick one।
  • Elastic Net — both selected, gene region identified।

Implications for engineering:

  • Sensor systems — redundancy desired।
  • Lasso eliminates redundancy।
  • Elastic Net preserves robustness।

Limitations:

  • "Within-group" sparsity not achieved।
  • Group Lasso superior for explicit groups।
  • Sparse Group Lasso — combines all।

মূল উপলব্ধি: Grouping effect — Elastic Net's defining theoretical advantage। Real-world correlated data-তে stability। Pure Lasso brittleness — production-এ deal-breaker often।

প্র ০২ হাইপারপ্যারামিটার tuning—$\lambda$ ও $\alpha$ একসাথে—curse of dimensionality? Bayesian optimization কীভাবে সাহায্য?

Multi-hyperparameter tuning — production ML-এর hidden complexity।

Grid search baseline:

  • $\lambda$: ৫০ values, $\alpha$: ৫ values = ২৫০ models।
  • ৫-fold CV — ১২৫০ training runs।
  • Linear models — fast। Computationally feasible।

Issues at scale:

  • NN-এ — hours per training run।
  • Discrete grid — between-grid optima miss।
  • Coarse-to-fine multi-stage usually।

Random search:

  • Bergstra & Bengio (2012)।
  • Random sample hyperparameter space।
  • Often beats grid search (high-D efficient)।
  • Reason: parameter importance varies।

Bayesian optimization:

(১) Surrogate model:

  • Gaussian Process — model objective function।
  • Predicts mean + uncertainty।
  • Fast to evaluate।

(২) Acquisition function:

  • Expected Improvement (EI), Upper Confidence Bound (UCB)।
  • Balance exploration vs exploitation।
  • Choose next point।

(৩) Iteration:

  • Evaluate at chosen point।
  • Update GP।
  • Repeat।

Advantages:

  • Sample-efficient — fewer evaluations।
  • Smart exploration।
  • Handles continuous hyperparameters natively।
  • Uncertainty quantification।

Tools:

  • Optuna — modern Python library।
  • Hyperopt — older standard।
  • Ray Tune — distributed।
  • Scikit-optimize — sklearn-compatible।

For Elastic Net specifically:

  • Fast training — Bayesian opt overkill।
  • Grid search competitive।
  • $\alpha$ — small discrete set okay।
  • $\lambda$ — log scale ৫০ values।

Multi-fidelity methods:

  • Hyperband: Successive halving।
  • BOHB: Bayesian + Hyperband।
  • Cheap evaluations early, expensive later।

Asymmetric search spaces:

  • $\alpha$ — bounded $[0, 1]$।
  • $\lambda$ — log-uniform $[10^{-4}, 10^2]$।
  • Different priors apply।

Curse of dimensionality:

  • ২ hyperparameters — manageable।
  • ৫-১০ — Bayesian opt advantageous।
  • ২০+ — hierarchical methods।

Best practices:

  • Coarse search first — broad strokes।
  • Fine search — narrow optimal region।
  • Stratified CV — class imbalance।
  • Multiple seeds — variance estimate।

Production deployment:

  • Tune once, deploy fixed hyperparameters।
  • Periodic retuning — drift।
  • Hyperparameter dashboard।
  • Reproducibility — random seed।

Auto-ML perspective:

  • Automated hyperparameter selection।
  • Auto-sklearn, H2O AutoML।
  • Black-box for users।
  • Trade-off: control vs convenience।

মূল উপলব্ধি: Linear models-এ tuning manageable। Bayesian opt — bigger fish। Tools mature, accessible। Hyperparameter tuning — neither magic nor brute force। Smart strategies = sample-efficient।

প্র ০৩ Bangladesh-এ একটি customer churn predictor — ৩০০ features, অনেক correlated। কোন regularizer? কেন?

Real production scenario — features explosion typical।

Feature breakdown:

  • Demographic: ২০ (age, gender, location, education)।
  • Behavioral: ১০০ (usage patterns)।
  • Transaction: ৮০ (amount, frequency, channel)।
  • Engagement: ৫০ (app open, support contact)।
  • Derived: ৫০ (ratios, aggregations)।

Correlation patterns:

  • "Daily app opens" ↔ "weekly app opens" highly correlated।
  • "Average transaction amount" ↔ "median amount" similar।
  • Geographic — district + nearby districts।
  • Time windows — overlapping aggregations।

Why pure Lasso problematic:

  • Random choice between correlated features।
  • Different runs — different features selected।
  • Business team confused — "last week feature X important, this week Y?"
  • Production deployment unstable।

Why pure Ridge suboptimal:

  • ৩০০ small coefficients।
  • Interpretation noisy।
  • Operational complexity (compute সব features প্রতি prediction)।
  • No clear "important" features list।

Elastic Net advantages:

  • ~৫০-১০০ features non-zero (manageable)।
  • Correlated features grouped (interpretable)।
  • Stable across retrainings।
  • "Group of channels" insights possible।

Implementation strategy:

(১) Data preparation:

  • Standardize all numerical features।
  • One-hot categorical (manageable cardinality)।
  • Target encoding for high-cardinality।
  • Handle missing — indicator + impute।

(২) Initial baseline:

  • Logistic regression (no regularization)।
  • Identify features with non-finite weights।
  • Quick correlation check।

(৩) Elastic Net tuning:

  • $\alpha$ ∈ {0.1, 0.3, 0.5, 0.7, 0.9}।
  • $\lambda$ — log scale 50 values।
  • 5-fold stratified CV।
  • Optimize: AUC (churn — imbalanced)।

(৪) Validation:

  • Out-of-time test set।
  • Calibration check।
  • Per-segment performance।
  • Business KPI translation।

Benchmarking:

  • Baseline: Logistic regression (no reg)।
  • Elastic Net (tuned)।
  • XGBoost (compare benchmark)।
  • Choose based on AUC + interpretability।

Bangladesh context features:

  • bKash transaction patterns।
  • Eid/Pohela Boishakh seasonality।
  • Mobile network operator (Grameenphone, Robi)।
  • Region (Dhaka, Chittagong, Sylhet)।
  • Language preference (Bangla/English)।

Production considerations:

  • Real-time inference (~১০ms target)।
  • Daily batch retraining।
  • Feature store maintained।
  • Monitoring dashboards।
  • A/B test new feature additions।

Stakeholder communication:

  • "৫০ features driving ৭৫% predictions"।
  • Feature importance plot।
  • SHAP values for individual cases।
  • Adverse action notices automated।

Iteration roadmap:

  • Quarter 1: Elastic Net production।
  • Quarter 2: Feature engineering iteration।
  • Quarter 3: XGBoost + SHAP।
  • Quarter 4: Deep learning experiment।

মূল উপলব্ধি: ৩০০ features, correlated — Elastic Net textbook fit। Stability + interpretability + performance triangle। Linear model baseline production-grade often সর্বদা।

প্র ০৪ Genetics-এ Elastic Net dominant — কেন? GWAS-এ thousands of SNPs ও correlated genes — Elastic Net কী contribution করে?

Genomics — Elastic Net-এর showcase domain।

Genetics setting:

  • SNPs — Single Nucleotide Polymorphisms — ১M+।
  • Patients — হাজার থেকে লক্ষ।
  • $N \ll p$ extreme regime।
  • Phenotype prediction (disease risk)।

Linkage disequilibrium (LD):

  • Adjacent SNPs inherited together।
  • High correlation in genome regions।
  • "LD blocks" — natural feature groups।
  • Distance-decaying correlation।

Pure Lasso problems:

  • Each LD block — random SNP selected।
  • Different studies — different SNPs reported।
  • Replication failures — major concern।
  • Biological interpretation — confused।

Elastic Net solutions:

  • LD blocks selected together।
  • Stable SNP sets across studies।
  • Gene regions identified (not single SNP)।
  • Replication improved।

GWAS workflow:

(১) Pre-processing:

  • Quality control SNPs।
  • Population structure correction।
  • Principal components for ancestry।

(২) Univariate screen:

  • Each SNP — separate test।
  • Bonferroni correction।
  • Lead SNPs identified।

(৩) Polygenic risk score:

  • Many SNPs combined।
  • Elastic Net often used।
  • Risk prediction model।

Specific applications:

(১) Disease prediction:

  • Diabetes risk — multifactorial।
  • Cardiovascular — many small effects।
  • Cancer susceptibility।

(২) Drug response:

  • Pharmacogenomics।
  • Treatment effectiveness prediction।
  • Personalized medicine।

(৩) Quantitative traits:

  • Height, BMI, blood pressure।
  • Polygenic — many small effects।

Software ecosystem:

  • PLINK + custom Elastic Net।
  • BLUP (Best Linear Unbiased Predictor) — related Bayesian।
  • LDpred — LD-aware methods।
  • Pruning + thresholding alternatives।

Why Elastic Net wins here:

  • $N \ll p$: Lasso limit হিট।
  • Correlation: LD requires grouping।
  • Sparsity: Most SNPs irrelevant।
  • Stability: Replication scientific gold standard।
  • Interpretation: Gene regions, not single SNPs।

Limitations:

  • Linear — gene-gene interactions miss।
  • Non-linear methods — random forest, neural networks।
  • Heritability not fully captured।
  • "Missing heritability" research direction।

Bayesian alternatives:

  • BayesR — mixture priors।
  • LDpred — LD-aware prior।
  • SBayesR — summary statistics-based।
  • Theoretical foundations sometimes preferred।

Cross-population transferability:

  • Most GWAS — European populations।
  • Polygenic scores — limited transfer।
  • South Asian, African — under-studied।
  • Active research area।

Bangladesh genomics future:

  • Bangladesh Genome Project — initial steps।
  • South Asian genetic patterns unique।
  • Local disease (thalassemia, diabetes patterns)।
  • ML opportunity unexplored।

Computational considerations:

  • 1M SNPs × 100k patients — memory challenge।
  • Sparse storage critical।
  • GPU acceleration emerging।
  • Distributed computing standard।

মূল উপলব্ধি: Genomics — Elastic Net's flagship application। Real-world correlation structure + sparsity assumption + stability requirement = match made in heaven। Modern ML alternatives exist but don't quite match interpretability।

অনুশীলন

  1. হিসাব করুন: $\alpha = 0.5$, $\lambda = 0.1$ — Elastic Net penalty কী form-এ?

    $0.1 \cdot [0.5 \|\mathbf{w}\|_1 + 0.25 \|\mathbf{w}\|_2^2] = 0.05 \|\mathbf{w}\|_1 + 0.025 \|\mathbf{w}\|_2^2$।

    L1 ও L2 দু'টোই present, L1 এক্ষেত্রে dominant। $\alpha$ বদলে weight shift।

  2. sklearn: $\alpha$ = ০, ০.৫, ১ — তিন setting compare করুন। কোনটি কত feature select?
    from sklearn.linear_model import ElasticNet
    import numpy as np
    np.random.seed(0)
    X = np.random.randn(100, 50)
    y = X[:, :5] @ np.array([1, -1, 0.5, -0.5, 1]) + np.random.randn(100)*0.3
    
    for r in [0.0, 0.5, 1.0]:
        m = ElasticNet(alpha=0.05, l1_ratio=r).fit(X, y)
        print(f"l1_ratio={r}, non-zero={np.sum(np.abs(m.coef_) > 1e-4)}")

    r=0 (Ridge) → ৫০ non-zero। r=1 (Lasso) → ~৫-১০। r=0.5 (mix) → middle।

  3. চিন্তা: Bangladesh agricultural data — soil nutrients, weather, geography। ৮০ features। কোন regularizer? কেন?

    Soil nutrients correlated, weather features overlap (temp/humidity)। Elastic Net — group selection optimal। Pure Lasso — random nutrient pick problematic। Ridge — সব small coefficient — interpretation আমলাতান্ত্রিক।

আরও পড়ুন

কোড রানার কাজ না করলে? Google Colab ব্যবহার করুন।
পূর্ববর্তী পাঠ
পাঠ ১৫ · Ridge ও Lasso