Unsupervised Learning Using Python

Learn unsupervised machine learning with Python through practical examples of clustering, customer segmentation, product grouping, anomaly detection, dimensionality reduction, and real-world business data analysis.

What is Unsupervised Learning?

Unsupervised Learning is a type of Machine Learning where the algorithm works with data that does not have a predefined target or label. Instead of learning from known answers, the algorithm searches for useful patterns, groups, relationships, or unusual observations within the data.

Raw Data
→
Features
→
Find Patterns
→
Build Groups
→
Analyze
→
Insights

No Target Column

The dataset does not contain a predefined answer that the model must learn to predict.

Find Hidden Patterns

Algorithms search for structures, similarities, relationships, or unusual observations.

Clustering

Similar records are grouped together based on selected features.

Dimensionality Reduction

Large numbers of features can be transformed into fewer dimensions while attempting to preserve useful structure.

1. How Unsupervised Learning Works

In supervised learning, the model learns from labeled examples. In unsupervised learning, the algorithm explores the structure of the available data.

Unsupervised Learning Flow
Raw Data
   ↓
Feature Selection
   ↓
Data Preparation
   ↓
Unsupervised Algorithm
   ↓
Patterns / Groups
   ↓
Business or Scientific Analysis
Example: An online store may have thousands of customer records but no predefined customer segment. Clustering can identify groups such as frequent buyers, occasional buyers, and high-value customers.
2. Supervised vs Unsupervised Learning
Feature Supervised Learning Unsupervised Learning
Target Known target No predefined target
Learning From labeled examples From data structure
Common Task Prediction Pattern discovery
Example Predict house price Group customers
Common Algorithms Linear Regression, Random Forest K-Means, DBSCAN, PCA
3. Main Types of Unsupervised Learning

Clustering

Groups similar records together. Example: customer segmentation.

Dimensionality Reduction

Reduces the number of features while retaining useful structure.

Anomaly Detection

Identifies observations that are unusual compared with the rest of the data.

4. Installing Required Libraries
Terminal
pip install pandas numpy scikit-learn matplotlib seaborn
5. Importing Libraries
Python
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
6. Creating a Customer Dataset

Consider a business with customer information such as annual income and spending score.

Python
data = {
    "CustomerID": [
        101, 102, 103, 104, 105,
        106, 107, 108, 109, 110
    ],

    "AnnualIncome": [
        25000, 28000, 30000, 45000, 50000,
        55000, 70000, 75000, 90000, 95000
    ],

    "SpendingScore": [
        20, 25, 30, 45, 50,
        55, 70, 75, 90, 95
    ]
}

df = pd.DataFrame(data)

print(df)
7. Selecting Features

CustomerID identifies the customer but does not represent a useful behavioral feature for this clustering example.

Python
X = df[
    ["AnnualIncome", "SpendingScore"]
]

print(X)
Features: AnnualIncome and SpendingScore are used to find groups of customers with similar characteristics.
8. Feature Scaling

Scaling is often useful when features have substantially different numerical ranges.

Python
scaler = StandardScaler()

X_scaled = scaler.fit_transform(
    X
)

print(X_scaled)
Important: Scaling changes the numerical representation of the features. Always choose preprocessing based on the algorithm and the characteristics of the data.
9. K-Means Clustering

K-Means is one of the most commonly taught clustering algorithms. It attempts to divide observations into a specified number of clusters.

Python
kmeans = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

kmeans.fit(X_scaled)
10. Getting Cluster Labels
Python
df["Cluster"] = kmeans.labels_

print(df)
Result: Each customer receives a cluster number. The numbers are identifiers for the groups; they do not inherently mean "better", "worse", or any particular business category.
11. Understanding the Clusters

After clustering, examine the characteristics of each group instead of assigning meanings automatically.

Python
cluster_summary = df.groupby(
    "Cluster"
)[
    ["AnnualIncome", "SpendingScore"]
].mean()

