5.6.1 · D2 · HinglishMachine Learning (Aerospace Applications)

Visual walkthroughLinear regression — normal equation, gradient descent derivation

2,230 words10 min read↑ Read in English

5.6.1 · D2 · Coding › Machine Learning (Aerospace Applications) › Linear regression — normal equation, gradient descent deriva

Yeh page normal equation ko ek picture ek time pe rebuild karta hai, dots ki ek scatter se shuru karke ek single formula tak pahunchta hai:

Yahan har symbol use hone se pehle earn kiya jaata hai. Agar aapne kabhi matrix, dot product, ya derivative nahi dekha, toh line one se shuru karo — hum sab kuch build karte hain.


Step 1 — Dots aur woh line jo hum dhundh rahe hain

KYA. Humein measurements di gayi hain. Har measurement ek pair hai: ek input (maan lo, altitude) aur ek output (maan lo, fuel-burn rate). Chhota sirf ek naam-tag hai: pehla dot hai, doosra, aur aise tak (total dots hain).

Hum ek line chahte hain

  • (padho "y-hat") line ki prediction hai — woh height jo line input pe claim karti hai.
  • slope hai: line kitni steeply utha karti hai. ko 1 badlao, se badlega.
  • bias (ya intercept) hai: line ki height jahan ho.

LINE kyun? Kyunki yeh sabse simple honest guess hai: constant slope, koi curves nahi. Isse mushkil sab kuch iske upar build hota hai.

PICTURE. Blue mein dots, ek candidate line yellow mein. Har dot ke liye, dot aur line ke beech ka vertical red gap error hai — wahan line kitni galat hai.

Figure — Linear regression — normal equation, gradient descent derivation

Step 2 — Total galti measure karna: hum square kyun karte hain

KYA. Dot ke liye error hai (true height minus predicted height). Hum saare errors jod lete hain — lekin squared:

Symbols padhna:

  • ka matlab hai "har dot ke liye se tak ye add karo".
  • ek red gap hai, squared — hamesha positive.
  • ek tidy-up factor hai: se divide karne se yeh ek average ban jaata hai, aur ek cancel karega jo baad mein differentiate karne pe aata hai. Yeh sirf minimum ki value badalta hai, minimum kahan baith hai nahi.
  • total badness score hai. Chhota = behtar line.

SQUARE kyun, seedha add kyun nahi? Do reasons hain, dono figure mein dikhaye. (1) Line ke upar ka dot positive gap rakhta hai, neeche wala negative gap — agar hum unhe raw jodein, woh cancel ho jaenge aur ek buri line zero score kar sakti thi. Squaring har gap ko positive banati hai. (2) Squared curve smooth hai (ek parabola), toh calculus — jise hर jagah slopes chahiye — cleanly kaam karta hai.

PICTURE. Left: raw gaps cancel ho rahe hain (bura). Right: har gap ek red square mein badal raha hai jiska area penalty hai; bade gaps chhote walon se bahut zyada punish hote hain.

Figure — Linear regression — normal equation, gradient descent derivation

Step 3 — Saare dots ko ek matrix mein pack karna

KYA. baar baar likhna clumsy hai. Hum data ko ek matrix aur ek vector mein stack karte hain.

  • Ek vector numbers ka ek column hai, e.g. — har row pe ek target.
  • Ek matrix numbers ki ek grid hai. Hum ko har dot ke liye ek row ke saath banate hain. Bias ko bhi capture karne ke liye, hum saamne s ka ek column chipka dete hain:

Ab — matrix times vector — ek saath predictions ka poora column hai. Product ki row hai . s ka column hi hai jo ko ek real column banata hai taaki woh multiplication mein saath aaye.

KYUN. Taaki puri cost ek clean object ban jaaye:

Yahan (squared norm) ka matlab hai " ki entries ke squares jodo" — exactly hamara sum of squared gaps, ab ek symbol mein.

PICTURE. Left pe dot-by-dot table boxed grid mein collapse ho raha hai, s ka column highlighted, arrow ki taraf.

Figure — Linear regression — normal equation, gradient descent derivation

Step 4 — Poori geometry ek flat sheet mein rehti hai

KYA. Yeh picture hai jo formula ko inevitable banati hai. ko space mein ek single arrow socho (har dot ke liye ek axis — axes). Har possible line jo hum draw kar sakte hain koi deti hai, ek aur arrow. Jab hum dials aur ghoomte hain, ki tip origin se ek flat sheet sweep karti hai: ki column space ( ke columns ke saare combinations).

ISKA MATLAB KYUN. Hamara target arrow almost kabhi us sheet pe nahi hota (real data perfectly fit nahi hota line mein). Toh "best line" = "sheet pe woh point jo ke sabse paas ho". Is space mein distance exactly hai — hamara error. Squared error minimize karna = sheet pe closest point dhundhna.

PICTURE. Green sheet column space hai. (blue) uske upar float kar raha hai. Candidate predictions sheet pe rehte hain.

Figure — Linear regression — normal equation, gradient descent derivation

Step 5 — Closest point = perpendicular giraa do

KYA. Bahar ke kisi point se flat sheet pe closest point seedha neeche giraakar milta hai — sheet ke perpendicular. Us perpendicular ke paaon ko bolo. Bacha hua arrow,

residual (red), sheet ke perpendicular hona chahiye.

