Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🤖 Week 7 – Machine Learning Core  ·  दिन 36

दिन 36: Machine Learning क्या है?

⏱ 90 min📶 Beginner🎯 100% self-study
दिन 36 / 60

✅ आज आप सीखेंगे

  • Rules vs examples से सीखना
  • Supervised, unsupervised, reinforcement
  • Regression vs classification
  • Features, labels, training और testing
  • Overfitting और 7-step ML workflow

🏢 असली दुनिया में

एक bank पहले rule book से loans approve करता था: “income ₹40,000 से ऊपर और credit score 700 से ऊपर”। अच्छे customers छूट जाते और risky approve हो जाते। 1,200 पुरानी applications और उनके नतीजों से machine-learning model pattern ख़ुद सीखता है – और नए data के साथ बेहतर होता रहता है।

Rules vs learning

Traditional programming

  • Rules आप लिखते हैं
  • Data + rules → जवाब
  • दुनिया बदले तो टूटता है
  • Example: if score > 700 then approve

Machine learning

  • Rules computer ढूँढता है
  • Data + जवाब → rules (model)
  • Data बदले तो फिर train
  • Example: model 1,200 पुराने loans से सीखता है
Python
import pandas as pd
loans = pd.read_csv("loan_applications.csv")
print(loans.head())
rule = (loans["monthly_income"] > 40000) & (loans["credit_score"] > 700)
print(f"Old rule accuracy: {(rule.astype(int) == loans['approved']).mean():.1%}")
print(f"Approval rate in history: {loans['approved'].mean():.1%}")
applicant_id monthly_income credit_score loan_amount employment years_employed approved 0 L5001 45300 819 502000 Salaried 12 1 1 L5002 61000 799 806000 Self-Employed 25 1 2 L5003 71200 801 367000 Self-Employed 3 1 3 L5004 35000 775 611000 Salaried 19 1 4 L5005 76500 737 2083000 Salaried 27 0 Old rule accuracy: 72.2% Approval rate in history: 46.2%
Python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
loans = pd.read_csv("loan_applications.csv")
X = loans[["monthly_income", "credit_score", "loan_amount", "years_employed"]]
y = loans["approved"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
model = DecisionTreeClassifier(max_depth=4, random_state=42).fit(X_train, y_train)
print(f"ML model accuracy on unseen applications: {model.score(X_test, y_test):.1%}")
ML model accuracy on unseen applications: 81.0%
🎉 आपका पहला model

scikit-learn की पाँच lines ने उन applications पर hand-written rule को हरा दिया जो model ने कभी नहीं देखीं। कल आप हर line ठीक से समझेंगे।

Machine learning के प्रकार

🏷️
Supervisedजवाब (labels) वाले examples से सीखना। Loan approve? House price?
🧩
UnsupervisedLabels के बिना ढाँचा ढूँढना – customer segments, anomalies।
🎮
Reinforcementकोशिश और इनाम से सीखना – games, robots, ad bidding।
तीन परिवार
समस्याप्रकारOutputCourse day
House price predictSupervised – regressionnumber (₹ lakh)38–39
Loan approve करें?Supervised – classificationclass (हाँ/नहीं)40–43
Customer churn करेगा?Supervised – classificationprobability56–60
Mall customers के groupsUnsupervised – clusteringsegment id46
कई columns compress करनाUnsupervised – dimensionality reductionकम features47
Review positive/negativeSupervised – NLP classificationsentiment48
अगले महीने का AQITime-series forecastingभविष्य की values49

ज़रूरी शब्द

Python
import pandas as pd
h = pd.read_csv("house_prices.csv")
X = h.drop(columns="price_lakh")      # features (inputs)
y = h["price_lakh"]                   # label / target (what we predict)
print("Features:", list(X.columns))
print("Target:", y.name, "| one example →", dict(X.iloc[0]), "→", y.iloc[0], "lakh")
Features: ['city', 'area_sqft', 'bedrooms', 'bathrooms', 'age_years', 'metro_km', 'furnishing', 'parking'] Target: price_lakh | one example → {'city': 'Pune', 'area_sqft': np.int64(1210), 'bedrooms': np.int64(2), 'bathrooms': np.int64(2), 'age_years': np.int64(18), 'metro_km': np.float64(7.0), 'furnishing': 'Furnished', 'parking': 'No'} → 55.07 lakh
शब्दमतलब
Feature (X)Input column: area, bedrooms, city
Label / target (y)जो जवाब predict करना है: price
Modelसीखा हुआ function f(X) ≈ y
Trainingजाने-पहचाने examples पर model fit करना
Test setModel की ईमानदार जाँच के लिए अलग रखे examples
Predictionनए X के लिए model का output
OverfittingTraining data रटना, नए data पर fail

Overfitting – रटना vs सीखना

Python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
loans = pd.read_csv("loan_applications.csv")
X = loans[["monthly_income", "credit_score", "loan_amount", "years_employed"]]; y = loans["approved"]
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
for depth in [1, 2, 4, 8, 16, None]:
    m = DecisionTreeClassifier(max_depth=depth, random_state=42).fit(X_tr, y_tr)
    print(f"depth={str(depth):4s} train={m.score(X_tr, y_tr):.3f}  test={m.score(X_te, y_te):.3f}")
depth=1 train=0.787 test=0.817 depth=2 train=0.787 test=0.817 depth=4 train=0.829 test=0.810 depth=8 train=0.922 test=0.783 depth=16 train=1.000 test=0.743 depth=None train=1.000 test=0.743
Python
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
loans = pd.read_csv("loan_applications.csv")
X = loans[["monthly_income", "credit_score", "loan_amount", "years_employed"]]; y = loans["approved"]
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
d = range(1, 16); tr, te = [], []
for k in d:
    m = DecisionTreeClassifier(max_depth=k, random_state=42).fit(X_tr, y_tr)
    tr.append(m.score(X_tr, y_tr)); te.append(m.score(X_te, y_te))
plt.figure(figsize=(8.5, 3.8))
plt.plot(d, tr, marker="o", label="training accuracy", color="#94a3b8")
plt.plot(d, te, marker="o", label="test accuracy", color="#4f46e5")
best = d[te.index(max(te))]; plt.axvline(best, color="#16a34a", ls="--", label=f"sweet spot (depth {best})")
plt.xlabel("model complexity (tree depth)"); plt.ylabel("accuracy"); plt.legend()
plt.title("Too simple = underfit, too complex = overfit"); plt.show()
Tree depth के साथ training और test accuracy, overfitting दिखाते हुए
Training accuracy बढ़ती रहती है; test accuracy ऊपर जाकर गिरती है। हमें peak चाहिए।
📚 Exam से तुलना

जो student पिछले साल का paper रट ले, वह उस पर 100% लाता है पर नए paper में fail। Test set ही “नया paper” है – training में model को इसे कभी न दिखाएँ।

ML workflow

  1. 1Business समस्या और metric तय करें
  2. 2Data इकट्ठा और साफ़ करें
  3. 3EDA और feature engineering
  4. 4Train / test split
  5. 5Baseline, फिर बेहतर models train करें
  6. 6Test set पर evaluate
  7. 7Deploy, monitor, retrain
Churn project (Days 56–60) में आप सातों करेंगे
⚠️ ML हमेशा जवाब नहीं

अगर simple rule या SQL report समस्या हल करे तो वही लें। ML के लिए labels वाला काफ़ी अच्छा पुराना data, साफ़ metric और उसे संभालने वाला चाहिए।

🏅 आज के Job Skills

✔ ML vs traditional programming समझाना✔ समस्याओं को regression, classification या clustering में बाँटना✔ Features, target, train, test शब्द इस्तेमाल करना✔ Overfitting पहचानना

Practice tasks

🏋️ Practice – ख़ुद करें

Task 1: हर समस्या का प्रकार बताएँ: (a) कल का Delhi AQI, (b) spam SMS पहचानना, (c) shoppers को व्यवहार से group करना, (d) पुरानी car की कीमत।
Task 2: Decision tree को सिर्फ़ credit_score और monthly_income से train करें। कितनी accuracy कम हुई?
Solution देखें
Python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
l = pd.read_csv("loan_applications.csv")
X_tr, X_te, y_tr, y_te = train_test_split(l[["credit_score", "monthly_income"]], l["approved"], test_size=0.25, random_state=42, stratify=l["approved"])
print(round(DecisionTreeClassifier(max_depth=4, random_state=42).fit(X_tr, y_tr).score(X_te, y_te), 3))
0.783
Task 3: Student average marks predict करने वाले model के features और target लिखें।

Quick quiz

🧠 Quick Quiz – ख़ुद को जाँचें

1. House price predict करना है…
Output एक number है।
2. बिना labels customers को group करना है…
Clustering unsupervised है।
3. Training accuracy 100%, test 70% मतलब…
उसने training data रट लिया।
4. Test set किसलिए?
आख़िर तक इसे न छुएँ।
5. scikit-learn में y क्या है?
X = features, y = target।

Interview corner

💼 Interview Corner

Supervised और unsupervised learning में क्या फ़र्क है?

Supervised learning labelled examples पर train होकर जाना-पहचाना target predict करती है (price, churn)। Unsupervised learning बिना labels के data में ढाँचा ढूँढती है (clusters, components, anomalies)।

Overfitting क्या है और कैसे रोकें?

Model training data का शोर सीख लेता है और नए data पर fail होता है। Held-out test set, cross-validation, सरल models या regularisation, ज़्यादा data, और early stopping से रोकें।

सारांश

  • ML rules हाथ से लिखने की जगह examples से सीखता है।
  • Supervised (regression, classification), unsupervised, reinforcement।
  • X = features, y = target; हमेशा अनदेखे test data पर evaluate।
  • Overfitting = रटना; सबसे अच्छे test score का लक्ष्य।
➡️ आगे

Day 37: scikit-learn step by step – fit, predict, score, और सही baseline।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या ML के लिए advanced maths चाहिए?
ML अच्छे से इस्तेमाल करने के लिए calculus से ज़्यादा statistics (Days 26–31) की समझ चाहिए। Research roles में बाद में maths मदद करता है।
क्या AI और ML एक हैं?
AI बुद्धिमान machines का बड़ा लक्ष्य है। आज AI बनाने का मुख्य तरीका ML है; deep learning (Day 51) ML का हिस्सा है।
← Data Science (60 Days)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicदिन 37: scikit-learn और Train/Test Split आपकी प्रगति: 36 / 60 (60%) · Lesson 37 / 60 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें