Worked examples — Linear regression — normal equation, gradient descent derivation
5.6.1 · D3· Coding › Machine Learning (Aerospace Applications) › Linear regression — normal equation, gradient descent deriva
Yeh deep dive parent topic ka hands-on companion hai. Wahan humne ek line fit karne ke do tarike derive kiye the. Yahan hum un machines ko har tarah ke input par run karte hain jo tum meet kar sakte ho — clean data, perfect fits, broken (singular) data, badly scaled data, real-world word problems, aur ek exam twist.
Yeh page self-contained hai: jo bhi hum use karte hain, woh pehle yahan define kiya gaya hai.
Recall Woh do formulas jinhe hum baar baar use karenge
Closed form (ek shot mein): . Iterative: . Yahan learning rate hai (step size). isliye aata hai kyunki average cost ke gradient mein wahi hota hai — hum us averaged slope ke against step karte hain.
Scenario matrix
Ek line-fitting problem tumhare saamne limited tarah ke flavours mein aa sakti hai. Yeh poora menu hai. Har flavour ko neeche kam se kam ek fully worked example milta hai.
| Cell | Case class | Kya special hai | Example |
|---|---|---|---|
| A | Positive slope, imperfect fit | ordinary case, errors cancel nahi karte | Ex 1 |
| B | Perfectly collinear points | error exactly zero hai; fit sabse guzarti hai | Ex 2 |
| C | Negative slope | ka sign negative hai — sign slips pakadta hai | Ex 3 |
| D | Zero slope (flat) | best line horizontal hai, | Ex 4 |
| E | Singular / degenerate | inverse exist nahi karta (sab equal hain) | Ex 5 |
| F | Gradient descent, step-by-step | iterative path, learning rate behaviour | Ex 6 |
| G | Learning rate too large (divergence) | ka limiting behaviour | Ex 7 |
| H | Badly scaled features | kyun Feature Scaling matter karta hai | Ex 8 |
| I | Real-world word problem (aerospace) | units, interpretation | Ex 9 |
| J | Exam twist: proof/geometry | residual ki orthogonality | Ex 10 |
Example 1 — Cell A: positive slope, imperfect fit
Forecast: points left-to-right rise karte hain, toh guess karo aur ek chota positive . Aage padhne se pehle apna guess likhlo.
-
aur banao. Yeh step kyun? s ka pehla column bias ko ke andar rehne deta hai, taaki ek formula dono knobs handle kare.
-
aur banao. Yeh step kyun? Normal equation ko exactly yahi do objects chahiye. saare inputs ko ek summary mein collapse karta hai (row·column sums: top-left hai , bottom-right hai ); har input column ko answers ke saath pair karta hai (, aur ).
-
ko invert karo. Determinant . Yeh step kyun? ko isolate karne ke liye hume multiply-by- ko undo karna hai; ke liye diagonal swap karo, off-diagonal negate karo, determinant se divide karo.
-
Multiply out karo. Toh .
Verify: , , . Residuals ka sum — exactly yahi ek least-squares fit force karta hai. Slope ne hamare forecast se match kiya. ✓
Figure — aise padhein: black dots teen data points hain, red line fitted hai, aur har dashed vertical segment ek residual hai (ek dot red line ke kitna upar ya neeche hai). Notice karo ki beech wala dot line ke upar hai jabki bahar ke dono neeche hain — yeh visual balance residuals ka zero tak sum hona hai.

