Lesson 11 � Intermediate
BINA LABELS KE,
GROUPS BANAO.
Clustering se data ko groups mein baant-te hain bina labels ke � customer segmentation, anomaly detection sab clustering se hota hai. Jab tumhare paas labeled data nahi hota tab clustering kaam aata hai. Real-world mein 80% data unlabeled hota hai � isliye clustering bahut powerful hai.
WHY: Clustering kyun zaroori hai?
Machine learning mein do tarah ka data hota hai � labeled aur unlabeled. Classification mein humne dekha ki labels hote hain (spam/not spam). Lekin real-world mein zyada tar data unlabeled hota hai. Clustering se hum data ko naturally occurring groups mein baant-te hain. Customer segmentation (kaun sa customer premium hai), anomaly detection (fraud transactions), document grouping � sab clustering se hota hai. Yeh unsupervised learning ka sabse popular technique hai.
Sabse popular clustering algorithm. Data points ko K groups mein baant-ta hai. Har group ka ek center (centroid) hota hai. Points apne nearest center ke group mein jaate hain. Simple hai, fast hai, lekin K dena padta hai.
Sahi K (number of clusters) dhoondne ka method. Alag-alag K ke liye inertia (within-cluster sum of squares) nikalte hain. Jab inertia drop hona band ho jaaye � woh point "elbow" hai. Wohi best K hai. Graph mein elbow shape dikhta hai.
Tree-like structure banata hai clusters ka. Agglomerative (bottom-up) ya Divisive (top-down). Dendrogram se visual kar sakte ho kitne clusters chahiye. K dena nahi padta � dendrogram se decide karo.
Density-based clustering. Jagah jahan points dense hain woh cluster banega. Noise points automatically identify hote hain. Irregular shapes ke clusters ban sakta hai. K dena nahi padta � epsilon aur min_samples dena padta hai.
HOW: Clustering techniques with code
Yeh 4 main techniques hain jo har data scientist use karta hai. Har ek ka apna use case hai � kab kaunsa lagana hai yeh samajhna zaroori hai.
1. K-Means � Groups banana
K-Means sabse simple aur fast clustering algorithm hai. Tum K (number of clusters) specify karte ho, aur algorithm data points ko K groups mein baant-ta hai. Har group ka ek centroid hota hai jo uska center hai.
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import numpy as np
# Customer data � age aur salary
X = np.array([[25,50000],[30,60000],[35,80000],[45,120000],
[20,30000],[50,150000],[28,55000],[40,100000]])
# Scaling zaroori hai � salary bahut badi hai age ke comparison mein
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# K-Means with 3 clusters
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X_scaled)
print(f"Labels: {kmeans.labels_}")
# Output: [0, 0, 0, 2, 0, 2, 0, 1]
# Har point ka cluster number
print(f"Centers: {kmeans.cluster_centers_}")
# Har cluster ka center point2. Elbow Method � Sahi K dhoondna
K-Means mein sabse bada sawaal hai � K kitna rakhein✓ Elbow method se tum inertia (within-cluster sum of squares) nikalte ho alag-alag K ke liye. Jab inertia drop hona band ho jaaye � woh point "elbow" hai. Wohi best K hai.
# Elbow method � best K dhoondna
inertias = []
K_range = range(1, 8)
for k in K_range:
km = KMeans(n_clusters=k, random_state=42)
km.fit(X_scaled)
inertias.append(km.inertia_)
print(f"K values: {list(K_range)}")
print(f"Inertias: {[round(i, 2) for i in inertias]}")
# Output:
# K: [1, 2, 3, 4, 5, 6, 7]
# Inertia: [16.0, 6.5, 2.8, 2.1, 1.5, 1.1, 0.8]
# Elbow point = 3 (yahan se drop slow hota hai)
# Visualize karo (matplotlib)
import matplotlib.pyplot as plt
plt.plot(K_range, inertias, 'bo-')
plt.xlabel('K (number of clusters)')
plt.ylabel('Inertia')
plt.title('Elbow Method')
plt.show()3. Hierarchical � Tree structure
Hierarchical clustering mein tumhe K dena nahi padta. Yeh ek tree (dendrogram) banata hai jisse tum visualize kar sakte ho kitne clusters chahiye. Agglomerative approach mein har point apna cluster hai, phir closest clusters merge hote hain.
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
# Hierarchical clustering
linkage_matrix = linkage(X_scaled, method='ward')
# Dendrogram visualize karo
plt.figure(figsize=(10, 5))
dendrogram(linkage_matrix, labels=range(len(X_scaled)))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Data Points')
plt.ylabel('Distance')
plt.show()
# Agglomerative clustering � 3 clusters
agg = AgglomerativeClustering(n_clusters=3)
labels = agg.fit_predict(X_scaled)
print(f"Labels: {labels}")4. DBSCAN � Density-based
DBSCAN density-based clustering hai. Jagah jahan points dense hain woh cluster banega. Noise points (outliers) automatically identify hote hain. Irregular shapes ke clusters ban sakta hai � jaise crescent moon shape.
from sklearn.cluster import DBSCAN
# DBSCAN � density-based clustering
# eps = maximum distance between two points to be neighbors
# min_samples = minimum points to form a dense region
dbscan = DBSCAN(eps=0.8, min_samples=2)
labels = dbscan.fit_predict(X_scaled)
print(f"Labels: {labels}")
# -1 means noise point (outlier)
# 0, 1, 2 etc. are cluster numbers
# DBSCAN ke fayde:
# 1. K dena nahi padta
# 2. Irregular shapes handle karta hai
# 3. Outliers automatically detect hote hain
# 4. Real-world data mein bahut usefulTry it: Clustering practice karo
Neeche ka editor Python jaisa hai. Yahan clustering code likho aur "Run Python" dabao. Screen par output dikhenge � yeh browser-based execution hai, real Python chalega.
Quick check
Elbow method kya hai? Batao yeh kyun use hota hai clustering mein.
Elbow method se hum inertia (within-cluster sum of squares) plot karte hain alag-alag K ke liye. Jab inertia drop slow ho jaaye � woh point "elbow" hai, wohi best K hai.
Clustering tips
- Scaling zaroori hai: K-Means distance-based hai, isliye features ki scale same honi chahiye. Agar salary (50000-200000) aur age (20-60) hai, toh salary dominate karega. StandardScaler use karo.
- Elbow method alone mat dekho: Elbow point kabhi clear nahi hota. Silhouette score bhi check karo � yeh batata hai clusters kitne well-separated hain. Silhouette close to 1 = achha cluster.
- DBSCAN for outliers: Agar tumhe outliers detect karne hain toh DBSCAN best hai. -1 label wale points outliers hain. Real-world fraud detection mein bahut useful hai.
- Hierarchical jab K pata nahi: Agar tumhe nahi pata kitne clusters chahiye toh hierarchical clustering karo aur dendrogram se decide karo. K dena nahi padta.
Common Clustering mistakes
- Scaling skip karna: Sabse bada mistake. K-Means distance calculate karta hai � agar features ki scale alag hai toh cluster galat banega. Hamesha pehle scaling karo.
- Random initialization pe trust mat karo: K-Means random centroids se start hota hai. Isliye
n_init=10ya zyada rakho.random_statebhi fix karo results reproduce karne ke liye. - Silhouette score ignore mat karo: Sirf inertia pe mat jao. Silhouette score bhi dekho � yeh batata hai cluster quality. Score close to 0 ya negative hai toh clustering achha nahi hai.
- Over-clustering se bacho: Bahut zyada clusters banana bhi galat hai. Elbow method aur domain knowledge dono use karo. Har cluster meaningful hona chahiye.
Ab Hypothesis Testing par chalo � statistical testing ka complete guide. P-values, significance levels, aur real-world testing examples.