In the world of machine learning, not all problems come with clear instructions. Sometimes, we don't know the answer in advance we just have raw data. This is where Unsupervised Learning comes in.
While Supervised Learning teaches a model using labeled data (like predicting prices or classifying emails as spam), Unsupervised Learning lets algorithms explore and uncover structure in unlabeled datasets much like solving a puzzle without knowing the final picture.
What is Unsupervised Learning?
Unsupervised Learning is a type of machine learning where the model is trained without labeled output. The goal is to find patterns, groupings, or meaningful insights from the data based purely on its internal structure.
Think of unsupervised learning like walking into a library with no catalog system. Books are scattered around, and you have no idea what genres or categories exist. You start grouping them based on clues like titles, covers, or page numbers. Eventually, you might sort them into categories like fiction, history, or science even though no one told you those categories existed.
That’s exactly what an unsupervised learning algorithm does.
Main Techniques in Unsupervised Learning
1. Clustering
Clustering is about grouping similar data points together.
A) KMeans Clustering
Idea: Divide data into K groups based on similarity.
How it works: The algorithm randomly picks K "centroids", assigns each data point to the closest one, and adjusts centroids until things stabilize.
Real-world use:
Customer Segmentation: In retail or hospitality, we can use clustering to group customers based on age, income, and spending habits.
Example:Here's a step-by-step visual demonstration of applying K-Means clustering to mall customer data:
Understanding the Data
Typical mall customer data includes: Customer ID, Age, Annual Income(k$), Spending Score (1-100).
A simple ASCII-style diagram to help you visualize how KMeans clustering works:
These * symbols represent data points like customers, documents, or patterns, but we don’t yet know how they’re grouped.
C1, C2, C3 are randomly placed centroids, each representing a starting guess for a cluster center.
Each point now belongs to the nearest centroid forming three clusters.
B) Hierarchical Clustering
Idea: Build a tree of clusters (like a family tree) by progressively merging or splitting groups.
Visual Aid: Dendrograms show the merging process.
Use case: Better for visualizing how clusters are formed, especially when we don’t know the number of clusters in advance.
Below is a python code using the mall customer data;
from scipy.cluster.hierarchy import dendrogram, linkage
linkage_matrix = linkage(df[['Age', 'Spending Score (1-100)']], method='ward')
dendrogram(linkage_matrix)
2. Dimensionality Reduction
When working with machine learning models, datasets with too many features can cause issues like slow computation and overfitting. Dimensionality reduction helps to reduce the number of features while retaining key information. Techniques like principal component analysis (PCA), singular value decomposition (SVD) and linear discriminant analysis (LDA) convert data into a lower-dimensional space while preserving important details.
A) Principal Component Analysis (PCA)
Idea: Transform the data into fewer dimensions while preserving variance.
How it helps:
- Improves speed and efficiency.
- Useful for visualizing high-dimensional data in 2D or 3D.
- Helps in noise reduction and pattern recognition.
Why It Matters
You don’t always have labels: In real-world business problems, labels (like churned customers or fraud cases) are expensive or missing.
Find hidden insights: Unsupervised models can uncover segments, anomalies, or structures you didn’t even know existed.
Better targeting: Marketers use clustering to tailor campaigns. Hotels can design loyalty offers based on cluster behavior. Security teams can identify unusual access patterns.
Analogy: Sorting Socks Blindfolded
Imagine reaching into a laundry basket blindfolded. You don’t know the colors, but you try sorting socks by feel — thick vs. thin, long vs. short. Eventually, you form groups. That’s unsupervised learning — no labels, just internal patterns.
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
kmeans = KMeans(n_clusters=5)
df['Cluster'] = kmeans.fit_predict(df[['Annual Income (k$)', 'Spending Score (1-100)']])
# Plot clusters
plt.figure(figsize=(8, 5))
plt.scatter(df['Annual Income (k$)'], df['Spending Score (1-100)'], c=df['Cluster'], cmap='rainbow')
plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score')
plt.title('Customer Segmentation using KMeans')
plt.show()
This gives a powerful visualization — showing customer groups based on how they spend and what they earn.
Conclusion
Unsupervised learning may seem like data alchemy at first — turning unlabeled, messy data into gold. But with tools like KMeans, Hierarchical Clustering, and PCA, we can find meaning in the chaos. Whether you're segmenting customers, detecting fraud, or simplifying data — these techniques are vital for modern data-driven decision making.
Top comments (0)