KYUN. Agar residual sheet ke saath thoda bhi jhuk raha ho, toh hum us direction mein thoda slide karke closer aa sakte the — matlab hum abhi minimum pe nahi hote. True minimum pe residual seedha bahar ki taraf point karta hai. Yeh geometrically "derivative bottom pe zero hai" ka twin hai.

Poori sheet ke perpendicular hone ka matlab hai ke har column ke perpendicular. Zero dot product perpendicular matlab hai, toh:

ki har entry ka ek column ke saath dotted hai; sab ko zero set karna kehta hai har column.

PICTURE. Red residual green sheet se right-angle mark pe milta hai; ke yellow columns sheet mein flat hain, har ek residual ke pe.

Figure — Linear regression — normal equation, gradient descent derivation

Step 6 — Calculus se same answer (slope-is-zero view)

KYA. Step 5 ko algebra se re-derive karte hain, taaki dono pictures agree karein. Step 3 ke dot-product facts use karke cost expand karo:

KYUN. Dials ka function ke roop mein, yeh ek bowl hai (ek paraboloid) — quadratic term guarantee karta hai ki yeh upar curve karta hai. Ek bowl ka exactly ek lowest point hota hai, aur bottom pe uski slope har direction mein zero hoti hai. Har-direction-mein-slope gradient hai: ek vector jiske entries partial derivatives hain (har dial ko nudge karne pe ki sensitivity). Ise set karo:

Yeh normal equation Step 5 ke identical hai ( expand karo). Geometry aur calculus same line pe aakar milte hain.

PICTURE. plane ke upar bowl-shaped cost surface; ek ball single bottom ki taraf roll karti hai jahan tangent plane flat hai.

Figure — Linear regression — normal equation, gradient descent derivation

Step 7 — Winning dials ke liye solve karo

KYA. se, left pe matrix ko uske inverse se multiply karke undo karo (matrix version of dividing):

Worked check (parent ka Example 1). Data :

Toh , : fitted line hai .

PICTURE. Teen dots solved yellow line ke saath, residuals dikhaye, aur fitted numbers label kiye.

Figure — Linear regression — normal equation, gradient descent derivation

Step 8 — Degenerate case: jab inverse mar jaata hai

KYA. Upar ka sab kuch ke exist hone pe depend karta tha. Yeh fail hota hai jab ke do columns same information carry karte hain — e.g. altitude metres mein aur altitude feet mein. Tab green sheet squash ho jaati hai: infinitely many lines best ke liye tie karti hain, aur singular ho jaata hai (koi inverse nahi).

KYUN TOOT TA HAI. Geometrically columns parallel hain, toh "sheet" actually ek patla slab hai — perpendicular ka paaon unique nahi hai. Algebraically , aur aap divide nahi kar sakte.

Fix. Moore–Penrose pseudoinverse use karo, jo sabse chhota valid choose karta hai, ya ek nudge add karo (dekho ridge regression): , jo ke liye hamesha invertible hota hai. Saath hi, bahut alag feature scales numerically ko almost singular banate hain — Feature Scaling se thik hota hai.

PICTURE. Left: healthy full sheet, unique foot. Right: collapsed slab, tied feet ki ek poori line — ambiguity.

Figure — Linear regression — normal equation, gradient descent derivation

Ek-picture summary

Dots → squared gaps → column-space sheet → perpendicular giraa do → normal equation → solved line. Ek figure, poora safar.

Figure — Linear regression — normal equation, gradient descent derivation
Recall Feynman retelling — ise ek story ki tarah bolo

Humne kuch dots scatter kiye aur unse guzarne wali sabse seedhi, sabse fair line chahte the. "Fair" ka matlab tha: har dot pe kitna off hai yeh jodo, lekin har miss ko square karo taaki ups aur downs chhup ke cancel na ho sakein aur math smooth rahe. Humne saare dots ko ek grid mein pack kiya (s ke ek column ke saath taaki line ki height free mein saath aa jaaye) aur saare answers ko ek column mein. Phir aaya woh khoobsurat trick: ko ek arrow socho, aur har possible line ko ek flat sheet pe padi ek arrow. Best line ka us sheet pe shadow hai — tum seedhe neeche giraate ho, aur bacha hua error sheet ke bilkul perpendicular point karta hai. "Har column ke perpendicular" symbols mein likha jaata hai , jo rearrange hoke banta hai . Calculus agree karta hai: cost ek bowl hai, aur bowl ka bottom wahan hota hai jahan har slope zero ho — same equation. Ek hi catch hai jab do columns same baat kehte hain; tab sheet collapse ho jaati hai, inverse mar jaata hai, aur hum pseudoinverse ya ek tiny ridge nudge pe lean karte hain. Hamare teen dots ke liye isne nikala, bina kisi guessing ke.

Recall Quick self-test

Hum errors ko raw add karne ki jagah square kyun karte hain? ::: Taaki positive aur negative gaps cancel na ho sakein, aur taaki cost ek smooth parabola/bowl ho jisme har jagah derivatives hon. geometrically kya matlab hai? ::: Residual (error arrow) ke har column ke, yaani poori column-space sheet ke, perpendicular hai. kab exist nahi karta? ::: Jab ke columns redundant hon (multicollinearity), ko singular bana dete hain. Data ke liye, normal equation kaunsi line deta hai? ::: .