Day 36: What is Machine Learning?
✅ 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
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%}")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%}")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
| Problem | Type | Output | Course day |
|---|---|---|---|
| Predict house price | Supervised – regression | a number (₹ lakh) | 38–39 |
| Approve a loan? | Supervised – classification | a class (yes/no) | 40–43 |
| Will a customer churn? | Supervised – classification | probability | 56–60 |
| Group mall customers | Unsupervised – clustering | segment id | 46 |
| Compress many columns | Unsupervised – dimensionality reduction | fewer features | 47 |
| Review positive/negative | Supervised – NLP classification | sentiment | 48 |
| Next month’s AQI | Time-series forecasting | future values | 49 |
Key words
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")| Word | Meaning |
|---|---|
| Feature (X) | An input column: area, bedrooms, city |
| Label / target (y) | The answer we want to predict: price |
| Model | The learned function f(X) ≈ y |
| Training | Fitting the model on known examples |
| Test set | Held-back examples to check the model honestly |
| Prediction | Model output for new X |
| Overfitting | Memorising training data, failing on new data |
Overfitting – memorising vs learning
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}")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()
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
- 1Define the business problem and metric
- 2Collect and clean data
- 3EDA and feature engineering
- 4Split train / test
- 5Train baseline, then better models
- 6Evaluate on test set
- 7Deploy, monitor, retrain
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
Practice tasks
🏋️ Practice – do it yourself
credit_score and monthly_income. How much accuracy is lost?Show solution
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))Quick quiz
🧠 Quick Quiz – test yourself
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.
Day 37: scikit-learn step by step – fit, predict, score, and a proper baseline.