print(cluster_summary)
Business interpretation: You might discover groups with relatively low income, high income, high spending, or low spending. The actual interpretation should come from the data and business context.
12. Finding the Number of Clusters

K-Means requires a value for the number of clusters. One common exploratory technique is the Elbow Method.

Python
inertia = []

for k in range(1, 8):

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

    model.fit(X_scaled)

    inertia.append(
        model.inertia_
    )

print(inertia)

Plotting the inertia values can help identify a point where adding more clusters produces diminishing improvements.

13. Elbow Method Visualization
Python
plt.plot(
    range(1, 8),
    inertia,
    marker="o"
)

plt.xlabel("Number of Clusters")
plt.ylabel("Inertia")
plt.title("Elbow Method")

plt.show()
Remember: The Elbow Method is a heuristic. The chosen number of clusters should also make sense for the data and the intended use.
14. Visualizing Customer Clusters
Python
plt.scatter(
    df["AnnualIncome"],
    df["SpendingScore"],
    c=df["Cluster"]
)

plt.xlabel("Annual Income")
plt.ylabel("Spending Score")
plt.title("Customer Segments")

plt.show()
15. K-Means Centroids

Each K-Means cluster has a centroid representing the center of the cluster in feature space.

Python
print(
    kmeans.cluster_centers_
)

When clustering was performed on standardized features, the centroid values are expressed in the standardized feature space.

16. Real-Time Example: Customer Segmentation

An e-commerce company wants to understand its customers without having predefined customer categories.

Business Workflow
Customer Data
      ↓
Income + Spending + Visits
      ↓
Data Scaling
      ↓
K-Means Clustering
      ↓
Customer Groups
      ↓
Analyze Group Behavior
      ↓
Create Business Strategies

Group A

Customers with relatively lower spending and lower purchasing activity.

Group B

Customers with moderate income and spending.

Group C

Customers with relatively higher spending activity.

These group descriptions are examples of how a business might interpret clusters. They should be validated against the actual data before being used for decisions.
17. Real-Time Example: Product Grouping

A retailer can group products according to price, sales volume, customer rating, and inventory movement.

Python
products = pd.DataFrame({
    "Price": [
        100, 120, 150, 500,
        550, 600, 1000, 1100
    ],

    "Sales": [
        500, 450, 420, 250,
        230, 210, 80, 70
    ],

    "Rating": [
        3.8, 4.0, 4.1, 4.2,
        4.4, 4.5, 4.7, 4.8
    ]
})

features = products[
    ["Price", "Sales", "Rating"]
]

scaler = StandardScaler()

scaled_products = scaler.fit_transform(
    features
)

product_model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

products["Cluster"] = product_model.fit_predict(
    scaled_products
)

print(products)
18. Real-Time Example: Store Location Analysis

A company can use geographic coordinates and store characteristics to identify groups of similar locations.

Python
locations = pd.DataFrame({
    "Latitude": [
        18.52, 18.53, 18.54,
        18.60, 18.61, 18.62,
        19.00, 19.01, 19.02
    ],

    "Longitude": [
        73.85, 73.86, 73.87,
        73.80, 73.81, 73.82,
        73.10, 73.11, 73.12
    ]
})

location_model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

locations["Cluster"] = location_model.fit_predict(
    locations[
        ["Latitude", "Longitude"]
    ]
)

print(locations)
Important: Geographic clustering should account for the coordinate system, distance assumptions, and the geographic scale involved. This simple example is for learning purposes.
19. Hierarchical Clustering

Hierarchical clustering builds a hierarchy of groups and can be visualized as a dendrogram.

Python
from scipy.cluster.hierarchy import (
    dendrogram,
    linkage
)

Z = linkage(
    X_scaled,
    method="ward"
)

dendrogram(Z)

plt.title(
    "Hierarchical Clustering Dendrogram"
)

plt.xlabel("Samples")
plt.ylabel("Distance")

