Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🤖 Week 7 – Machine Learning Core  ·  Day 36

Day 36: What is Machine Learning?

⏱ 90 min📶 Beginner🎯 100% self-study
Day 36 / 60

✅ Today you will learn

  • Rules vs learning from examples
  • Supervised, unsupervised, reinforcement
  • Regression vs classification
  • Features, labels, training and testing
  • Overfitting and the 7-step ML workflow

🏢 In the real world

A bank used to approve loans with a rule book: “income above ₹40,000 and credit score above 700”. It missed good customers and approved risky ones. With 1,200 past applications and their outcomes, a machine-learning model learns the pattern itself – and keeps improving as new data arrives.

Rules vs learning

Traditional programming

  • You write the rules
  • Data + rules → answers
  • Breaks when the world changes
  • Example: if score > 700 then approve

Machine learning

  • The computer finds the rules
  • Data + answers → rules (model)
  • Retrain when data changes
  • Example: model learns from 1,200 past 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%
🎉 Your first model

Five lines of scikit-learn beat the hand-written rule on applications the model has never seen. Tomorrow you will understand each line properly.

Types of machine learning

🏷️
SupervisedLearn from examples with answers (labels). Loan approved? House price?
🧩
UnsupervisedFind structure without labels – customer segments, anomalies.
🎮
ReinforcementLearn by trial and reward – games, robots, ad bidding.
Three families
ProblemTypeOutputCourse day
Predict house priceSupervised – regressiona number (₹ lakh)38–39
Approve a loan?Supervised – classificationa class (yes/no)40–43
Will a customer churn?Supervised – classificationprobability56–60
Group mall customersUnsupervised – clusteringsegment id46
Compress many columnsUnsupervised – dimensionality reductionfewer features47
Review positive/negativeSupervised – NLP classificationsentiment48
Next month’s AQITime-series forecastingfuture values49

Key words

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
WordMeaning
Feature (X)An input column: area, bedrooms, city
Label / target (y)The answer we want to predict: price
ModelThe learned function f(X) ≈ y
TrainingFitting the model on known examples
Test setHeld-back examples to check the model honestly
PredictionModel output for new X
OverfittingMemorising training data, failing on new data

Overfitting – memorising vs learning

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()
Training and test accuracy versus tree depth showing overfitting
Training accuracy keeps rising; test accuracy peaks and then falls. We want the peak.
📚 Exam analogy

A student who memorises last year’s paper scores 100% on it but fails a new paper. The test set is the “new paper” – never let the model see it during training.

The ML workflow

  1. 1Define the business problem and metric
  2. 2Collect and clean data
  3. 3EDA and feature engineering
  4. 4Split train / test
  5. 5Train baseline, then better models
  6. 6Evaluate on test set
  7. 7Deploy, monitor, retrain
You will do all 7 in the churn project (Days 56–60)
⚠️ ML is not always the answer

If a simple rule or SQL report solves the problem, use it. ML needs enough good historical data with labels, a clear metric, and someone to maintain it.

🏅 Job skills unlocked today

✔ Explain ML vs traditional programming✔ Classify problems as regression, classification or clustering✔ Use the words features, target, train, test✔ Recognise overfitting

Practice tasks

🏋️ Practice – do it yourself

Task 1: Classify each problem: (a) predict tomorrow’s Delhi AQI, (b) detect spam SMS, (c) group shoppers by behaviour, (d) estimate a used car’s price.
Task 2: Train the decision tree with only credit_score and monthly_income. How much accuracy is lost?
Show 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: List the features and the target for a model that predicts student average marks.

Quick quiz

🧠 Quick Quiz – test yourself

1. Predicting a house price is…
The output is a number.
2. Grouping customers without labels is…
Clustering is unsupervised.
3. Training accuracy 100%, test 70% means…
It memorised the training data.
4. The test set is used to…
Keep it untouched until the end.
5. In scikit-learn, y is…
X = features, y = target.

Interview corner

💼 Interview Corner

What is the difference between supervised and unsupervised learning?

Supervised learning trains on labelled examples to predict a known target (price, churn). Unsupervised learning finds structure in unlabelled data (clusters, components, anomalies).

What is overfitting and how do you prevent it?

The model learns noise in the training data and fails on new data. Prevent it with a held-out test set, cross-validation, simpler models or regularisation, more data, and early stopping.

Summary

  • ML learns rules from examples instead of hand-coding them.
  • Supervised (regression, classification), unsupervised, reinforcement.
  • X = features, y = target; always evaluate on unseen test data.
  • Overfitting = memorising; aim for the best test score.
➡️ Next

Day 37: scikit-learn step by step – fit, predict, score, and a proper baseline.

Frequently Asked Questions

Do I need advanced maths for ML?
For using ML well you need intuition about statistics (Days 26–31) more than calculus. The maths helps later for research roles.
Is AI the same as ML?
AI is the broad goal of intelligent machines. ML is the main way we build AI today; deep learning (Day 51) is a part of ML.
← Data Science (60 Days)
🔗

Share this topic with a friend

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

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

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

Next topicDay 37: scikit-learn & Train/Test Split Your progress: 36 / 60 (60%) · Lesson 37 / 60 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free