DEV Community

Cover image for A Beginner’s Guide to Unsupervised Learning in Machine Learning
Ephantus Macharia
Ephantus Macharia

Posted on

A Beginner’s Guide to Unsupervised Learning in Machine Learning

What Is Unsupervised Learning?

Unsupervised learning is a type of machine learning where an algorithm learns patterns from data without being given labeled answers.

Imagine you have information about 1,000 customers.

You know things such as:

  • Age
  • Annual income
  • Amount spent
  • Number of purchases

But you don't have a column telling you what type of customer each person is.

There might not be a column like:

Customer Income Spending Customer Type
A 25,000 15,000 Budget
B 60,000 45,000 Regular
C 90,000 80,000 Premium

Instead, you give the data to an algorithm and ask it to discover patterns or groups.

It might discover:

Group 1 → Low income + low spending
Group 2 → Medium income + medium spending
Group 3 → High income + high spending
Enter fullscreen mode Exit fullscreen mode

You didn't tell the algorithm that these groups existed.

It discovered them from the data.

That's the basic idea behind unsupervised learning.


Supervised vs. Unsupervised Learning

One of the easiest ways to understand unsupervised learning is by comparing it with supervised learning.

Supervised Learning

In supervised learning, we have data with known answers.

Input Data → Known Labels → Machine Learning Model
Enter fullscreen mode Exit fullscreen mode

For example:

House Size + Bedrooms → House Price
Enter fullscreen mode Exit fullscreen mode

The model learns from historical examples where the correct answer is already known.

House Size Bedrooms Price
800 sq ft 2 $100,000
1,200 sq ft 3 $160,000
1,800 sq ft 4 $240,000

The model can then predict the price of a new house.


Unsupervised Learning

With unsupervised learning, there is no target label.

Input Data → Algorithm → Hidden Patterns
Enter fullscreen mode Exit fullscreen mode

For example:

Customer Data
      ↓
Machine Learning Algorithm
      ↓
Discover Groups
      ↓
Customer Segments
Enter fullscreen mode Exit fullscreen mode

The key difference is:

Supervised learning learns from known answers, while unsupervised learning searches for patterns without known answers.


How Unsupervised Learning Works

A simple way to visualize the process is:

flowchart LR
    A[Raw Data] --> B[Explore Data]
    B --> C[Choose Features]
    C --> D[Unsupervised Algorithm]
    D --> E[Discover Patterns]
    E --> F[Interpret Results]

The algorithm isn't necessarily trying to predict a specific answer.

Instead, it might be trying to answer questions such as:

  • Which observations are similar?
  • Are there natural groups?
  • Which observations are unusual?
  • Can we represent this dataset using fewer dimensions?

Why Do We Need Unsupervised Learning?

Real-world datasets aren't always neatly labeled.

Sometimes we have thousands or millions of records but don't know what patterns exist inside them.

Unsupervised learning helps us explore these datasets.

Some common applications include:

Customer Segmentation

Businesses can group customers according to:

  • Spending habits
  • Income
  • Purchase frequency
  • Product preferences

This can help businesses create more targeted marketing strategies.

Recommendation Systems

Unsupervised techniques can help identify products, movies, songs, or other items that are similar.

For example:

Customer purchases Product A
            ↓
Find similar customers/products
            ↓
Recommend Products B and C
Enter fullscreen mode Exit fullscreen mode

Anomaly Detection

Unsupervised algorithms can identify observations that look very different from normal behavior.

For example, a bank could analyze transaction behavior and identify unusual transactions.

Data Exploration

Sometimes you don't even know what you're looking for.

Unsupervised learning can help you explore a dataset and discover hidden structures.


Main Types of Unsupervised Learning

There are several important techniques you should understand as a beginner.

The three I recommend learning first are:

  1. Clustering
  2. Dimensionality Reduction
  3. Anomaly Detection

Clustering

Clustering is one of the most common applications of unsupervised learning.

The goal is simple:

Group similar data points together.

Imagine having hundreds of points on a graph.

Before clustering, you might see something like:

        • • •
      • • • •
        • •

                    • • •
                  • • • •
                    • •

   • • •
 • • • •
   • •
Enter fullscreen mode Exit fullscreen mode

The algorithm attempts to identify natural groups within those points.

After clustering:

Cluster A          Cluster B          Cluster C

  • • •              • •               • •
 • • • •            • • •             • • •
  • •                • •               • •
Enter fullscreen mode Exit fullscreen mode

Some popular clustering algorithms include:

  • K-Means
  • Hierarchical Clustering
  • DBSCAN

Dimensionality Reduction

Datasets can contain hundreds or even thousands of features.

Imagine having:

Feature 1
Feature 2
Feature 3
Feature 4
...
Feature 100
Enter fullscreen mode Exit fullscreen mode

Trying to visualize 100 dimensions isn't practical.

Dimensionality reduction attempts to reduce the number of features while preserving important information.

A common technique is Principal Component Analysis (PCA).

