2.1.15 · AI-ML › Data Preprocessing & Feature Engineering
Correlation analysis statistical relationship examine karta hai features ke beech, jabki multicollinearity tab hoti hai jab predictor variables aapas mein highly correlated hote hain. Dono hi feature selection aur model interpretability ke liye critical hain.
Intuition Correlation Kyun Matters Karta Hai
Socho tum house prices predict kar rahe ho "square footage" aur "number of rooms" dono use karke. Ye features naturally correlated hain—bade houses mein zyada rooms hote hain. Agar tum dono include karo, to tumhara model:
Is size aspect ko Overweight kar sakta hai (double-counting)
Unstable ho sakta hai : chhote data changes se coefficient wild swings karte hain
Interpretability kho sakta hai : "Kya price area se badhti hai ya rooms se?" unclear ho jaata hai
Correlation analysis tumhe model todne se pehle redundancy spot karne mein help karta hai.
Definition Correlation Coefficient
Features X aur Y ke beech Pearson correlation coefficient r linear relationship measure karta hai:
r X Y = σ X σ Y Cov ( X , Y ) = ∑ i = 1 n ( x i − x ˉ ) 2 ∑ i = 1 n ( y i − y ˉ ) 2 ∑ i = 1 n ( x i − x ˉ ) ( y i − y ˉ )
Range : − 1 ≤ r ≤ 1
r = 1 : perfect positive linear relationship
r = 0 : koi linear relationship nahi
r = − 1 : perfect negative linear relationship
Ye formula kyun? Ye covariance hai (kitna X aur Y saath vary karte hain) jo unke individual standard deviations se normalize kiya gaya hai (unka "vary karne ka room"). Isse r unit-free aur feature pairs ke beech comparable ban jaata hai.
Definition Multicollinearity
Multicollinearity tab exist karta hai jab predictor variables highly linearly dependent hote hain. Formally, feature X j mein multicollinearity hai agar:
X j ≈ β 0 + β 1 X 1 + β 2 X 2 + … + β j − 1 X j − 1 + β j + 1 X j + 1 + …
Linear regression par impact : Agar X T X (design matrix product) nearly singular hai, to ( X T X ) − 1 unstable ho jaata hai, jisse:
Coefficient standard errors bahut bade ho jaate hain
Chhote data changes se coefficients ke signs flip ho jaate hain
High R 2 lekin individual coefficients insignificant hote hain
Pehle principles se shuru karo : Hum ek aisa measure chahte hain jo:
Symmetric ho: measure ( X , Y ) = measure ( Y , X )
Scale-invariant ho: X ki units double karne se relationship strength nahi badlni chahiye
"Co-movement" capture kare: jab X apne mean se upar ho, to Y apne mean se upar/neeche hone ki tendency rakhta ho
Step 1 : Covariance se co-movement measure karo:
Cov ( X , Y ) = E [( X − μ X ) ( Y − μ Y )] = E [ X Y ] − E [ X ] E [ Y ]
Ye step kyun? ( X − μ X ) positive hota hai jab X average se upar ho, negative jab neeche ho. Unka product ( X − μ X ) ( Y − μ Y ) :
Positive hota hai jab dono average se upar/neeche hों (same direction)
Negative hota hai jab ek upar ho, ek neeche (opposite direction)
Is product ka average "typical co-movement" deta hai.
Step 2 : Normalize karke scale-invariant banao:
r = σ X σ Y Cov ( X , Y )
Ye step kyun? Covariance ki units hoti hain ( X -units ) × ( Y -units ) . Dono standard deviations se divide karne par units hat jaati hain aur r ko [ − 1 , 1 ] mein bound kar deta hai (Cauchy-Schwarz inequality se).
Bounds ka proof : Cauchy-Schwarz se, ∣ Cov ( X , Y ) ∣ ≤ σ X σ Y , isliye ∣ r ∣ ≤ 1 . Equality tab hoti hai jab Y = a X + b (perfect linear relationship).
Worked example Correlation Matrix Compute Karna
Dataset : 4 features X 1 , X 2 , X 3 , X 4 with n = 5 samples
X1 = [ 1 , 2 , 3 , 4 , 5 ]
X2 = [ 2 , 4 , 6 , 8 , 10 ] # Perfect correlation with X1
X3 = [ 5 , 4 , 3 , 2 , 1 ] # Perfect negative with X1
X4 = [ 3 , 1 , 4 , 1 , 5 ] # Random
Step 1 : Calculate karo x ˉ 1 = 3 , aur ∑ i ( x 1 , i − x ˉ 1 ) 2 = 4 + 1 + 0 + 1 + 4 = 10 . Similarly x ˉ 2 = 6 aur ∑ i ( x 2 , i − x ˉ 2 ) 2 = 16 + 4 + 0 + 4 + 16 = 40 .
Step 2 : r 12 compute karo:
r 12 = 10 ⋅ 40 ( 1 − 3 ) ( 2 − 6 ) + ( 2 − 3 ) ( 4 − 6 ) + ( 3 − 3 ) ( 6 − 6 ) + ( 4 − 3 ) ( 8 − 6 ) + ( 5 − 3 ) ( 10 − 6 )
= 400 ( − 2 ) ( − 4 ) + ( − 1 ) ( − 2 ) + 0 + ( 1 ) ( 2 ) + ( 2 ) ( 4 ) = 20 8 + 2 + 0 + 2 + 8 = 20 20 = 1.0
Ye step kyun? Numerator cross-deviation products ka raw sum hai ∑ i ( x 1 , i − x ˉ 1 ) ( x 2 , i − x ˉ 2 ) = 20 . Denominator hai ∑ ( x 1 − x ˉ 1 ) 2 ⋅ ∑ ( x 2 − x ˉ 2 ) 2 = 10 ⋅ 40 = 400 = 20 . Kyunki X 2 = 2 X 1 exactly hai, correlation exactly 1.0 hai. (Note: sample covariance hogi 20/ ( n − 1 ) = 20/4 = 5 , aur σ 1 σ 2 = 2.5 ⋅ 10 = 5 , same r = 5/5 = 1 milta hai. Normalization cancel ho jaata hai chahe population ya sample scaling use karo.)
Step 3 : r 14 compute karo (with X 4 = [ 3 , 1 , 4 , 1 , 5 ] , x ˉ 4 = 2.8 ):
∑ i ( x 1 , i − x ˉ 1 ) ( x 4 , i − x ˉ 4 ) = ( − 2 ) ( 0.2 ) + ( − 1 ) ( − 1.8 ) + ( 0 ) ( 1.2 ) + ( 1 ) ( − 1.8 ) + ( 2 ) ( 2.2 )
= − 0.4 + 1.8 + 0 − 1.8 + 4.4 = 4.0
∑ i ( x 4 , i − x ˉ 4 ) 2 = 0.04 + 3.24 + 1.44 + 3.24 + 4.84 = 12.8
r 14 = 10 ⋅ 12.8 4.0 = 128 4.0 = 11.31 4.0 ≈ 0.354
Ye step kyun? X 4 ka X 1 ke saath mild positive association hai, jisse r 14 ≈ 0.35 milta hai—zero nahi, lekin perfect X 1 –X 2 relationship se bahut weaker. Kyunki X 2 = 2 X 1 hai, hamare paas r 24 = r 14 ≈ 0.35 hai, aur kyunki X 3 = 6 − X 1 hai, hamare paas r 34 = − r 14 ≈ − 0.35 hai.
Full symmetric matrix (diagonal = 1, symmetric off-diagonals):
R = 1.00 1.00 − 1.00 0.35 1.00 1.00 − 1.00 0.35 − 1.00 − 1.00 1.00 − 0.35 0.35 0.35 − 0.35 1.00
Insight : X 1 , X 2 , X 3 perfectly colinear hain (har ek doosron ka linear function hai)—sirf ek rakho! X 4 genuinely independent information carry karta hai.
Worked example VIF Calculation
Setup : House price predict karna in features ke saath:
X 1 : square footage (1000-3000 sqft)
X 2 : number of bedrooms (2-5)
X 3 : number of bathrooms (1-3)
X 4 : age of house (0-50 years)
Step 1 : X 2 ko X 1 , X 3 , X 4 par regress karo:
X 2 = 0.5 + 0.001 ⋅ X 1 + 0.8 ⋅ X 3 − 0.01 ⋅ X 4
R 2 2 = 0.85 milta hai (bedrooms size aur bathrooms se highly predictable hain).
Step 2 : VIF calculate karo:
VIF 2 = 1 − 0.85 1 = 0.15 1 = 6.67
Ye kyun matters karta hai : "bedrooms" coefficient ka standard error 6.67 ≈ 2.58 times inflate ho gaya hai. Hum bedrooms ke true effect ke baare mein 2.6× kam certain hain.
Saare features ke liye VIF :
VIF 1 = 4.2 (sqft correlated hai bedrooms/bathrooms se)
VIF 2 = 6.7 (bedrooms highly correlated hain)
VIF 3 = 5.1 (bathrooms correlated hain)
VIF 4 = 1.3 (age independent hai)
Action : X 2 (bedrooms) drop karo kyunki ye X 1 aur X 3 ke saath redundant hai.
Worked example Nonlinear Relationships Detect Karna
Pearson correlation ki limitation : Sirf linear relationships detect karta hai.
Data : X = [ − 2 , − 1 , 0 , 1 , 2 ] , Y = [ 4 , 1 , 0 , 1 , 4 ] (perfect quadratic Y = X 2 )
Pearson correlation (x ˉ = 0 , y ˉ = 2 ):
numerator = ∑ i x i ( y i − 2 ) = ( − 2 ) ( 2 ) + ( − 1 ) ( − 1 ) + ( 0 ) ( − 2 ) + ( 1 ) ( − 1 ) + ( 2 ) ( 2 ) = − 4 + 1 + 0 − 1 + 4 = 0
⇒ r = 0
Zero kyun? Kyunki x ˉ = 0 hai, ( x i − x ˉ ) = x i . Product x i ( y i − y ˉ ) x mein antisymmetric hai (kyunki Y = X 2 symmetric hai), isliye positive aur negative terms exactly cancel ho jaate hain.
Solution : Spearman rank correlation use karo (values ke bajaye ranks par based) ya scatter plot karo U-shape dekhne ke liye. Feature engineering ke liye, X 2 ko nayi feature ke roop mein create karo.
Common mistake Correlation Implies Causation
Galat soch : "Ice cream sales aur drowning deaths correlated hain (r = 0.9 ), isliye ice cream drowning cause karta hai!"
Ye sahi kyun lagta hai : High correlation matlab dono saath move karte hain. Hamare dimaag ko simple cause-effect stories pasand hain.
Fix : Dono ek confounding variable (summer temperature) ki wajah se hote hain. Correlation association measure karta hai, causation nahi. Causation claim karne ke liye tumhe chahiye:
Temporal precedence (cause pehle, effect baad mein)
Controlled experiments ya causal inference methods
Koi plausible confounders nahi
ML mein : Correlated features prediction mein help karte hain (ice cream sales summer mein drowning risk predict karta hai), lekin mechanisms explain nahi karte.
Common mistake Nonlinear Relationships ko Ignore Karna
Galat soch : "r = 0.02 , isliye features unrelated hain. Main ek drop kar dunga."
Example : X = temperature , Y = energy usage . Linear correlation weak hai, lekin Y ka U-shape hai (extreme cold/heat mein high usage).
Ye fail kyun hota hai : Pearson r sirf straight-line fit measure karta hai. Relationship strong hai lekin curved hai.
Fix :
r trust karne se pehle scatter plots se visualize karo
Monotonic nonlinearity ke liye rank correlation (Spearman/Kendall) use karo
Agar justified ho to polynomial features create karo
Common mistake Feature Selection ke liye Raw Correlation Use Karna
Galat soch : "Feature X j ka target Y ke saath r = 0.1 hai, isliye ye useless hai."
Ye galat kyun hai : X j ka marginal correlation zero ho sakta hai lekin conditional importance high ho sakti hai. Example:
XOR problem: Y = X 1 ⊕ X 2 (exclusive OR)
r ( X 1 , Y ) = 0 aur r ( X 2 , Y ) = 0
Lekin Y puri tarah X 1 , X 2 se saath mein determine hota hai!
Fix : Akele correlation ki jagah trained models se feature importance use karo (tree-based methods, permutation importance).
Common mistake VIF Thresholds Absolute Hain
Galat soch : "X 3 ke liye VIF = 12 hai. Mujhe ise ZAROOR drop karna hai."
Context matters karta hai :
Prediction-only : High multicollinearity predictions ko bias nahi karta, sirf variance inflate karta hai. Agar tumhare paas kaafi data hai, to theek hai.
Inference : Agar tumhe coefficients interpret karne hain (jaise, "ek extra bedroom price mein $10K add karta hai"), to multicollinearity fix karna critical hai.
Regularization : Ridge/Lasso regression coefficients shrink karke multicollinearity handle karta hai.
Fix : Apne goal (prediction vs. interpretation) aur available techniques ke basis par decide karo.
Compute Correlation Matrix
High pairwise abs r over 0.8?
Calculate VIF for each feature
Multicollinearity detected
Remediation: Drop/Combine/PCA/Regularize
No severe multicollinearity
Step-by-step :
Pairwise check : Saare feature pairs ke liye ∣ r ij ∣ compute karo. Agar > 0.8 ho to flag karo.
VIF check : Har feature ke liye, doosron par regress karo, VIF compute karo. Agar > 10 ho to flag karo.
Global check : κ ( X ) (design matrix ka condition number) compute karo. Agar > 30 hai, to matrix ill-conditioned hai.
Action : Har correlated pair se ek feature drop karo, ya PCA use karo, ya L2 regularization apply karo.
Har highly correlated pair se ek feature drop karo (agar r 12 = 0.95 hai to X 1 rakho, X 2 drop karo)
Features combine karo : X new = X 1 + X 2 create karo (jaise, "total_rooms = bedrooms + bathrooms")
Principal Component Analysis (PCA) : Uncorrelated components mein transform karo
Regularization : Ridge (L 2 penalty) ya Lasso (L 1 , selection bhi karta hai) use karo
Domain knowledge : Jo feature clearer causal interpretation rakhta ho use rakho
Covariance Matrix : Correlation normalized covariance hai
Feature Selection : Features drop karne ke liye Correlation ek criterion hai
Principal Component Analysis (PCA) : Correlated features ko orthogonal components mein transform karta hai
Ridge Regression : L 2 penalty ke zariye multicollinearity handle karta hai
Lasso Regression : Multicollinearity ke under feature selection karta hai
Linear Regression Assumptions : Multicollinearity "no perfect colinearity" assumption violate karta hai
Variance-Bias Tradeoff : High VIF coefficient estimates ke variance ko inflate karta hai
Condition Number : Multicollinearity ka matrix theory perspective
Recall Ek 12-Saal ke Bacche ko Explain Karo
Socho tum figure out karne ki koshish kar rahe ho ki koi video game fun kyun hoti hai, alag-alag features dekh kar: graphics quality, sound quality, story depth, aur number of explosions.
Correlation aise puuchna hai: "Kya better graphics wale games mein better sound bhi hota hai?" Agar haan, to ye correlated hain. Tum ise -1 se 1 tak ke number se measure karte ho:
1 ka matlab hai : Better graphics ka HAMESHA better sound matlab hai (perfect match)
0 ka matlab hai : Graphics aur sound ka ek doosre se koi lena dena nahi
-1 ka matlab hai : Better graphics ka HAMESHA worse sound matlab hai (opposite)
Multicollinearity tab hoti hai jab do features itni similar hoti hain ki dono use karna ek hi cheez do baar count karne jaisa hai. Example: "graphics quality" aur "texture resolution" almost same cheez hain! Agar tum dono include karo, to tumhara analysis confuse ho jaata hai—ye nahi bata sakta ki kaunsa actually matter karta hai kyunki dono hamesha saath chalte hain.
Ye bura kyun hai? Socho tum figure out karne ki koshish kar rahe ho ki graphics ya story games ko fun banati hai. Agar tumhare data mein graphics aur story hamesha saath improve hote hain, to tum unke effects alag nahi kar sakte. Ye aise hai jaise figure out karna ki cake mein flour ya sugar tasty banata hai jab tum dono hamesha saath daalte ho—tum bata nahi sakte!
Fix : Similar features mein se ek drop karo (sirf graphics rakho, texture resolution drop karo), ya unhe ek "visual quality" score mein combine karo.
V ariance I nflation F actor = V ery I ll-conditioned F eatures
Socho: "Jab VIF HIGH hota hai, feature doosron mein HIDE ho raha hota hai (correlated)."
Correlation scale :
0.0-0.3 : N egligible (socho N one)
0.3-0.7 : M oderate (socho M aybe related)
0.7-1.0 : S trong (socho S uper related)
Features X aur Y ke beech -0.85 ka correlation coefficient kya indicate karta hai? Strong negative linear relationship: jaise X badhta hai, Y proportionally decrease hoti hai. Features highly corre