Example 2 — Cell B: perfectly collinear data
Forecast: agar points already ek line par hain, toh "best" line wahi line hai, aur total error exactly zero hona chahiye.
- Banao. , .
- Summaries. , . Yeh step kyun? Ex 1 jaisi hi normal-equation recipe — top-left , bottom-right ; "perfect data" procedure ko kuch nahi badalta.
- Determinant , toh Yeh step kyun? Ex 1 jaisi hi isolate- wajah: multiply undo karne ke liye invert karo.
- Solve karo. . Toh .
Verify: exactly deta hai — residual vector hai, cost . Fit exact hai, forecast se match karta hai. ✓
Example 3 — Cell C: negative slope
Forecast: downhill data → . mein ka har step ko se girata hai, toh expect karo , .
- Banao. , .
- Summaries. (Ex 1 jaisi hi 's!), . Yeh step kyun? sirf inputs par depend karta hai, isliye Ex 1 ka inverse reuse karte hain; sirf badalta hai (, ).
- Solve karo. . Toh .
Verify: → : exact fit hai, aur negative slope confirm karta hai jo haari aankhon ne predict kiya tha. ✓ Lesson: negative answer koi error nahi hai — sign direction encode karta hai.
Example 4 — Cell D: zero slope (flat line)
Forecast: completely ko ignore karta hai, toh best line flat hai: , .
- Summaries. , . Yeh step kyun? 's Ex 1 jaisi hain toh unchanged hai; answers ko har column ke against sum karta hai (, ). Hum yeh dono isliye banate hain kyunki normal equation ko exactly yahi feed hota hai.
- Solve karo. . Yeh step kyun? ke respect mein gradient tabhi zero hota hai jab slope kuch contribute nahi karta — algebra exactly de deta hai.
Verify: har jagah, residuals sab . Flat target genuinely flat fit produce karta hai. ✓
Example 5 — Cell E: singular / degenerate input
Forecast: saare points vertical line par hain. Ek function vertical nahi ho sakta. Machine ke tootne ki expect karo — inverse exist nahi karna chahiye.
- Banao. , .
- Summaries. . Yeh step kyun? Same recipe; note karo ki -column exactly -column ka hai, ek warning sign jo agli step mein feel hoga.
- Determinant. . Yeh step kyun? Zero determinant matlab matrix ka koi inverse nahi — normal equation formula undefined hai. Yeh multicollinearity hai: bias column aur -column same information carry karte hain (ek doosre ka hai), toh hum aur alag nahi kar sakte.
Verify: determinant literally hai, forecast confirm karta hai. Fixes: redundant column drop karo, ya thoda penalty add karo — dekho Regularization (Ridge, Lasso), jo ko se replace karta hai taaki determinant kabhi zero na ho. ✓
Figure — aise padhein: red vertical line woh jagah hai jahan teeno black dots rehte hain (). form ki koi bhi sloped-ya-flat line vertical trace nahi kar sakti — yeh impossibility exactly zero determinant hai ek picture ke roop mein.

Example 6 — Cell F: gradient descent, do honest steps
Forecast: zero se start karte hue, pehla gradient bada aur negative hoga (predictions data se kaafi neeche hain), toh upar jump karega. Expect karo ki yeh Ex 1 ke true answer ki taraf crawl karega.
- Iteration 0 — predict karo phir error measure karo. , toh error . Yeh step kyun? Gradient ko pehle error chahiye — konsi direction mein step lena hai yeh decide karne se pehle hume jaanna hai ki hum kitne galat hain.
- Gradient.
- Update. . Yeh step kyun? Hum gradient ke against step karte hain (cost surface par downhill); scale karta hai ki kitna dur.
- Iteration 1. , error . Gradient : pehla component ; doosra component . Update . Yeh step kyun? Exactly wohi rule baar baar — descent sirf "error measure karo, downhill step lo" baar baar hai.
Verify: . Har step closed-form target ki taraf move karta hai — exactly woh crawl jo humne forecast kiya tha. ✓
Figure — aise padhein: axes do knobs hain ( horizontal, vertical). Black path pe hop karta hai, aur red star exact answer hai. Dekho ki kaise har hop shrink hota hai aur star ki taraf bend karta hai — yeh convergence hai.

Example 7 — Cell G: learning rate bahut bada
Forecast: ek giant step valley ko overshoot karega aur doosri wall par aur upar land karega. Cost worse honi chahiye, aur repeated steps blow up karenge.
- Ek update. . Yeh step kyun? step ko multiply karta hai; sign abhi bhi correct hai (downhill), lekin magnitude bahut zyada badi hai.
- Cost check karo. use karke ke saath: . ke saath: , residuals , . Yeh step kyun? ko before aur after compare karne se pata chalta hai ki step ne help kiya ya nahi. Yahan toh woh explode ho gaya.
Verify: — cost ~100× badh gayi, divergence confirm karti hai. Rule of thumb: agar increase kare, toh aadha kar do. Convex Optimization guarantee karta hai ki ek valley exist karta hai; bahut bade steps usse find nahi kar sakte. ✓
Example 8 — Cell H: badly scaled features
Forecast: metre-feature ka gradient component fraction ka daba dega, toh ek knob race karega jabki doosra barely move karega — descent zig-zag karega.
- Gradient component per feature. Ek point ke liye, . Yeh step kyun? Gradient error times feature value hota hai; ek bada feature value bada component banata hai.
- Ratio. . Yeh step kyun? Do knobs factor se alag forces feel karte hain; koi bhi single dono mein se ek ke liye galat hoga.
Verify: component ratio feature-magnitude ratio se exactly match karta hai — proof ki imbalance purely scale se aata hai. Fix: pehle features standardise karo — dekho Feature Scaling. Scaling ke baad, dono components comparable hote hain aur descent seedha downhill jaata hai. ✓
Example 9 — Cell I: aerospace word problem
Forecast: humne yeh data already fit kiya tha (Ex 1): . par expect karo kg/s.
- Fit reuse karo. , . Yeh step kyun? Calibration = ek baar fit karo, phir new readings ke liye line ko lookup ki tarah use karo.
- par predict karo. . Yeh step kyun? Hum data se ek volt bahar extrapolate kar rahe hain — allowed hai kyunki physical relation yahan linear hai.
Verify: units check — slope kg/s per volt hai, volts se multiply karo toh kg/s milta hai; bias kg/s hai; sum kg/s hai. ✓ Answer kg/s forecast se match karta hai. (Practice mein hum ko – V calibration range se thoda bahar flag karenge.)
Example 10 — Cell J: exam twist (geometry proof)
Forecast: normal equation condition se hi janma tha, toh yeh exactly hold hona chahiye — geometry hai, luck nahi.
- Residuals compute karo. Ex 1 se, , toh . Yeh step kyun? Perpendicularity ke baare mein ek statement hai, toh pehle numbers mein chahiye.
- Column 1 se dot karo (s wala). . Yeh step kyun? All-ones column se dot karna bias direction mein perpendicularity test karta hai — geometrically matlab hai ki residuals ka sum zero hona chahiye (koi leftover vertical offset nahi jo bias abhi bhi absorb kar sake).
- Column 2 se dot karo ('s wala). . Yeh step kyun? -column se dot karna slope direction mein perpendicularity test karta hai — matlab hai ki errors mein koi leftover linear trend nahi jo slope abhi bhi exploit kar sake. Dono dots zero hone ka matlab hai , proof complete.
Verify: dono dot products hain, toh . Geometrically, ka shadow (orthogonal projection) hai ke columns se spanned plane par; bacha hua us plane se seedha bahar point karta hai. Yeh Least Squares Estimation ka dil hai. ✓
Figure — aise padhein: black horizontal arrow column space ke andar lie karta hai (black line ke roop mein draw kiya); tall black arrow hai; red arrow residual hai jo ki tip se tak jaata hai. Chhota square dikhata hai ki red arrow column space se right angle par milta hai — woh right angle hi hai.

Recall Self-test
Kis cell mein koi inverse nahi hai aur kyun? ::: Cell E — saab equal hone se hota hai (bias aur feature columns proportional hain). Agar gradient descent ki cost badhe, toh kya karte ho? ::: Learning rate aadha kar do (Cell G). Negative ka matlab kya hai? ::: Downhill data — badhne par kam hota hai (Cell C). Residuals ke columns ke orthogonal kyun hone chahiye? ::: Warna ek leftover trend abhi bhi remove kiya ja sakta tha, toh fit optimal nahi tha (Cell J).