plt.show()
20. Agglomerative Clustering

Agglomerative clustering starts with individual observations and progressively merges similar groups.

Python
from sklearn.cluster import AgglomerativeClustering

model = AgglomerativeClustering(
    n_clusters=3
)

labels = model.fit_predict(
    X_scaled
)

df["HierarchicalCluster"] = labels

print(df)
21. DBSCAN Clustering

DBSCAN groups observations based on density and can identify observations that do not belong to dense groups.

Python
from sklearn.cluster import DBSCAN

dbscan = DBSCAN(
    eps=0.5,
    min_samples=3
)

labels = dbscan.fit_predict(
    X_scaled
)

df["DBSCAN_Cluster"] = labels

print(df)
Special value: In many DBSCAN implementations, the label -1 represents observations identified as noise.
22. Real-Time Example: Fraud and Anomaly Detection

Unsupervised techniques can help identify transactions or observations that look unusual compared with normal patterns.

Workflow
Transaction Data
      ↓
Amount + Frequency + Time
      ↓
Find Normal Patterns
      ↓
Identify Unusual Records
      ↓
Investigate Potential Anomalies
Important: An anomaly is not automatically fraud. Unusual observations require investigation and appropriate domain rules.
23. Isolation Forest

Isolation Forest is commonly used for anomaly detection. It attempts to isolate unusual observations more quickly than typical observations.

Python
from sklearn.ensemble import IsolationForest

transactions = pd.DataFrame({
    "Amount": [
        100, 120, 90, 110,
        130, 125, 105, 115,
        5000, 95
    ],

    "Frequency": [
        2, 3, 2, 4,
        3, 2, 3, 4,
        1, 2
    ]
})

model = IsolationForest(
    contamination=0.1,
    random_state=42
)

transactions["Anomaly"] = model.fit_predict(
    transactions[
        ["Amount", "Frequency"]
    ]
)

print(transactions)
Typical interpretation: Isolation Forest commonly uses 1 for an observation considered normal and -1 for one considered anomalous.
24. Principal Component Analysis

Principal Component Analysis, or PCA, transforms a dataset into a smaller number of components that capture important variation in the data.

Python
from sklearn.decomposition import PCA

pca = PCA(
    n_components=2
)

X_pca = pca.fit_transform(
    X_scaled
)

print(X_pca)
25. Visualizing PCA Results
Python
plt.scatter(
    X_pca[:, 0],
    X_pca[:, 1]
)

plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
plt.title("PCA Visualization")

plt.show()
PCA can be especially useful when a dataset contains many features and you want a lower-dimensional representation for visualization or downstream analysis.
26. Explained Variance

PCA provides explained-variance information that indicates how much variation is captured by each principal component.

Python
print(
    pca.explained_variance_ratio_
)

print(
    "Total:",
    pca.explained_variance_ratio_.sum()
)
27. Real-Time Example: Customer Data with PCA

Suppose an e-commerce dataset contains many customer behavior features.

Python
customers = pd.DataFrame({
    "Visits": [5, 8, 12, 20, 25],
    "Orders": [1, 2, 3, 7, 9],
    "Returns": [0, 1, 1, 2, 2],
    "Reviews": [1, 2, 4, 8, 10],
    "SupportTickets": [4, 3, 3, 1, 1]
})

scaler = StandardScaler()

scaled = scaler.fit_transform(
    customers
)

pca = PCA(
    n_components=2
)

reduced = pca.fit_transform(
    scaled
)

pca_df = pd.DataFrame(
    reduced,
    columns=[
        "PC1",
        "PC2"
    ]
)

print(pca_df)
28. Real-Time Example: Employee Analysis

An organization can explore employee data using features such as experience, training hours, project count, and performance-related measurements.

Python
employees = pd.DataFrame({
    "Experience": [
        1, 2, 3, 5, 7,
        8, 10, 12
    ],

    "TrainingHours": [
        10, 15, 20, 30,
        35, 40, 50, 55
    ],

    "Projects": [
        1, 2, 2, 4,
        5, 6, 8, 10
    ]
})

