Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Unsupervised Learning
Unsupervised learning finds hidden patterns in data without any labels or correct answers. The AI discovers structure in the data on its own.
How Unsupervised Learning Works
Imagine you have data about 10,000 customers: what they bought, how often they visit, how much they spend. You have no labels.
Unsupervised learning can look at this data and discover natural groups:
- Group A: customers who buy frequently but spend little each time
- Group B: customers who visit rarely but spend a lot each time
- Group C: customers who stopped shopping after one visit
The AI found these groups without anyone telling it they existed. This is unsupervised learning.
Labeled data → supervised, no labels → unsupervised, rewards → RL
Common Unsupervised Learning Uses
- Clustering: group similar customers, documents, or items together automatically
- Anomaly detection: find data points that do not fit any normal pattern (useful for fraud detection)
- Dimensionality reduction: simplify complex data while keeping the important information
- Data exploration: understand the structure of a new dataset before building supervised models
Simple Clustering Example
# Simple illustration of unsupervised clustering
# Finding natural groups in customer purchase data
import random
random.seed(42)
# Customer data: [monthly_visits, avg_spend_per_visit]
# No labels - we do not know the groups in advance
customers = [
[10, 15], # visits often, low spend
[12, 18],
[9, 20],
[1, 120], # visits rarely, high spend
[2, 150],
[1, 100],
[5, 50], # middle group
[6, 55],
[4, 60],
]
# Simple approach: find natural groups by calculating distances
# (Real ML uses algorithms like K-Means)
def find_group(visits, spend):
"""Assign to group based on behavior"""
if visits >= 8:
return "Frequent Low-Spenders"
elif spend >= 100:
return "Rare High-Spenders"
else:
return "Regular Mid-Tier"
print("Unsupervised Clustering Results:")
print("(No labels given - AI discovered these groups)")
print()
groups = {}
for v, s in customers:
group = find_group(v, s)
groups.setdefault(group, []).append((v, s))
for group, members in groups.items():
print(f"Group: {group} ({len(members)} customers)")
for v, s in members:
print(f" Visits/month: {v}, Avg spend: ${s}")
print()Tip
Tip
Unsupervised learning is especially valuable when you have a lot of data but no labels, and you want to understand it better. Many data science projects start with unsupervised exploration to find patterns before deciding what supervised task to build.
Key Takeaways
- Unsupervised learning finds hidden patterns in data without any labels or correct answers.
- Clustering: group similar customers, documents, or items together automatically
- Anomaly detection: find data points that do not fit any normal pattern (useful for fraud detection)
- Dimensionality reduction: simplify complex data while keeping the important information