flowchart TD
    A[100 Features] --> B[PCA]
    B --> C[Important Components]
    C --> D[2D or 3D Visualization]

Other techniques include:

  • PCA
  • t-SNE
  • UMAP

These techniques are especially useful when exploring complex datasets.


Anomaly Detection

Anomaly detection focuses on finding observations that don't look like the rest of the data.

Imagine most transactions from a customer look like this:

$10
$25
$40
$15
$30
Enter fullscreen mode Exit fullscreen mode

Then suddenly:

$5,000
Enter fullscreen mode Exit fullscreen mode

That transaction might be considered unusual.

Anomaly detection algorithms can help identify observations like this.

Some algorithms include:

  • Isolation Forest
  • Local Outlier Factor
  • One-Class SVM

A Practical Example: Customer Segmentation

Let's make things more practical.

Suppose a company has customer data containing:

  • Annual income
  • Annual spending

Our goal is to discover whether customers naturally form different groups.

We can use K-Means clustering.

Our dataset might look something like this:

Customer Annual Income Annual Spending
A 25,000 15,000
B 30,000 20,000
C 35,000 18,000
D 60,000 45,000
E 65,000 50,000
F 80,000 75,000

We can visualize this data using a scatter plot.

The x-axis represents income, while the y-axis represents spending.

Once K-Means is applied, the algorithm may discover three different groups.


How Does K-Means Work?

K-Means is easier to understand when broken down into steps.

Suppose we want to create 3 clusters.

We tell the algorithm:

n_clusters = 3
Enter fullscreen mode Exit fullscreen mode

The algorithm then roughly follows this process.

Step 1 Choose K

We decide how many clusters we want.

K = 3
Enter fullscreen mode Exit fullscreen mode

Step 2 Initialize Centroids

The algorithm selects initial cluster centers called centroids.

          X
       Centroid
Enter fullscreen mode Exit fullscreen mode

Step 3 Assign Data Points

Each data point is assigned to the closest centroid.

Data Point → Closest Centroid
Enter fullscreen mode Exit fullscreen mode

Step 4 Recalculate Centroids

The algorithm calculates new centers based on the assigned data points.

Step 5 Repeat

The assignment and centroid calculation process continues until the clusters stabilize.

The basic process looks like this:

flowchart TD
    A[Choose K] --> B[Initialize Centroids]
    B --> C[Assign Points to Closest Centroid]
    C --> D[Recalculate Centroids]
    D --> E{Clusters Stable?}
    E -->|No| C
    E -->|Yes| F[Final Clusters]

Let's Implement K-Means in Python

Now let's put the concept into practice.

We'll use:

  • Pandas
  • Matplotlib
  • Scikit-learn

Install them with:

pip install pandas matplotlib scikit-learn
Enter fullscreen mode Exit fullscreen mode

Then import the libraries:

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
Enter fullscreen mode Exit fullscreen mode

Load the Dataset

Let's assume we have a file called:

customer_data.csv
Enter fullscreen mode Exit fullscreen mode

We can load it using Pandas:

data = pd.read_csv("customer_data.csv")

print(data.head())
Enter fullscreen mode Exit fullscreen mode

Select Our Features

For this example, we'll use:

  • Annual Income
  • Annual Spending
X = data[
    ["Annual Income", "Annual Spending"]
]
Enter fullscreen mode Exit fullscreen mode

These are the features we want the algorithm to use when creating clusters.


Create the K-Means Model

Let's create three clusters:

kmeans = KMeans(
    n_clusters=3,
    random_state=42
)
Enter fullscreen mode Exit fullscreen mode

Then train the model:

kmeans.fit(X)
Enter fullscreen mode Exit fullscreen mode

We can obtain the cluster assigned to each customer:

data["Cluster"] = kmeans.labels_
Enter fullscreen mode Exit fullscreen mode

Now each customer has a cluster number.

For example:

Customer Income Spending Cluster
A 25,000 15,000 0
B 30,000 20,000 0
C 60,000 45,000 1
D 65,000 50,000 1
E 80,000 75,000 2

Remember:

Cluster 0, 1, and 2 don't automatically mean "bad", "average", and "good".

They are simply labels assigned by the algorithm.

We need to interpret what each cluster represents.


Visualizing Our Clusters

Visualization is one of the most useful parts of machine learning because it allows us to see what the algorithm discovered.

We can create a scatter plot:

plt.scatter(
    X["Annual Income"],
    X["Annual Spending"],
    c=data["Cluster"]
)

plt.xlabel("Annual Income")
plt.ylabel("Annual Spending")

plt.title("Customer Segmentation Using K-Means")

plt.show()
Enter fullscreen mode Exit fullscreen mode

The resulting visualization allows us to see how customers have been grouped.

A simplified representation looks like:

Annual Spending
       ↑
       |
  ● ● ●              Cluster 3
 ● ● ● ●
       |
              ● ● ●
            ● ● ●        Cluster 2
       |
 ● ● ●
