xbe.at

Machine learning / Classification and clustering

Understanding Popular Clustering Algorithms

9 min read, 15 sections

Contents
  1. Introduction to Clustering
  2. Why We Need Clustering
  3. Popular Distance Metrics
  4. K-Means Clustering
  5. K-Means Algorithm Steps
  6. Optimizing K in K-Means
  7. Hierarchical Clustering
  8. Agglomerative Hierarchical Clustering
  9. DBSCAN (Density-Based Spatial Clustering of Applications with Noise)
  10. DBSCAN Parameters
  11. Gaussian Mixture Models (GMM)
  12. Comparing Clustering Algorithms
  13. Real-Life Example: Image Segmentation
  14. Real-Life Example: Document Clustering
  15. Additional Resources

Introduction to Clustering

Clustering is an unsupervised machine learning technique used to group similar data points together. It's essential for discovering patterns and structures in datasets without predefined labels. Clustering helps in data exploration, customer segmentation, anomaly detection, and much more.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Generate random data
np.random.seed(42)
X = np.random.randn(300, 2)

# Perform K-means clustering
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')
plt.title('K-means Clustering Example')
plt.show()

Why We Need Clustering

Clustering allows us to uncover hidden patterns in data, making it invaluable for various applications. It helps in customer segmentation for targeted marketing, image compression by grouping similar pixels, and even in bioinformatics for gene expression analysis. Clustering can also be used for anomaly detection by identifying data points that don't fit into any cluster.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Generate sample customer data
np.random.seed(42)
age = np.random.randint(18, 70, 1000)
income = np.random.randint(20000, 100000, 1000)
X = np.column_stack((age, income))

# Perform K-means clustering
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')
plt.xlabel('Age')
plt.ylabel('Income')
plt.title('Customer Segmentation Example')
plt.show()

Popular Distance Metrics

Distance metrics are crucial in clustering as they determine how similarity between data points is measured. Common metrics include Euclidean distance, Manhattan distance, and Cosine similarity. The choice of metric depends on the nature of the data and the specific clustering problem.

import numpy as np

def euclidean_distance(x1, x2):
    return np.sqrt(np.sum((x1 - x2)**2))

def manhattan_distance(x1, x2):
    return np.sum(np.abs(x1 - x2))

def cosine_similarity(x1, x2):
    return np.dot(x1, x2) / (np.linalg.norm(x1) * np.linalg.norm(x2))

# Example usage
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(f"Euclidean distance: {euclidean_distance(a, b):.2f}")
print(f"Manhattan distance: {manhattan_distance(a, b):.2f}")
print(f"Cosine similarity: {cosine_similarity(a, b):.2f}")

K-Means Clustering

K-Means is one of the most popular clustering algorithms. It aims to partition n observations into k clusters, where each observation belongs to the cluster with the nearest mean. The algorithm iteratively assigns points to clusters and updates cluster centers until convergence.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Generate sample data
np.random.seed(42)
X = np.random.randn(300, 2)

# Perform K-means clustering
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], 
            marker='x', s=200, linewidths=3, color='r', label='Centroids')
plt.title('K-means Clustering')
plt.legend()
plt.show()

K-Means Algorithm Steps

The K-Means algorithm follows these steps: 1) Initialize k cluster centers randomly. 2) Assign each data point to the nearest cluster center. 3) Update cluster centers by calculating the mean of all points in each cluster. 4) Repeat steps 2 and 3 until convergence or a maximum number of iterations is reached.

import numpy as np

def kmeans(X, k, max_iters=100):
    # Initialize cluster centers randomly
    centers = X[np.random.choice(X.shape[0], k, replace=False)]
    
    for _ in range(max_iters):
        # Assign points to nearest center
        distances = np.sqrt(((X - centers[:, np.newaxis])**2).sum(axis=2))
        labels = np.argmin(distances, axis=0)
        
        # Update centers
        new_centers = np.array([X[labels == i].mean(axis=0) for i in range(k)])
        
        # Check for convergence
        if np.all(centers == new_centers):
            break
        
        centers = new_centers
    
    return labels, centers

# Example usage
X = np.random.randn(100, 2)
labels, centers = kmeans(X, k=3)
print("Cluster centers:", centers)

Optimizing K in K-Means

Choosing the optimal number of clusters (k) is crucial for K-Means. The Elbow Method is a common approach, where we plot the within-cluster sum of squares (WCSS) against different k values. The "elbow" in the plot suggests a good k value, balancing cluster quality and complexity.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Generate sample data
np.random.seed(42)
X = np.random.randn(300, 2)

