DAY 31 • ML ALGORITHMS
Linear Regression Explained: Machine Learning Ka Sabse Fundamental Algorithm
Suppose kijiye ki aap ek ghar khareedna chahte hain. Aap dekhte hain ki 1,000 sq ft ke ghar ki kimat ₹50 Lakh hai, aur 1,500 sq ft ke ghar ki kimat ₹75 Lakh hai. Agar main aapko boloon ki 2,000 sq ft ke ghar ki kimat estimate kijiye, to aapka dimaag turant ek pattern notice karega: “Jaise-jaise size badh raha hai, kimat bhi ek specific rate par badh rahi hai.”
Aapne abhi-abhi apne dimaag mein jo calculation kiya, wahi Linear Regression hai!
Namaste! Main hoon Coach Krish. Day 30 ke lesson mein humne samjha tha ki ek Machine Learning model data se kaise seekhta hai — Cost Function aur Gradient Descent ke zariye. Aaj hum Supervised Machine Learning ke sabse pehle aur sabse famous algorithm ko samajhne wale hain: Linear Regression.
📌 What You Will Learn Today
- Linear Regression kya hai aur ye kis tarah ki problems solve karta hai.
- Line of Best Fit, Weights ($w$), aur Bias ($b$) ka intuitive matlab.
- Linear Regression mathematically kaise kaam karta hai ($y = wx + b$).
- Python mein scikit-learn se Linear Regression code karne ka tareeka.
- Is algorithm ki real-world limitations aur common misconceptions.
1. Linear Regression Kya Hai?
Linear Regression Supervised Machine Learning ka ek algorithm hai jo Continuous Numerical Values predict karne ke liye use hota hai.
Machine Learning mein do main tarah ki Supervised problems hoti hain:
- Classification: Jab output discrete categories mein ho (e.g., Spam vs Not Spam, Pass vs Fail).
- Regression: Jab output ek continuous number ho (e.g., Price, Salary, Temperature, Sales).
Linear Regression ka main goal hota hai Input Features (jaise House Size) aur Output Target (jaise House Price) ke beech ek straight-line relationship (rekha) dhundna.
🧠 Mental Model: The Best Fit Line
Imagine kijiye ki aapke paas ek graph paper hai. X-axis par house size hai aur Y-axis par house price. Data points graph par alag-alag dots ki tarah phaile hue hain.
Linear Regression ka kaam ek aisi straight line draw karna hai jo in sabhi dots ke jitni ho sake utni paas se guzre. Is line ko hum kehate hain Line of Best Fit.
2. Linear Regression Ka Math: Made Simple
School ke maths se aapko line ki equation yaad hogi:
Equation: y = w · x + b
(School math mein hum ise y = m · x + c likhte the)
Aaiye is equation ke har ek part ko Machine Learning ke context mein samajhte hain:
| Symbol | ML Terminology | Meaning & Intuition |
|---|---|---|
| y | Target / Dependent Variable | Jo value hum predict karna chahte hain (e.g., House Price). |
| x | Feature / Independent Variable | Input data jo hum model ko dete hain (e.g., House Size in sq ft). |
| w | Weight / Slope / Coefficient | Feature ki importance. Ek sq ft badhne se price kitni badhegi? |
| b | Bias / Intercept | Base value. Agar house size 0 ho, tab bhi starting prediction kya hoga? |
Model Best Line Kaise Dhundta Hai? (Connecting to Day 30)
Day 30 mein humne padha tha ki model sabse pehle randomly weights ($w$) aur bias ($b$) choose karta hai. Phir wo Mean Squared Error (MSE) ke zariye error calculate karta hai:
$$\text{MSE} = \frac{1}{N} \sum (y_{\text{actual}} – y_{\text{predicted}})^2$$
Uske baad Gradient Descent algorithm weights ($w$) aur bias ($b$) ko adjust karta hai jab tak error minimum na ho jaye. Jab error minimum ho jata hai, humein hamari final Line of Best Fit mil jaati hai!
3. Real-World Example: Salary Prediction
Maan lijiye humare paas 5 employees ka data hai — unka Experience (years) aur unki Monthly Salary (in ₹):
- 1 Year Experience → ₹30,000
- 2 Years Experience → ₹40,000
- 3 Years Experience → ₹50,000
- 4 Years Experience → ₹60,000
Yahan agar model train hone ke baad humein equation deta hai: Salary = 10,000 × (Experience) + 20,000
To yahan:
- Weight ($w$): 10,000 (Har saal experience badhne par salary ₹10k badhti hai).
- Bias ($b$): 20,000 (Starting base salary).
- Agar kisi ka experience 5 saal hai, to prediction hogi:
10,000 × 5 + 20,000 = ₹70,000.
4. Python Code Example (scikit-learn)
Real world mein hum Linear Regression khud se haath se calculate nahi karte. Python ki scikit-learn library is kaam ko 3 lines mein kar deti hai:
# Step 1: Import LinearRegression model
from sklearn.linear_model import LinearRegression
import numpy as np
# Step 2: Sample Training Data (Experience in years vs Salary in Thousands)
# X: Inputs (Must be 2D array in scikit-learn)
X = np.array([[1], [2], [3], [4]])
# y: Target outputs
y = np.array([30, 40, 50, 60])
# Step 3: Initialize and train the model
model = LinearRegression()
model.fit(X, y) # Model learns the optimal weight (w) and bias (b)
# Step 4: Make a prediction for 5 years of experience
new_experience = np.array([[5]])
predicted_salary = model.predict(new_experience)
print(f"Predicted Salary for 5 years: ₹{predicted_salary[0]:.2f} Thousand")
# Output: Predicted Salary for 5 years: ₹70.00 Thousand
print(f"Weight (Slope): {model.coef_[0]}") # w = 10
print(f"Bias (Intercept): {model.intercept_}") # b = 20
5. Common Misconceptions & Limitations
⚠️ Limitation: Sensitive to Outliers
Outlier ka matlab hai aisa data point jo baaki data se bilkul alag ho. Agar data mein ek aisi entry aa jaye (jaise 1 saal experience par ₹5 Lakh salary), to Linear Regression ki line us ek point ki taraf kheench jaayegi, jisse baaki sabhi predictions kharab ho sakti hain.
❌ Misconception: Linear Regression Har Target Ke Liye Sahi Hai
Fact: Linear Regression sirf tabhi kaam karta hai jab input aur output ke beech linear (straight-line) relationship ho. Agar relationship complex ya non-linear hai (e.g., curve waali), to Linear Regression wahan fail ho jata hai.
💡 Krish’s Coach Note
Jab bhi aap koi naye Machine Learning problem par kaam shuru karein, hamesha sabse pehle Linear Regression jaise simple baseline model se shuru karein. Direct Deep Learning ya Complex Neural Networks lagana zaroori nahi hota. Agar simple model accha result de raha hai, to wahi sabse efficient aur fast solution hai!
6. Quick Recall & Self-Check
Q1: Linear Regression Classification ke liye use hota hai ya Regression ke liye?
Linear Regression Regression problems ke liye use hota hai, jahan hum continuous numerical values (jaise price, height, salary) predict karte hain.
Q2: Equation y = wx + b mein ‘w’ kya represent karta hai?
‘w’ Weight (ya Slope/Coefficient) ko represent karta hai, jo batata hai ki Input feature (x) ke change hone se Output (y) kitna change hota hai.
Q3: Linear Regression algorithm best line kaise chunata hai?
Linear Regression Mean Squared Error (MSE) loss function ko compute karke aur Gradient Descent ka use karke weights ko update karta hai, taaki line aur actual data points ke beech ka distance (error) minimum ho.
🔑 Key Takeaways
- Linear Regression Supervised Learning ka foundational algorithm hai jo continuous numeric outputs predict karta hai.
- Iska core objective data points ke beech ek Line of Best Fit draw karna hota hai.
- Mathematical formula:
y = w · x + b. - Ye algorithm outliers ke prati sensitive hota hai aur linear relationships ko prefer karta hai.
What Comes Next?
Aaj humne dekha ki continuous numbers (jaise ₹50 Lakh, 70kg, 35°C) kaise predict kiye jaate hain. Lekin tab kya jab humein number ke bajay ayesa prediction karna ho ki “Is patient ko diabetes hai ya nahi (Yes / No)?”
Kal Day 32: Logistic Regression Explained mein hum sikhenge ki regression word hote hue bhi wo algorithm Classification problems ko kaise master karta hai!
Prefer learning in English? Continue with the English version of Day 31.