scaler = StandardScaler()

employee_scaled = scaler.fit_transform(
    employees
)

model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

employees["Group"] = model.fit_predict(
    employee_scaled
)

print(employees)
The resulting groups could be investigated to understand different employee profiles. The cluster labels themselves have no inherent organizational meaning.
29. Real-Time Example: Inventory Analysis

A retailer can group products according to demand, inventory levels, and sales frequency.

Python
inventory = pd.DataFrame({
    "MonthlySales": [
        100, 120, 150, 300,
        350, 400, 50, 60
    ],

    "StockLevel": [
        500, 450, 400, 300,
        280, 250, 700, 650
    ],

    "Orders": [
        20, 25, 30, 60,
        70, 80, 10, 12
    ]
})

features = inventory[
    [
        "MonthlySales",
        "StockLevel",
        "Orders"
    ]
]

scaler = StandardScaler()

scaled = scaler.fit_transform(
    features
)

model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

inventory["Cluster"] = model.fit_predict(
    scaled
)

print(inventory)
30. Silhouette Score

The Silhouette Score is one measure that can help evaluate how well observations fit within their assigned clusters.

Python
from sklearn.metrics import silhouette_score

score = silhouette_score(
    X_scaled,
    kmeans.labels_
)

print(
    "Silhouette Score:",
    score
)
Scores should be interpreted in the context of the dataset, feature representation, distance measure, and clustering objective rather than treated as a universal quality rating.
31. Choosing a Clustering Algorithm
Algorithm Useful When Important Consideration
K-Means Compact groups and known cluster count Requires choosing k
Hierarchical Exploring nested group structures Can be computationally expensive
DBSCAN Density-based groups and possible noise Sensitive to parameter choices
Gaussian Mixture Soft or probabilistic cluster membership Distribution assumptions matter
32. Gaussian Mixture Model

Gaussian Mixture Models represent data as a mixture of probability distributions and can provide soft cluster membership.

Python
from sklearn.mixture import GaussianMixture

gmm = GaussianMixture(
    n_components=3,
    random_state=42
)

gmm.fit(X_scaled)

labels = gmm.predict(
    X_scaled
)

df["GMM_Cluster"] = labels

print(df)
33. Finding Similar Customers

Unsupervised methods can be used to discover customer groups before building more targeted business analyses.

Python
customer_features = df[
    ["AnnualIncome", "SpendingScore"]
]

scaler = StandardScaler()

scaled_customers = scaler.fit_transform(
    customer_features
)

model = KMeans(
    n_clusters=4,
    random_state=42,
    n_init=10
)

df["CustomerGroup"] = model.fit_predict(
    scaled_customers
)

summary = df.groupby(
    "CustomerGroup"
)[
    ["AnnualIncome", "SpendingScore"]
].mean()

print(summary)
34. Real-Time Example: Website Visitor Segmentation

A website can group visitors based on browsing behavior such as session duration, pages viewed, and visits per month.

Python
visitors = pd.DataFrame({
    "SessionMinutes": [
        2, 3, 5, 8,
        15, 18, 20, 25,
        30, 35
    ],

    "PagesViewed": [
        2, 3, 4, 6,
        10, 12, 15, 18,
        22, 25
    ],

    "MonthlyVisits": [
        1, 2, 3, 5,
        8, 9, 10, 12,
        15, 18
    ]
})

scaler = StandardScaler()

scaled = scaler.fit_transform(
    visitors
)

model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

visitors["Segment"] = model.fit_predict(
    scaled
)

print(visitors)
35. Real-Time Example: News Article Grouping

Articles can be represented using text features and grouped according to similarities in their content.

Workflow
News Articles
      ↓
Text Cleaning
      ↓
TF-IDF Features
      ↓
Clustering
      ↓
