Kickstart ML with Python snippets

Getting started with clustering (unsupervised learning) in Python

Clustering is a type of unsupervised learning that involves grouping data points into clusters based on their similarities.

What is Clustering?

Clustering is the process of dividing a dataset into groups, or clusters, where the data points in each cluster are more similar to each other than to those in other clusters. It is a form of unsupervised learning because it doesn't rely on predefined labels or outcomes.

Why Use Clustering?

  • Exploratory Data Analysis: To uncover hidden patterns or structures in data.
  • Data Compression: To reduce the complexity of data by grouping similar data points together.
  • Anomaly Detection: To identify outliers or unusual data points.
  • Segmentation: To segment customers, markets, or other entities into distinct groups for targeted analysis or action.

Clustering Methods Overview

Clustering methods can be broadly categorized into four types: Partitioning, Hierarchical, Density-based, and Grid-based clustering. Each method has unique characteristics and is suited for different types of data and clustering requirements.

1. Partitioning Clustering

Partitioning Clustering methods divide the data into distinct, non-overlapping subsets (clusters). The goal is to partition the dataset into K clusters, where each cluster has a centroid, and each data point belongs to the cluster with the nearest centroid.


  • K-Means:

    • Algorithm:
      1. Initialize K cluster centroids randomly.
      2. Assign each data point to the nearest centroid.
      3. Update centroids by calculating the mean of the assigned points.
      4. Repeat steps 2 and 3 until centroids converge.
    • Applications: Customer segmentation, image compression.
  • K-Medoids (PAM):

    • Algorithm:
      1. Initialize K medoids (actual data points) randomly.
      2. Assign each data point to the nearest medoid.
      3. Update medoids by minimizing the sum of dissimilarities between points and their medoids.
      4. Repeat steps 2 and 3 until medoids converge.
    • Applications: More robust to outliers compared to K-Means.

2. Hierarchical Clustering

Hierarchical Clustering methods build a hierarchy of clusters by either merging smaller clusters into larger ones (agglomerative) or splitting larger clusters into smaller ones (divisive). The result is a dendrogram, which represents the nested structure of the clusters.


  • Hierarchical Agglomerative Clustering (HAC):

    • Algorithm:
      1. Start with each data point as its own cluster.
      2. Merge the two closest clusters based on a distance metric (e.g., Euclidean distance).
      3. Repeat step 2 until all points are merged into a single cluster.
    • Applications: Gene expression analysis, social network analysis.
  • Divisive Clustering:

    • Algorithm:
      1. Start with a single cluster containing all data points.
      2. Recursively split the clusters into smaller clusters.
      3. Continue splitting until each cluster contains a single data point.
    • Applications: Less common, but useful for specific hierarchical structures.

3. Density-Based Clustering

Density-Based Clustering methods identify clusters based on the density of data points in a region. Clusters are formed in regions of high density, separated by regions of low density.


  • DBSCAN (Density-Based Spatial Clustering of Applications with Noise):

    • Algorithm:
      1. Select a point randomly and find all points within a given radius (epsilon).
      2. If the number of points within this radius exceeds a threshold (minPts), form a cluster.
      3. Expand the cluster by including all density-reachable points.
      4. Repeat steps 1-3 for all points, identifying clusters and noise.
    • Applications: Spatial data analysis, anomaly detection.
  • OPTICS (Ordering Points To Identify the Clustering Structure):

    • Algorithm:
      1. Similar to DBSCAN but with varying density.
      2. Orders the points based on their reachability distance.
      3. Clusters are identified by analyzing the reachability plot.
    • Applications: Complex data with varying densities.

4. Grid-Based Clustering

Grid-Based Clustering methods divide the data space into a finite number of cells forming a grid structure. The clustering is performed on the grid cells instead of the actual data points, which makes the method efficient for large datasets.


  • STING (Statistical Information Grid):

    • Algorithm:
      1. Divide the data space into rectangular cells.
      2. Compute statistical information for each cell (e.g., mean, variance).
      3. Use this information to merge or split cells to form clusters.
    • Applications: Efficient clustering for large spatial datasets.
  • CLIQUE (Clustering In QUEst):

    • Algorithm:
      1. Divide the data space into grids.
      2. Identify dense cells based on a density threshold.
      3. Form clusters by merging adjacent dense cells.
    • Applications: High-dimensional data clustering.

Summary of Key Concepts

  1. Partitioning Clustering:

    • Divides data into K non-overlapping clusters.
    • Examples: K-Means, K-Medoids.
    • Applications: Customer segmentation, image compression.
  2. Hierarchical Clustering:

    • Builds a tree-like structure of clusters.
    • Examples: HAC, Divisive Clustering.
    • Applications: Gene expression analysis, social network analysis.
  3. Density-Based Clustering:

    • Forms clusters based on data density.
    • Examples: DBSCAN, OPTICS.
    • Applications: Spatial data analysis, anomaly detection.
  4. Grid-Based Clustering:

    • Divides data space into a grid structure.
    • Examples: STING, CLIQUE.
    • Applications: Efficient clustering for large spatial and high-dimensional datasets.

Back to Kickstart ML with Python cookbook page