Simple linear regression model
2.2.1· AI-ML › Linear & Logistic Regression
Overview
Simple linear regression ek single independent variable aur dependent variable ke beech ke relationship ko ek straight line se model karta hai. Yeh predictive modeling ki neenv hai aur sabhi supervised learning ko samajhne ka darwaza hai.

[!intuition] Core Idea
Socho tum ghar ke daam predict kar rahe ho. Tumne notice kiya: bade ghar zyada mehange hote hain. Simple linear regression tumhare (size, price) data points ke through ek "best-fit" straight line kheenchti hai. Jab yeh line mil jaaye, tum nayi prices line se padh ke predict karte ho.
Straight line kyun? Kyunki hum ek proportional relationship model kar rahe hain: har extra square foot price mein lagbhag utni hi rakam badhata hai. Duniya hamesha linear nahi hoti, lekin linear models:
- Interpret karne mein aasaan hote hain (slope = "price per sq ft")
- Compute karne mein fast hote hain
- Bahut se real phenomena ke liye surprizingly effective hote hain
[!definition] Mathematical Model
Simple linear regression model yeh hai:
Jahan:
- dependent variable (target, jo hum predict karte hain)
- = independent variable (feature, predictor)
- = intercept ( ki value jab )
- = slope ( mein ek unit change par mein change)
- = error term (random noise, jo line chhodd deti hai use capture karta hai)
Key assumptions:
- Linearity: Saccha relationship roughly linear hai
- Independence: Observations ek doosre ko affect nahi karte
- Homoscedasticity: Saare ke liye error variance constant hai
- Normality: Errors , follow karte hain
[!formula] Best-Fit Line Derive Karna (Ordinary Least Squares)
Goal: dhundho jo prediction errors minimize karein.
Step 1: Loss Function Define Karo
training points ke liye, hamara prediction hai .
Point ke liye residual (error) hai:
Hum chhote errors chahte hain, isliye sum of squared residuals (RSS) minimize karo:
Errors ko square kyun karein?
- Squaring se saare errors positive ho jaate hain ( error utna hi bura hai jitna )
- Squaring se bade errors ko zyada penalty milti hai (outliers hurt karte hain)
- Squaring se math differentiable ho jaata hai (hum calculus use kar sakte hain)
Step 2: Partial Derivatives Lo
Minimize karne ke liye, aur set karo.
ke saath respect karke derivative:
Chain rule use karke:
se divide karo:
Expand karo:
Yeh step kyun? Saare residuals sum karke zero set karne ka matlab hai ki line data ke "center" se guzarti hai.
ke liye solve karo:
jahan aur means hain.
ke saath respect karke derivative:
substitute karo:
Yeh step kyun? Hum data ko uske mean ke around center kar rahe hain, jo covariance aur variance tak simplify ho jaata hai.
Note: , aur
ke liye solve karo:
Statistics ke terms mein:
Final OLS formulas:
[!example] Worked Example 1: House Prices
Data: 5 ghar size (100s of sq ft) aur price (1000s of $) ke saath
| (size) | (price) |
|---|---|
| 10 | 250 |
| 15 | 350 |
| 20 | 450 |
| 25 | 550 |
| 30 | 650 |
Step 1: Means calculate karo
Step 2: Slope calculate karo
| 10 | 250 | -10 | -200 | 2000 | 100 |
| 15 | 350 | -5 | -100 | 500 | 25 |
| 20 | 450 | 0 | 0 | 0 | |
| 25 | 550 | 5 | 100 | 500 | 25 |
| 30 | 650 | 10 | 2000 | 100 | |
| Sum | 5000 | 250 |
Yeh value kyun? Har 100 sq ft increase price ko $20k badhata hai. Intuitive sense banata hai!
Step 3: Intercept calculate karo
Final model:
Interpretation: 0 sq ft ke ghar ka (jo exist nahi karta!) cost 20k badhata hai.
Prediction: 200 sq ft ke ghar ke liye ():
[!example] Worked Example 2: Study Hours vs Exam Score
Data: 4 students
| Study Hours () | Score () |
|---|---|
| 2 | 50 |
| 4 | 65 |
| 6 | 80 |
| 8 | 95 |
Step 1: ,
Step 2:
Step 3:
Model:
Matlab: Har extra study hour 7.5 points badhata hai. 0 hours ke saath, base score 35 hai (purani knowledge?).
[!formula] Model Evaluation Metrics
1. R-squared (Coefficient of Determination)
jahan TSS (Total Sum of Squares) = jo mein total variance measure karta hai.
Matlab ki derivation:
- RSS = unexplained variance (jo model chhodd gaya)
- TSS = data mein total variance
- = unexplained fraction
- = explained fraction (0 aur 1 ke beech)
Yeh metric kyun? ka matlab hai " mein 85% variance se explain hoti hai". Zyada better hota hai.
2. Root Mean Squared Error (RMSE)
Yeh metric kyun? RMSE ke same units mein hota hai. Agar price dollars mein predict kar rahe ho, toh RMSE bhi dollars mein hoga. "Average par, predictions se off hoti hain."
[!mistake] Common Pitfalls
Mistake 1: Data Range se Bahar Extrapolation
Galat soch: "Mera model 1000-3000 sq ft ke ghar ke liye kaam karta hai, toh main 10,000 sq ft ke mansion ke liye bhi predict karunga."
Kyun sahi lagta hai: Formula kisi bhi ke liye kaam karta hai!
Fix: Linearity tumhari data range ke bahar nahi chalti. Bahut bade ghar ki alag price dynamics hoti hai (luxury market). Sirf observed range ke andar interpolate karo.
Mistake 2: Correlation ≠ Causation
Galat soch: "Ice cream sales aur drowning deaths correlated hain aur linear model mein fit hote hain, isliye ice cream drowning ka cause hai!"
Kyun sahi lagta hai: Math valid hai; regression line exist karti hai.
Fix: Regression association dhundta hai, causation nahi. Dono ek confounding variable (garmi) ki wajah se hote hain. Causation claim karne ke liye controlled experiments ya causal inference methods chahiye.
Mistake 3: Outliers ko Ignore Karna
Galat soch: "Ek data point bahut alag hai, lekin main ise waise hi rakhunga."
Kyun sahi lagta hai: Zyada data better hota hai, na?
Fix: Outliers line ko distort karte hain (squared errors unka influence amplify karte hain). Outliers investigate karo: data entry errors hain? Ya legitimate extreme cases hain? Robust regression methods consider karo ya justification ke baad remove karo.
Mistake 4: Check Kiye Bina Linearity Assume Karna
Galat soch: "Maine ek line fit ki, toh relationship linear hai."
Kyun sahi lagta hai: Chahe kuch bhi ho line milti hi hai; OLS hamesha return karta hai.
Fix: Residuals vs fitted values plot karo. Random scatter = achha. Patterns (curve, funnel shape) = violated assumptions. Data transform karo (log, sqrt) ya polynomial/nonlinear regression use karo.
[!recall]- 12 Saal Ki Umar Mein Samjhao
Socho tum apne doston ki height guess karne ki koshish kar rahe ho unka shoe size dekhkar.
Tumne notice kiya: bade shoes wale log usually lambe hote hain. Toh tum graph paper par ek straight line kheenchte ho jahan tumne sabka (shoe size, height) plot kiya hai.
Ab jab size 7 shoes wala naya bachha aata hai, tum apni line par 7 dhundh ke unki predicted height padh lete ho!
Simple linear regression bas tumhare dots ke through BEST line dhundhna hai. "Best" ka matlab hai wo line jo saare dots ke sabse kareeb ho (average par sabse chhoti mistakes).
Slope tumhe batata hai "har shoe size par kitne inches zyada lambe", aur intercept wahan hai jahan line height axis ko cross karti hai (halanki size 0 shoes wala baby actually utna lamba nahi hoga—line sirf ek model hai!).
[!mnemonic] SLOPE
Sum of (x - mean)(y - mean)
Less
Over
Product of (x - mean)²
Equals beta₁
Intercept: "y-bar minus beta times x-bar" (sounds like "why bother? Beat times X!")
Connections
- 2.1-Introduction-to-Regression: Broad context
- 2.2.02-Multiple-Linear-Regression: Multiple features tak extension
- 2.3-Gradient-Descent-for-Linear-Regression: Iterative optimization approach
- 2.5-Assumptions-and-Diagnostics: Model validity check karna
- 3.1-Logistic-Regression-Model: Classification tak extension
- 4.2-Overfitting-vs-Underfitting: Linear models mein bias-variance
- 1.3-Loss-Functions: RSS ek specific loss function hai
#flashcards/ai-ml
Simple linear regression kya model karta hai? :: Ek method jo single independent variable x aur dependent variable y ke beech ke relationship ko straight line equation y = β₀ + β₁x + ε se model karta hai
y = β₀ + β₁x + ε mein parameters kya hain?
Ordinary Least Squares (OLS) kya minimize karta hai?
β₁ ke liye OLS formula first principles se derive karo :: ∂RSS/∂β₁ = 0 lo, β₀ = ȳ - β₁x̄ substitute karo, solve karo to β₁ = Σ(xᵢ-x̄)(yᵢ-ȳ) / Σ(xᵢ-x̄)² = Cov(x,y)/Var(x) milta hai