Similar Article Groups
      ↓
Topic Exploration
36. Text Clustering with TF-IDF
Python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans

documents = [
    "Python programming and software development",
    "Machine learning and artificial intelligence",
    "Football match and sports results",
    "Cricket tournament and sports news",
    "Python data analysis with pandas",
    "Artificial intelligence model training"
]

vectorizer = TfidfVectorizer()

X = vectorizer.fit_transform(
    documents
)

model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

labels = model.fit_predict(X)

for document, label in zip(
    documents,
    labels
):
    print(label, ":", document)
37. Market Basket Analysis Concept

Market basket analysis looks for relationships between products that customers purchase together. Association-rule mining is commonly used for this type of task.

Concept
Customer Transactions
        ↓
Products Purchased Together
        ↓
Find Frequent Itemsets
        ↓
Discover Associations
        ↓
Business Analysis
Association-rule mining is related to unsupervised pattern discovery, although it is conceptually different from distance-based clustering.
38. Unsupervised Learning with Missing Values

Many unsupervised algorithms do not accept missing values directly. Missing data often needs to be handled before clustering or dimensionality reduction.

Python
data = pd.DataFrame({
    "Income": [
        25000,
        30000,
        None,
        50000,
        70000
    ],

    "Spending": [
        20,
        30,
        40,
        None,
        80
    ]
})

data["Income"] = data["Income"].fillna(
    data["Income"].median()
)

data["Spending"] = data["Spending"].fillna(
    data["Spending"].median()
)

print(data)
39. Complete Real-Time Customer Segmentation Project

This example combines data creation, feature selection, scaling, clustering, cluster summaries, and a visualization.

Complete Python Example
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

# --------------------------------
# 1. Create customer data
# --------------------------------

data = {
    "CustomerID": range(101, 113),

    "AnnualIncome": [
        22000, 25000, 28000, 30000,
        45000, 48000, 52000, 56000,
        70000, 76000, 85000, 95000
    ],

    "SpendingScore": [
        15, 20, 25, 30,
        40, 45, 50, 55,
        70, 75, 85, 95
    ],

    "MonthlyVisits": [
        2, 3, 3, 4,
        6, 7, 7, 8,
        10, 11, 13, 15
    ]
}

df = pd.DataFrame(data)

# --------------------------------
# 2. Select features
# --------------------------------

features = df[
    [
        "AnnualIncome",
        "SpendingScore",
        "MonthlyVisits"
    ]
]

# --------------------------------
# 3. Scale features
# --------------------------------

scaler = StandardScaler()

X_scaled = scaler.fit_transform(
    features
)

# --------------------------------
# 4. Train K-Means
# --------------------------------

model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

df["Cluster"] = model.fit_predict(
    X_scaled
)

# --------------------------------
# 5. Analyze clusters
# --------------------------------

summary = df.groupby(
    "Cluster"
)[
    [
        "AnnualIncome",
        "SpendingScore",
        "MonthlyVisits"
    ]
].mean()

print("Cluster Summary:")
print(summary)

# --------------------------------
# 6. Display customer groups
# --------------------------------

print("\nCustomers:")
print(df)

# --------------------------------
# 7. Visualize two features
# --------------------------------

plt.scatter(
    df["AnnualIncome"],
    df["SpendingScore"],
    c=df["Cluster"]
)

plt.xlabel("Annual Income")
plt.ylabel("Spending Score")
plt.title("Customer Segmentation")

plt.show()
40. Dimensionality Reduction Workflow
Many Features
→
Scale Data
→
PCA
→
Fewer Dimensions
→
Visualize

Dimensionality reduction can make complex datasets easier to visualize and can sometimes reduce computational burden. However, reducing dimensions may also remove information.

