Module 2: Machine Learning Fundamentals
4. Unsupervised Learning: Finding Patterns without Labels
Description
In this lesson you'll understand Unsupervised Learning: the type of Machine Learning where the system looks for patterns in the data without having correct answers (labels). Unlike supervised learning (where you tell the model "this is spam, this isn't spam"), here you only give it data and the model finds structure on its own (e.g. it groups similar customers, reduces dimensions for visualization, detects anomalies).
This matters because many real-world problems don't have clear labels: you don't know in advance what customer segments exist, or what hidden patterns are in your data. Unsupervised learning lets you explore that data and find structure without labeling everything manually.
What is Unsupervised Learning?
Definition: A type of Machine Learning where the system looks for patterns or structure in data without labels (without predefined correct answers). The goal isn't to predict a correct answer, but to find groupings, reduce dimensions or detect anomalies.
Analogy: It's like giving someone 1,000 photos of animals (without telling them which are cats, dogs, birds) and asking them to group them into similar categories. The person (or the model) doesn't know in advance what categories exist; they have to find them by observing similarities (size, shape, color, etc.).
Basic flow:
Data without labels → Unsupervised learning algorithm → Patterns/Groupings
Concrete example (customer segmentation):
- Data: Information on 10,000 customers (age, purchases, visit frequency, etc.) without labels (you don't know in advance what segments exist).
- Algorithm: For example, K-means (a clustering algorithm).
- Result: The algorithm groups the customers into 4-5 segments based on similarities (e.g. "young customers who buy a lot", "older customers who buy little", etc.).
- Use: Now you can design different marketing strategies for each segment.
Key point: You didn't tell the model "this customer is in segment A, this one in segment B". The model found the segments on its own.
The two main types: Clustering and Dimensionality Reduction
1. Clustering
Definition: Grouping data into clusters based on similarities. The data within a cluster is similar to each other; the data in different clusters is different.
Examples:
- Customer segmentation: Grouping customers by purchase behavior, age, location, etc. Without knowing in advance what segments exist.
- Document grouping: Grouping news articles by topic (sports, politics, technology) without predefined labels.
- Anomaly detection: Grouping normal transactions; the ones that fall outside the main clusters can be fraud.
- Organizing photos: Grouping photos by visual content (landscapes, people, objects) without labeling manually.
Common algorithms: K-means, DBSCAN, Hierarchical Clustering.
Analogy: It's like organizing your room: you group similar things together (books with books, clothes with clothes) without anyone having told you in advance "these are the correct groups". You decide the groups based on similarities you observe.
2. Dimensionality Reduction
Definition: Reducing the number of variables (dimensions) in the data while keeping the most important information. Useful for visualization, data compression, or as preprocessing before another algorithm.
Examples:
- Visualizing complex data: Reducing data from 100 dimensions (100 variables) to 2-3 dimensions in order to plot it and see patterns.
- Image compression: Reducing the size of an image while keeping the most important visual information.
- Preprocessing: Reducing dimensions before training a supervised model (it makes training faster and sometimes improves results).
Common algorithms: PCA (Principal Component Analysis), t-SNE, UMAP.
Analogy: It's like summarizing a 500-page book into 50 pages: you lose detail but keep the main ideas. Dimensionality reduction does the same with data: it reduces variables while keeping the essentials.
Quick comparison: Clustering vs Dimensionality Reduction
| Aspect | Clustering | Dimensionality Reduction |
|---|---|---|
| Goal | Grouping data into similar clusters | Reducing the number of variables (dimensions) |
| Output | Cluster labels (e.g. customer in cluster 1) | Data with fewer dimensions (e.g. 100D → 2D) |
| Example | Segmenting customers into 5 groups | Visualizing data with 100 variables in 2D |
| Common use | Segmentation, organizing data | Visualization, compression, preprocessing |
| Algorithms | K-means, DBSCAN | PCA, t-SNE |
Note: Both are unsupervised learning because they don't require labels. The model looks for patterns on its own.
Examples of Unsupervised Learning in products
1. Recommendations (Netflix, Spotify)
Besides supervised learning (predicting what you'll like based on your history), many recommendation systems use clustering to group similar users or similar content. Then they recommend content that users in your cluster liked.
Example: Spotify groups songs by musical similarities (tempo, genre, instruments) without manually labeling each song. Then it creates automatic playlists (Discover Weekly) based on those clusters.
2. Anomaly Detection (Banking, Security)
Clustering to group normal transactions; the ones that fall outside the main clusters can be fraud, errors or unusual cases that require review.
Example: A bank groups transactions by amount, location, time, etc. If a transaction is very far from the normal clusters (e.g. a $10,000 purchase at 3 AM in another country), the system flags it as suspicious.
3. Content Organization (Google Photos, Pinterest)
Clustering to group photos by visual content (landscapes, people, objects) without you labeling each photo.
Example: Google Photos groups the photos in your library by visual similarity; then it lets you search for "beach" or "mountain" even though you never labeled those photos.
4. Data Compression (image, audio formats)
Dimensionality reduction to reduce file size while keeping perceptible quality.
Example: JPEG uses techniques that reduce information in the image (dimensionality reduction) without you noticing much quality loss.
Why this matters for an AI Engineer
As an AI Engineer, you'll use unsupervised learning in cases like:
-
Exploring data without labels: Before building a supervised system, you sometimes need to understand the structure of your data (what groupings are there? which variables are important?). Clustering and dimensionality reduction help you explore.
-
Embeddings and semantic search: Embeddings (vector representations of text or images) are a form of dimensionality reduction: they convert text or images into vectors of numbers in a reduced-dimension space. Then you use those embeddings for semantic search (RAG) or to find similar content. That's unsupervised learning in action.
-
Anomaly detection: If you build a system that detects fraud, errors or rare cases, clustering helps you define "what's normal" and flag what falls outside the norm.
-
Reducing costs: Dimensionality reduction can reduce the size of the data you pass to a model (e.g. reducing embedding dimensions before storing them in a vector database), which saves on storage and search costs.
Limitations and challenges of Unsupervised Learning
1. No "correct answer" to validate against
Problem: In supervised learning you can measure accuracy (did the model predict correctly?). In unsupervised, there are no labels to compare against; you don't know whether the clusters the model found are "correct" or useful.
Example: If an algorithm groups customers into 5 segments, are those the "correct" segments? There's no correct answer; it depends on whether those segments are useful for your business.
2. Subjective interpretation
Problem: The results (clusters, reduced dimensions) need human interpretation. The model doesn't tell you "this cluster is young customers"; it gives you groupings and you decide what they mean.
Solution: Exploratory analysis after clustering: reviewing which characteristics the data in each cluster shares.
3. Choosing hyperparameters
Problem: You have to decide in advance how many clusters you want (e.g. K in K-means) or how many dimensions to reduce to. If you choose badly, the results aren't useful.
Example: If you ask for 2 clusters when there are actually 5 customer segments, the grouping will be very coarse. If you ask for 50 clusters when there are only 3 segments, you'll have clusters with very little data.
4. It isn't for direct prediction
Problem: Unsupervised learning doesn't predict labels or numbers the way supervised learning does. It only finds structure. If you need to predict "is this email spam?", you need supervised learning, not unsupervised.
Use: Unsupervised is useful for exploring data, not for making specific predictions (although you can combine them: use clustering to segment, and then train a supervised model per segment).
Exercises
Exercise 1: Classify problems
Classify each problem as supervised or unsupervised. Justify in one sentence.
- Grouping customers into segments by purchase behavior (without knowing in advance what segments exist).
- Predicting whether a customer will buy a product (yes/no).
- Reducing a dataset from 100 variables to 3 for visualization.
- Classifying emails as spam or not-spam.
- Detecting unusual transactions that could be fraud (without labeling each transaction as fraud or not).
See solution
- Unsupervised (clustering). There are no labels; the model groups by similarities.
- Supervised (classification). There are labels (yes/no); the model learns to predict.
- Unsupervised (dimensionality reduction). There are no labels; the model reduces dimensions while keeping information.
- Supervised (classification). There are labels (spam/not-spam); the model learns to predict.
- Unsupervised (clustering or anomaly detection). There are no "fraud/not-fraud" labels; the model groups normal transactions and flags the ones outside the norm.
Exercise 2: Identify the type of unsupervised learning
Identify whether each case is clustering or dimensionality reduction. Justify in one sentence.
- Grouping news articles by topic (without predefined labels).
- Reducing a dataset from 50 variables to 2 for plotting.
- Segmenting customers into 4 groups by behavior.
- Compressing an image from 1MB to 200KB while keeping visual quality.
See solution
- Clustering. It groups articles by similarities (topic) without labels.
- Dimensionality reduction. It reduces dimensions (50 → 2) for visualization.
- Clustering. It groups customers into segments by similarities (behavior).
- Dimensionality reduction (or related techniques). It reduces information (size) while keeping the essentials (visual quality).
Exercise 3: Limitations
What's the main advantage and the main disadvantage of unsupervised learning compared with supervised learning? Write 1-2 sentences for each.
See answer guide
Advantage: You don't need to label data manually (it saves time and cost). You can explore data and find patterns you didn't know existed.
Disadvantage: There's no "correct answer" to validate against; the results (clusters, reduced dimensions) need human interpretation. It isn't for direct prediction (e.g. "is this email spam?").
Exercise 4: Embeddings and semantic search
Embeddings (vector representations of text or images) are a form of dimensionality reduction: they convert text into vectors of numbers. Is this supervised or unsupervised learning? Justify in one sentence.
See solution
It depends on the method, but it's often unsupervised or semi-supervised. For example, Word2Vec (an embedding algorithm for words) learns without explicit labels: it finds patterns in the text (which words appear near which other words) and learns vector representations. It doesn't need anyone to label each word with its "meaning"; it learns from co-occurrence.
In the context of RAG (Retrieval-Augmented Generation), you use pre-trained embeddings (e.g. from OpenAI) that were trained with methods that learned from patterns in text, not from explicit "meaning" labels. That's unsupervised or semi-supervised.
Exercise 5: Why it matters for AI Engineering
Why does an AI Engineer need to understand unsupervised learning? Write 2 reasons.
See answer guide
Possible guide:
- Embeddings and RAG: Embeddings (vectors that represent text or images) are trained with unsupervised or semi-supervised methods. When you use embeddings in RAG (semantic search), you're using the result of unsupervised learning.
- Exploring data without labels: Before building a supervised system, you sometimes need to understand what structure your data has (what groupings are there? which variables are important?). Clustering and dimensionality reduction help you explore without labeling everything manually.
Summary
In one sentence: Unsupervised Learning is the type of ML where the model looks for patterns or structure in data without labels (without predefined correct answers).
Key points:
- Two main types: Clustering (grouping similar data) and Dimensionality Reduction (reducing the number of variables while keeping essential information).
- Flow: Data without labels → Algorithm → Patterns/Groupings/Reduced dimensions.
- Examples in products: Customer segmentation, anomaly detection, content organization (Google Photos, Pinterest), data compression.
- Advantage: You don't need to label data manually (it saves time and cost). You can find patterns you didn't know existed.
- Disadvantage: There's no "correct answer" to validate against; the results need human interpretation. It isn't for direct prediction.
- Why it matters for an AI Engineer: Embeddings (RAG, semantic search), exploring data without labels, reducing storage and search costs.
Connection with supervised learning and with the rest of the module
Sometimes you combine supervised and unsupervised:
- Example 1: You use clustering (unsupervised) to segment customers into 5 groups; then you train a supervised model for each group to predict what each customer will buy.
- Example 2: You use dimensionality reduction (PCA) to reduce 100 variables to 20; then you train a supervised model with those 20 variables (faster and sometimes more accurate than with the 100).
In the next lesson you'll see Reinforcement Learning (the third major type of ML: learning by trial and error with rewards). Then, in lesson 06 (Training vs Inference), you'll see that the training/inference distinction applies to supervised, unsupervised and reinforcement: in every case there's a learning phase and a usage phase.
Additional resources
-
Google ML Crash Course: Clustering — Official introduction to clustering with examples. In English.
-
StatQuest: K-means Clustering — A visual video explaining the K-means algorithm. In English. Very clear.
-
Scikit-learn: Clustering — Documentation of clustering algorithms in scikit-learn. In English.
-
StatQuest: PCA (Principal Component Analysis) — A video explaining PCA (dimensionality reduction) visually. In English.
-
Distill.pub: Visualizing High-Dimensional Data — An interactive article about t-SNE (dimensionality reduction for visualization). In English. Very visual.
-
Elements of AI – Unsupervised Learning — A chapter on unsupervised learning with exercises. Available in several languages.