● ● ●                  Cluster 1
       |
       +------------------------→ Annual Income
Enter fullscreen mode Exit fullscreen mode

The visualization makes the concept much easier to understand:

Customers that are closer together tend to belong to the same cluster.


Understanding the Distance

K-Means relies heavily on the concept of distance.

A common distance measure is Euclidean distance.

For two points:

A = (x₁, y₁)
B = (x₂, y₂)
Enter fullscreen mode Exit fullscreen mode

The Euclidean distance is:

distance = √((x₂ - x₁)² + (y₂ - y₁)²)
Enter fullscreen mode Exit fullscreen mode

You don't need to memorize the mathematics immediately.

The important idea is:

K-Means uses distance to determine which data points are closest to each centroid.


How Do We Choose the Number of Clusters?

This is one of the most important questions when working with K-Means.

We can't always simply guess:

n_clusters=3
Enter fullscreen mode Exit fullscreen mode

So how do we choose K?

One popular approach is the Elbow Method.


The Elbow Method

The basic idea is to test different values of K and measure how well the clusters fit the data.

We can calculate the inertia for different values of K:

inertia = []

for k in range(1, 11):

    model = KMeans(
        n_clusters=k,
        random_state=42
    )

    model.fit(X)

    inertia.append(model.inertia_)
Enter fullscreen mode Exit fullscreen mode

Then visualize the results:

plt.plot(
    range(1, 11),
    inertia,
    marker="o"
)

plt.xlabel("Number of Clusters")
plt.ylabel("Inertia")

plt.title("Elbow Method")

plt.show()
Enter fullscreen mode Exit fullscreen mode

(https://miro.medium.com/0*aY163H0kOrBO46S-.png)

You may see a graph where the improvement becomes much smaller after a certain point.

That bend is called the elbow.

The elbow can help us choose a reasonable number of clusters.

However, it isn't a magic rule.

Domain knowledge and other evaluation techniques can also be important.

Important Things to Understand

K-Means is powerful, but it isn't perfect.

There are several things beginners should be aware of.

1. You Need to Choose K

K-Means requires you to specify the number of clusters.

KMeans(n_clusters=3)
Enter fullscreen mode Exit fullscreen mode

Choosing the wrong K can produce misleading groups.


2 Feature Scaling Can Matter

Suppose one feature ranges from:

1–10
Enter fullscreen mode Exit fullscreen mode

while another ranges from:

1–1,000,000
Enter fullscreen mode Exit fullscreen mode

The larger-scale feature can dominate distance calculations.

This is why feature scaling is often important.

For example:

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)
Enter fullscreen mode Exit fullscreen mode

Then:

kmeans.fit(X_scaled)
Enter fullscreen mode Exit fullscreen mode

3. Clusters Need Interpretation

The algorithm gives you:

Cluster 0
Cluster 1
Cluster 2
Enter fullscreen mode Exit fullscreen mode

But the numbers themselves don't explain what the groups mean.

As a data analyst, you still need to investigate the characteristics of each cluster.

For example:

Cluster 0
→ Lower income
→ Lower spending

Cluster 1
→ Medium income
→ Medium spending

Cluster 2
→ Higher income
→ Higher spending
Enter fullscreen mode Exit fullscreen mode

This is where data analysis and domain knowledge become extremely important.

Real-World Applications

Unsupervised learning appears in many different industries.

Banking

  • Fraud detection
  • Customer segmentation
  • Transaction analysis

Retail

  • Customer segmentation
  • Product grouping
  • Shopping behavior analysis

Entertainment

  • Music recommendations
  • Movie recommendations
  • User behavior analysis

Healthcare

  • Patient grouping
  • Identifying unusual observations
  • Exploring medical datasets

Cybersecurity

  • Detecting unusual network activity
  • Identifying suspicious behavior
  • Finding unusual patterns

Beginner Project Idea

If you're learning unsupervised learning, one of the best projects you can build is a:

Customer Segmentation Project

Your workflow could look like this:

flowchart LR
    A[Find Dataset] --> B[Clean Data]
    B --> C[Explore Data]
    C --> D[Select Features]
    D --> E[Scale Features]
    E --> F[Apply K-Means]
    F --> G[Choose K]
    G --> H[Visualize Clusters]
    H --> I[Interpret Results]
    I --> J[Write Business Insights]

You could investigate questions such as:

  • Who are our high-value customers?
  • Which customers have low spending?
  • Are there natural groups of customers?
  • Which customer segment should receive a marketing campaign?
  • How does income relate to spending?

This transforms machine learning from just writing Python code into actual data-driven problem solving.

Conclusion

Unsupervised learning is a powerful part of machine learning because it allows us to discover patterns, relationships, and structures hidden within data even when we don't have predefined labels.
The most important lesson is that machine learning isn't always about predicting a known outcome. Sometimes, the goal is simply to understand the data and discover what it is telling us.

Top comments (0)