# Calculate WCSS for different k values
wcss = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters=k, random_state=42)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)

# Plot the Elbow Curve
plt.plot(range(1, 11), wcss, marker='o')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('WCSS')
plt.title('Elbow Method for Optimal k')
plt.show()

Hierarchical Clustering

Hierarchical clustering builds a tree of clusters, either by merging smaller clusters (agglomerative) or dividing larger clusters (divisive). This approach doesn't require specifying the number of clusters beforehand and provides a hierarchical representation of the data structure.

import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage

# Generate sample data
np.random.seed(42)
X = np.random.randn(50, 2)

# Perform hierarchical clustering
Z = linkage(X, method='ward')

# Plot the dendrogram
plt.figure(figsize=(10, 7))
dendrogram(Z)
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Sample Index')
plt.ylabel('Distance')
plt.show()

Agglomerative Hierarchical Clustering

Agglomerative clustering starts with each data point as a separate cluster and iteratively merges the closest clusters. The process continues until all points belong to a single cluster or a stopping criterion is met. Different linkage methods (e.g., single, complete, average) determine how inter-cluster distances are calculated.

import numpy as np
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
X = np.random.randn(100, 2)

# Perform agglomerative clustering
agg_clustering = AgglomerativeClustering(n_clusters=3)
labels = agg_clustering.fit_predict(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.title('Agglomerative Hierarchical Clustering')
plt.show()

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

DBSCAN is a density-based clustering algorithm that groups together points that are closely packed together, marking points in low-density regions as outliers. It doesn't require specifying the number of clusters and can discover clusters of arbitrary shape.

import numpy as np
from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt

# Generate sample data with noise
np.random.seed(42)
X = np.random.randn(300, 2)
X = np.vstack((X, np.random.uniform(low=-4, high=4, size=(50, 2))))  # Add noise

# Perform DBSCAN clustering
dbscan = DBSCAN(eps=0.3, min_samples=5)
labels = dbscan.fit_predict(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.title('DBSCAN Clustering')
plt.show()

DBSCAN Parameters

DBSCAN has two main parameters: eps (ε) and min_samples. Eps defines the maximum distance between two samples to be considered as neighbors. Min_samples specifies the minimum number of samples in a neighborhood for a point to be considered a core point. These parameters significantly affect the clustering results.

import numpy as np
from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
X = np.random.randn(300, 2)

# Function to plot DBSCAN results
def plot_dbscan(X, eps, min_samples):
    dbscan = DBSCAN(eps=eps, min_samples=min_samples)
    labels = dbscan.fit_predict(X)
    plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
    plt.title(f'DBSCAN (eps={eps}, min_samples={min_samples})')
    plt.show()

# Plot with different parameters
plot_dbscan(X, eps=0.3, min_samples=5)
plot_dbscan(X, eps=0.5, min_samples=10)

Gaussian Mixture Models (GMM)

Gaussian Mixture Models assume that the data points are generated from a mixture of a finite number of Gaussian distributions with unknown parameters. GMM is a probabilistic model that provides soft assignments of data points to clusters, allowing for more flexible and nuanced clustering.

import numpy as np
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
X = np.concatenate([np.random.randn(100, 2), np.random.randn(100, 2) + [3, 3]])

# Fit Gaussian Mixture Model
gmm = GaussianMixture(n_components=2, random_state=42)
labels = gmm.fit_predict(X)

# Plot the results
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.title('Gaussian Mixture Model Clustering')
plt.show()

Comparing Clustering Algorithms

Different clustering algorithms have their strengths and weaknesses. K-Means is simple and fast but assumes spherical clusters. Hierarchical clustering provides a dendrogram but can be computationally expensive. DBSCAN handles noise well and finds arbitrary-shaped clusters. GMM provides probabilistic assignments but assumes Gaussian distributions.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.mixture import GaussianMixture

# Generate sample data
np.random.seed(42)
X = np.concatenate([np.random.randn(100, 2), np.random.randn(100, 2) + [3, 3]])

# Apply different clustering algorithms
kmeans = KMeans(n_clusters=2, random_state=42)
hierarchical = AgglomerativeClustering(n_clusters=2)
dbscan = DBSCAN(eps=0.5, min_samples=5)
gmm = GaussianMixture(n_components=2, random_state=42)

algorithms = [kmeans, hierarchical, dbscan, gmm]
titles = ['K-Means', 'Hierarchical', 'DBSCAN', 'GMM']

# Plot results
fig, axs = plt.subplots(2, 2, figsize=(12, 12))
for ax, algorithm, title in zip(axs.ravel(), algorithms, titles):
    labels = algorithm.fit_predict(X)
    ax.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
    ax.set_title(title)

plt.tight_layout()
plt.show()

Real-Life Example: Image Segmentation

Clustering algorithms can be applied to image segmentation, a crucial task in computer vision. By treating each pixel as a data point with color features, we can use clustering to group similar pixels and segment the image into distinct regions.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from skimage import io

# Load and preprocess the image
image = io.imread('path_to_your_image.jpg')
pixels = image.reshape(-1, 3)

# Apply K-means clustering
kmeans = KMeans(n_clusters=5, random_state=42)
labels = kmeans.fit_predict(pixels)

# Reshape the labels back to the image shape
segmented_image = labels.reshape(image.shape[:2])

# Plot the original and segmented images
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.imshow(image)
ax1.set_title('Original Image')
ax2.imshow(segmented_image, cmap='viridis')
ax2.set_title('Segmented Image')
plt.show()

Real-Life Example: Document Clustering

Clustering can be applied to group similar documents based on their content. This is useful for organizing large collections of text data, such as news articles or scientific papers. We'll use TF-IDF vectorization to represent documents and apply K-Means clustering.

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
import numpy as np

# Sample documents
documents = [
    "Machine learning is a subset of artificial intelligence",
    "Deep learning is a part of machine learning",
    "Neural networks are used in deep learning",
    "Clustering is an unsupervised learning technique",
    "K-means is a popular clustering algorithm"
]

# Convert documents to TF-IDF vectors
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(documents)

# Apply K-means clustering
kmeans = KMeans(n_clusters=2, random_state=42)
labels = kmeans.fit_predict(X)

# Print results
for doc, label in zip(documents, labels):
    print(f"Cluster {label}: {doc}")

Additional Resources

For those interested in diving deeper into clustering algorithms and their applications, here are some valuable resources:

  1. "A Survey of Clustering Data Mining Techniques" by Pavel Berkhin (2006) ArXiv: https://arxiv.org/abs/cs/0604008
  2. "Cluster Analysis: Basic Concepts and Algorithms" chapter from "Introduction to Data Mining" by Pang-Ning Tan, Michael Steinbach, and Vipin Kumar Available online: https://www-users.cse.umn.edu/~kumar001/dmbook/ch8.pdf
  3. "Clustering" chapter from "The Elements of Statistical Learning" by Trevor Hastie, Robert Tibshirani, and Jerome Friedman Available online: https://web.stanford.edu/~hastie/ElemStatLearn/
  4. "A Tutorial on Spectral Clustering" by Ulrike von Luxburg (2007) ArXiv: https://arxiv.org/abs/0711.0189
  5. "Density-Based Clustering Based on Hierarchical Density Estimates" by Ricardo J. G. B. Campello, Davoud Moulavi, and Jörg Sander (2013) Available through: https://link.springer.com/chapter/10.1007/978-3-642-37456-2_14

These resources provide in-depth explanations of various clustering techniques, their theoretical foundations, and practical applications. They are suitable for readers looking to expand their knowledge beyond the introductory level presented in this slideshow.

More in Classification and clustering

  1. Understanding the Elbow Method in K-Means Clustering6 min

    The Elbow Method is a heuristic technique used to determine the optimal number of clusters (k) in K-means clustering by analyzing the relationship between...

  2. Understanding Decision Tree Limitations10 min

    Traditional decision trees suffer from axis-parallel splits, meaning they can only create rectangular decision boundaries by splitting features along...

  3. Unlocking Dynamic Weighted k-Nearest Neighbors in Python11 min

    KNN is a simple yet powerful machine learning algorithm used for classification and regression tasks. It works by finding the k closest data points to a...

  4. Time Complexity of K-Means Clustering in Python9 min

    K-means clustering is a popular unsupervised machine learning algorithm used for partitioning a dataset into K distinct, non-overlapping subgroups or...

  5. Unsupervised Anomaly Detection Methods11 min

    Isolation Forest is a powerful unsupervised anomaly detection method based on the principle that anomalies are easier to isolate than normal data points. It...

  6. The Power of Decision Trees in AI17 min

    A decision tree is a hierarchical model that makes sequential decisions based on feature values, splitting the data into increasingly homogeneous subsets....