41. Clustering Workflow for a Real Project
  1. Understand the business or research question.
  2. Collect relevant unlabeled data.
  3. Inspect and clean the dataset.
  4. Select meaningful features.
  5. Handle missing values appropriately.
  6. Scale or transform features when appropriate.
  7. Try suitable clustering algorithms.
  8. Evaluate cluster structure using appropriate measures.
  9. Profile and interpret the resulting groups.
  10. Validate whether the groups are useful for the intended purpose.
42. Common Unsupervised Learning Applications

Customer Segmentation

Discover groups of customers with similar behavior.

Fraud Analysis

Identify unusual transaction patterns for further investigation.

Product Grouping

Discover similar products using sales or product characteristics.

Document Clustering

Group documents with similar text content.

Image Analysis

Discover visual patterns or reduce image feature dimensions.

Anomaly Detection

Identify observations that differ significantly from common patterns.

43. Common Problems with Clustering
  • Choosing an unsuitable number of clusters.
  • Using irrelevant features.
  • Ignoring feature scale.
  • Allowing outliers to dominate the analysis.
  • Assuming clusters automatically represent meaningful business categories.
  • Using a clustering algorithm whose assumptions do not fit the data.
  • Ignoring changes in the data over time.
  • Interpreting cluster labels as ordered scores.
44. Important Unsupervised Learning Metrics
Inertia
Within-cluster squared-distance measure used by K-Means
Silhouette
Measures cluster separation and cohesion
Variance
Important when evaluating PCA components
Distance
Important in many clustering methods
Unsupervised evaluation is different from supervised prediction accuracy because there may be no ground-truth labels against which to compare the groups.
45. Supervised vs Unsupervised Example
Business Question Approach Example
Will this customer purchase? Supervised Learning Classification
How much will this customer spend? Supervised Learning Regression
Which customers behave similarly? Unsupervised Learning Clustering
Which transactions look unusual? Unsupervised Learning Anomaly Detection
Can we visualize 50 features in fewer dimensions? Unsupervised Learning PCA
46. Unsupervised Learning Best Practices
  1. Understand the purpose of discovering groups or patterns.
  2. Use meaningful features.
  3. Clean the data before modeling.
  4. Handle missing values appropriately.
  5. Consider scaling when required by the algorithm.
  6. Compare reasonable algorithm and parameter choices.
  7. Use multiple evaluation approaches when possible.
  8. Profile each cluster after creating it.
  9. Do not assume that every cluster is meaningful.
  10. Validate discovered patterns with domain knowledge.
  11. Monitor whether discovered structures change over time.
  12. Document preprocessing and modeling assumptions.
47. Complete Unsupervised Learning Project Checklist
Step Question
1 What pattern or structure are we trying to discover?
2 Do we have appropriate unlabeled data?
3 Which features are meaningful?
4 Does the data require scaling?
5 Which algorithm fits the data?
6 How will we evaluate the discovered structure?
7 Can the groups or patterns be interpreted?
8 Are the results useful for the intended purpose?
48. Important Unsupervised Learning Rules
  1. Unsupervised learning generally works without predefined target labels.
  2. Clustering groups observations according to similarities or distances.
  3. K-Means requires choosing the number of clusters.
  4. Cluster labels are identifiers, not rankings.
  5. Feature scaling can strongly affect distance-based algorithms.
  6. DBSCAN can identify dense groups and noise observations.
  7. Hierarchical clustering can reveal nested relationships.
  8. PCA reduces dimensions and creates new transformed components.
  9. Isolation Forest can be used for anomaly detection.
  10. Cluster quality should be evaluated using appropriate methods.
  11. Business meaning should be validated after patterns are discovered.
  12. Unsupervised results are exploratory and should not automatically be treated as ground truth.
Key Idea: Unsupervised Learning helps discover hidden structure in data when predefined labels are unavailable. Python libraries such as Scikit-learn provide tools for clustering, dimensionality reduction, and anomaly detection that can turn large datasets into useful patterns for further analysis.

Python Unsupervised Learning • Clustering • PCA • Anomaly Detection • Real-Time Examples